Skip to content

fix: don't break a live lock on staleness alone (#432) - #445

Open
arzaanxeng wants to merge 2 commits into
agentrhq:mainfrom
arzaanxeng:main
Open

fix: don't break a live lock on staleness alone (#432)#445
arzaanxeng wants to merge 2 commits into
agentrhq:mainfrom
arzaanxeng:main

Conversation

@arzaanxeng

Copy link
Copy Markdown
Contributor

Fixes #432

Bug

breakIfAbandoned() decided a lock was abandoned if expired OR ownerGone.
That meant a same-host owner that was still alive and running could have its
lock stolen just because its critical section took longer than staleMs
(big file, slow disk, loaded machine). Two processes would then race the same
file, and whichever rename() landed last silently discarded the other's work.

Fix

  • breakIfAbandoned() now checks owner liveness first: if the owner's pid is
    checkable and confirmed alive on the same host, the lock is never broken on
    staleness alone — liveness is decisive, not just one of two OR'd signals.
  • withFileLock() now runs a heartbeat while the critical section is active,
    refreshing the lock file's mtime every staleMs / 3. This covers the case
    where liveness can't be checked (cross-host lock files), so staleMs still
    reflects real inactivity rather than just how long the section has run.
  • Updated the header comment, which documented the old (buggy) "staleMs
    breaks regardless" behavior.

Testing

Added two regression tests in file-lock.test.ts that reproduce the exact
scenario from the issue: a live same-host holder whose lock is older than
staleMs, and a second acquirer racing it mid-critical-section. Both new
tests fail against the old code and pass with this fix. Full suite:
8/8 passing.

Enhance file lock mechanism with heartbeat and mtime refresh to prevent premature lock expiration.
@github-actions

Copy link
Copy Markdown
Contributor

🟠 Maintainer review suggested — low confidence

The automated review could not reach a fully supported conclusion.

Limitations

  • The automated review returned an invalid structured result.

This review is advisory and does not block merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

site-memory file lock breaks mutual exclusion when a critical section runs longer than staleMs

1 participant