fix(evolution): use CompareAndSwap for atomic lockStoreFile repair
Replace the Delete+LoadOrStore pattern with CompareAndSwap to atomically swap in a fresh mutex when a corrupted entry is detected. Changes: - Use a retry loop with CAS instead of Delete+LoadOrStore, avoiding the race where two goroutines could create different mutexes - Add nil check: a nil *sync.Mutex passes the type assertion but panics on Lock() — now handled by the CAS recovery path - Use continue-loop retry instead of unchecked second assertion CompareAndSwap guarantees that when multiple goroutines detect a corrupted entry, they converge on the same replacement mutex.
This commit is contained in:
parent
f5add27d39
commit
bbdf746bcb
1 changed files with 13 additions and 12 deletions
|
|
@ -554,19 +554,20 @@ func isInvalidJSON(err error) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func lockStoreFile(path string) func() {
|
func lockStoreFile(path string) func() {
|
||||||
actual, _ := storeFileLocks.LoadOrStore(path, &sync.Mutex{})
|
for {
|
||||||
mu, ok := actual.(*sync.Mutex)
|
actual, _ := storeFileLocks.LoadOrStore(path, &sync.Mutex{})
|
||||||
if !ok {
|
mu, ok := actual.(*sync.Mutex)
|
||||||
// Delete the corrupted entry so that the next LoadOrStore
|
if !ok || mu == nil {
|
||||||
// atomically creates a fresh mutex. Multiple goroutines may
|
// Corrupted entry (wrong type or nil *sync.Mutex).
|
||||||
// race to Delete, but LoadOrStore ensures they all converge
|
// Atomically swap in a fresh mutex via CompareAndSwap.
|
||||||
// on the same mutex before entering the critical section.
|
// If CAS fails, another goroutine already replaced it —
|
||||||
storeFileLocks.Delete(path)
|
// just retry the loop to pick up the valid entry.
|
||||||
actual, _ = storeFileLocks.LoadOrStore(path, &sync.Mutex{})
|
storeFileLocks.CompareAndSwap(path, actual, &sync.Mutex{})
|
||||||
mu = actual.(*sync.Mutex)
|
continue
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
return mu.Unlock
|
||||||
}
|
}
|
||||||
mu.Lock()
|
|
||||||
return mu.Unlock
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) profilePath(workspaceID, skillName string) (string, error) {
|
func (s *Store) profilePath(workspaceID, skillName string) (string, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue