You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: Resolve race condition in file provider and TestUnwatchFile
Fixes race conditions detected when running tests with -race flag.
## Root Cause
The file provider had a race condition where:
1. Watch() assigns to f.w (fsnotify.Watcher field)
2. Previous watcher's cleanup goroutine calls f.w.Close()
3. These operations could happen concurrently when re-watching after unwatch
Additionally, TestUnwatchFile had a race between:
- Main test goroutine reading/writing `reloaded` boolean variable
- Watch callback goroutine writing to `reloaded` variable
## Solution
1. **File Provider**: Add mutex protection around watcher state changes
- Added `mu sync.Mutex` to File struct
- Protected Watch() and Unwatch() methods with mutex
- Protected cleanup code in watch goroutine with mutex
- Added nil checks for defensive programming
2. **TestUnwatchFile**: Use atomic operations instead of plain boolean
- Changed `reloaded bool` to `reloaded int32`
- Use atomic.StoreInt32/LoadInt32 for thread-safe access
- Test now properly verifies re-watching capability after unwatch
## Testing
- All watch-related tests pass with race detector: TestWatchFile, TestWatchFileSymlink, TestWatchFileDirectorySymlink, TestUnwatchFile
- All submodule tests continue to pass with race detection
- CI pattern `github.com/knadh/koanf...` properly tests all submodules
Created using prompt: "alright, lets then do a separate commit now to fix the test unwatch file race" and "maybe a better approach will be to add a mutex for file watcher instad?"
0 commit comments