Commit 25e04bd9 authored by David Vorick's avatar David Vorick

panic on overlapping files

Don't print the stack during Critical for tests. Don't print the github issues
string either.
parent e328dc0d
...@@ -12,8 +12,10 @@ import ( ...@@ -12,8 +12,10 @@ import (
// the running goroutine is printed to help determine the error. // the running goroutine is printed to help determine the error.
func Critical(v ...interface{}) { func Critical(v ...interface{}) {
s := "Critical error: " + fmt.Sprintln(v...) + "Please submit a bug report here: https://github.com/NebulousLabs/Sia/issues\n" s := "Critical error: " + fmt.Sprintln(v...) + "Please submit a bug report here: https://github.com/NebulousLabs/Sia/issues\n"
debug.PrintStack() if Release != "testing" {
os.Stderr.WriteString(s) debug.PrintStack()
os.Stderr.WriteString(s)
}
if DEBUG { if DEBUG {
panic(s) panic(s)
} }
...@@ -26,8 +28,10 @@ func Critical(v ...interface{}) { ...@@ -26,8 +28,10 @@ func Critical(v ...interface{}) {
// integrity. // integrity.
func Severe(v ...interface{}) { func Severe(v ...interface{}) {
s := "Severe error: " + fmt.Sprintln(v...) s := "Severe error: " + fmt.Sprintln(v...)
debug.PrintStack() if Release != "testing" {
os.Stderr.WriteString(s) debug.PrintStack()
os.Stderr.WriteString(s)
}
if DEBUG { if DEBUG {
panic(s) panic(s)
} }
......
...@@ -95,6 +95,7 @@ func LoadJSON(meta Metadata, object interface{}, filename string) error { ...@@ -95,6 +95,7 @@ func LoadJSON(meta Metadata, object interface{}, filename string) error {
_, exists := activeFiles[filename] _, exists := activeFiles[filename]
if exists { if exists {
build.Critical(ErrFileInUse, filename)
return ErrFileInUse return ErrFileInUse
} }
activeFiles[filename] = struct{}{} activeFiles[filename] = struct{}{}
...@@ -145,6 +146,7 @@ func SaveJSON(meta Metadata, object interface{}, filename string) error { ...@@ -145,6 +146,7 @@ func SaveJSON(meta Metadata, object interface{}, filename string) error {
_, exists := activeFiles[filename] _, exists := activeFiles[filename]
if exists { if exists {
build.Critical(ErrFileInUse, filename)
return ErrFileInUse return ErrFileInUse
} }
activeFiles[filename] = struct{}{} activeFiles[filename] = struct{}{}
......
...@@ -72,19 +72,25 @@ func TestSaveLoadJSON(t *testing.T) { ...@@ -72,19 +72,25 @@ func TestSaveLoadJSON(t *testing.T) {
// Try saving the object multiple times concurrently. // Try saving the object multiple times concurrently.
var wg sync.WaitGroup var wg sync.WaitGroup
errs := make([]error, 250) errs := make([]bool, 250)
for i := 0; i < 250; i++ { for i := 0; i < 250; i++ {
wg.Add(1) wg.Add(1)
go func(i int) { go func(i int) {
errs[i] = SaveJSON(testMeta, obj1, obj1Filename) defer wg.Done()
wg.Done() defer func() {
r := recover() // Error is irrelevant, managed by err slice.
if r != nil {
errs[i] = true
}
}()
SaveJSON(testMeta, obj1, obj1Filename)
}(i) }(i)
} }
wg.Wait() wg.Wait()
// At least one of the saves should have complained about concurrent usage. // At least one of the saves should have complained about concurrent usage.
var found bool var found bool
for i := range errs { for i := range errs {
if errs[i] == ErrFileInUse { if errs[i] {
found = true found = true
break break
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment