Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(flock): race condition in acquire (#423)
There was a race condition that enabled multiple processes to acquire the same lock file. Consider the following sequence: - Process A successfully acquires lock - Process B enters acquire function - Process B calls unix.Open - Process A calls release(), deleting the lock file - Process B calls unix.Flock on the still-open file descriptor, successfully locking the now deleted file. - Process C successfully acquires lock on newly created lock file After this sequence both B and C hold the lock. To resolve this race, we can simply avoid removing the file. This is what e.g. dpkg does: the /var/lib/dpkg/lock file is always present under normal circumstances. The issue was discovered by repeatedly running TestContinueFlock: ``` === RUN TestContinueFlock flock_test.go:85: Did not expect an error but got: remove /tmp/TestContinueFlock3236442975/001/lock: no such file or directory --- FAIL: TestContinueFlock (0.10s) ``` That error, while otherwise benign, indicates that the two goroutines in the test hit the race condition in question.
- Loading branch information