-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/datarace when concurrent save the same file #836
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,17 @@ import ( | |
"os" | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
) | ||
|
||
var ( | ||
fileLocks = make(map[string]*sync.Mutex) | ||
filesLock = sync.Mutex{} | ||
) | ||
|
||
// SaveFileWithBackup will backup the file before save it. | ||
// e.g., backup meta.yaml as meta-2006-01-02T15:04:05Z07:00.yaml | ||
// backup the files in the same dir of path if backupDir is empty. | ||
|
@@ -25,6 +31,15 @@ func SaveFileWithBackup(path string, data []byte, backupDir string) error { | |
return errors.Errorf("%s is directory", path) | ||
} | ||
|
||
filesLock.Lock() | ||
defer filesLock.Unlock() | ||
if _, ok := fileLocks[path]; !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fileLocks itself is not thread safe, so I think the datarace is still exists. eg. # assume the fileLocks is empty as initial state.
# thread 1 start
if _, ok := fileLocks["/path/a"]; !ok { # the ok is false because fileLocks is empty
# thread 1 hang after check !ok but before fileLocks[path] = &sync.Mutex{}
# thread 2 start
if _, ok := fileLocks["/path/a"]; !ok { # the ok is false because fileLocks is empty and thread 1 doesn't assign it yet
fileLocks["/path/a"] = &sync.Mutex{} # assign a new mutex and success
}
fileLocks["/path/a"].Lock()
# thread 2 enter critical area
# thread 1 start
fileLocks["/path/a"] = &sync.Mutex{} # assign a new mutex and success
fileLocks["/path/a"].Lock() # this will success because thread 1 reset fileLocks["/path/a"]
# thread 1 enter critical area In this case, both threads enter into the same critical area. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your suggestion, sorry to forget that the map needs to be locked in concurrent access. |
||
fileLocks[path] = &sync.Mutex{} | ||
} | ||
|
||
fileLocks[path].Lock() | ||
defer fileLocks[path].Unlock() | ||
|
||
// backup file | ||
if !os.IsNotExist(err) { | ||
base := filepath.Base(path) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to revert this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to run tests with
-race
, but which need CGO was enabled. Seems there is no effective to setGO111MODULE=on CGO_ENABLED=1
before$(GO)
, such as