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
{{ message }}
Repository navigation
fix(lockutil): live-lock restoration can admit two concurrent holders #831
Static concurrency review; applies to the cross-platform lock protocol.
Steps to reproduce
The race is documented directly in internal/lockutil/reclaim.go:50-61:
A reclaimer renames a suspected stale lock away.
The moved lock is found to belong to a live holder.
Before restoration, another claimant observes the canonical path as absent and succeeds with O_EXCL.
restoreLiveLock uses replacing os.Rename, overwriting the new claimant's lock file.
The live original holder and the new claimant can both execute their protected critical sections.
internal/daemon/lock.go:100-106 compounds this because release removes the current path without checking an ownership token.
Expected behavior
At most one process should enter the protected critical section, and one holder must never remove another holder's lock.
Actual behavior
The restore window can admit two holders. A later release can remove a lock path that no longer belongs to the releasing instance.
Suggested fix and regression test
Use OS-backed locks held for the full critical-section lifetime (flock/fcntl on Unix and LockFileEx on Windows), or redesign recovery so a potentially live lock is never moved. Make release ownership-aware with an unguessable token.
Add a coordinated multiprocess test with a live holder, reclaimer, and third O_EXCL claimant and assert that no two critical sections overlap.
Reported by PierrunoYT from a full codebase audit.
Confirmed on current main. restoreLiveLock (internal/lockutil/reclaim.go:26) still calls plain os.Rename(reclaimed, path), which replaces an existing file, so a claimant that won the O_EXCL create in the gap has its lock file silently overwritten and both it and the live original end up inside the critical section.
Sorry for the wait on this one. We want the fix. The restore has to be non-replacing: attempt a create-exclusive restore and, when the canonical path already exists, treat that as "another claimant holds it now" and drop the reclaimed copy rather than forcing it back. Send the PR and I will review it.
Approved. Verified at internal/lockutil/reclaim.go — and worth noting the race is already described in the code comment above ReclaimStaleLock, which states plainly that the window is "inherent, unclosed" and that restoreLiveLock "only shrinks the window to roughly one syscall, it does not close it."
So this is not a new discovery, it is an accepted limitation being raised as a defect, which is fair: the comment also names the real fix, an OS-level advisory lock (flock / LockFileEx) held for the whole critical section and checked non-destructively rather than by moving the file. Approving on that basis. Anyone picking it up should treat the existing comment as the design brief.
Activity
Vasanthdev2004 commentedon Aug 5, 2026
Confirmed on current main.
restoreLiveLock(internal/lockutil/reclaim.go:26) still calls plainos.Rename(reclaimed, path), which replaces an existing file, so a claimant that won theO_EXCLcreate in the gap has its lock file silently overwritten and both it and the live original end up inside the critical section.Sorry for the wait on this one. We want the fix. The restore has to be non-replacing: attempt a create-exclusive restore and, when the canonical path already exists, treat that as "another claimant holds it now" and drop the reclaimed copy rather than forcing it back. Send the PR and I will review it.
Vasanthdev2004 commentedon Aug 12, 2026
Approved. Verified at
internal/lockutil/reclaim.go— and worth noting the race is already described in the code comment aboveReclaimStaleLock, which states plainly that the window is "inherent, unclosed" and thatrestoreLiveLock"only shrinks the window to roughly one syscall, it does not close it."So this is not a new discovery, it is an accepted limitation being raised as a defect, which is fair: the comment also names the real fix, an OS-level advisory lock (
flock/LockFileEx) held for the whole critical section and checked non-destructively rather than by moving the file. Approving on that basis. Anyone picking it up should treat the existing comment as the design brief.