Repository navigation
fix(codex): stop a stale takeover handing the lock to two takers - #4591
Merged
Merged
Conversation
`withSessionLock` aged a lock by its directory mtime, but the taker that won a takeover stamped the `owner` file before refreshing that mtime. A racer that lost the stamp retried immediately, read the still-old mtime, and `claimStaleLock` renamed the live winner's stamp aside to install its own — two holders in the critical section at once. On Linux the retry reliably lands inside that window, which is why `plugin-tests` failed on essentially every PR. Age the lock by its own stamp instead, and settle takeovers with a claim directory derived from the state the taker read: everyone who saw the same dead lock races one atomic `mkdir`, exactly one wins, and the losers re-read a lock that now carries the winner's fresh stamp. Neither the directory nor the stamp is ever momentarily absent, so no racer can slip in alongside the taker. Verified with 8 racers over 30 rounds: 4 concurrent holders before, 1 after.
There was a problem hiding this comment.
🟡 Changes recommended
The new claim-directory takeover flow still has a race that can allow overwriting a live winner’s stamp (and can throw on mid-takeover deletion) unless it re-validates the observed lock generation under the claim.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes a real race in withSessionLock that could allow concurrent “stale lock takeovers” to result in multiple holders entering the critical section, and adds regression coverage for an abandoned takeover-claim scenario.
Changes:
- Switch lock staleness aging to prefer the
ownerstamp mtime (fall back to directory mtime only before stamping exists). - Rework stale takeover arbitration to use an atomic per-generation claim directory rather than manipulating the
ownerfile directly. - Refresh the
ownerstamp on heartbeat and make lock release recursively remove the lock directory (including leftover claim dirs), with a new regression test.
File summaries
| File | Description |
|---|---|
| examples/codex-memory-plugin/scripts/session-state.mjs | Updates lock staleness and takeover mechanics; refreshes owner stamp and improves cleanup semantics. |
| examples/codex-memory-plugin/scripts/session-state.test.mjs | Adds regression coverage for abandoned claim directories not wedging the lock. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
6 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
plugin-testshas been failing on nearly every PR withconcurrent stale takeovers leave exactly one holder — 2 !== 1. It is a real bug inwithSessionLock, not a flaky test.withSessionLockaged a lock by the lock directory's mtime, but a taker that wins a takeover stamps theownerfile before refreshing that mtime. A racer that lost the stamp retried immediately, still read the old directory mtime, judged the lock stale again, andclaimStaleLockrenamed the live winner's stamp aside to install its own — two holders in the critical section at once. On Linux the retry reliably lands inside that window; on macOS the timing happens to miss it, which is why it never reproduced locally.Human Involvement
Type of Change
Changes Made
ownerstamp, falling back to the directory only while no stamp exists (a holder killed betweenmkdirand its write still ages out).mkdir, exactly one wins, and the losers re-read a lock that now carries the winner's fresh stamp. Neither the directory nor the stamp is ever momentarily absent, so no racer can claim alongside the taker.staleMs.Testing
plugin-testscommand: 409 tests, 408 pass, 0 fail, 1 skipped)Stress harness with 8 racers over 30 rounds on a stale lock: 4 concurrent holders before the fix, 1 after.
Additional Notes
Not specific to any one PR — the bug is on
main, so every open PR picks up the failure.