Skip to content

fix(lockutil): live-lock restoration can admit two concurrent holders #831

Closed

Description

@PierrunoYT

Version / branch / commit

main at 097c2652cba22d929c598347aacc6f95591c9031

OS and environment

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:

  1. A reclaimer renames a suspected stale lock away.
  2. The moved lock is found to belong to a live holder.
  3. Before restoration, another claimant observes the canonical path as absent and succeeds with O_EXCL.
  4. restoreLiveLock uses replacing os.Rename, overwriting the new claimant's lock file.
  5. 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.

Activity

  1. Vasanthdev2004 commented on Aug 5, 2026

    @Vasanthdev2004
    Collaborator

    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.

  2. added
    bug
    on Aug 5, 2026
  3. Vasanthdev2004 commented on Aug 12, 2026

    @Vasanthdev2004
    Collaborator

    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.

  4. added
    issue-approved
    on Aug 12, 2026
  5. self-assigned this
    on Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugissue-approved

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions