Repository navigation
fix(save): only unlink save.lock when this process actually owns it - #168
Merged
fdaviddpt merged 1 commit intoJul 26, 2026
Merged
Conversation
save-session.sh installs `trap cleanup EXIT` before it attempts to acquire save.lock, and cleanup() unlinked $LOCK_FILE unconditionally. A process that discovers another live PID holds the lock exits 0 to skip — but that exit runs the same trap, deleting the still-running holder's lock file. A third process racing in right after finds no lock at all, acquires cleanly, and now runs concurrently with the original holder: two live save-session.sh processes, two Haiku calls against overlapping transcript spans, two save-position writes racing each other. Fix: a HAVE_LOCK flag, set true on both paths that actually take ownership (fresh noclobber write, stale-lock takeover), never set on the skip path. cleanup() only unlinks the lock file when HAVE_LOCK is true; temp-file cleanup stays unconditional. New test: tests/test_save_session_lock_ownership.py drives three real subprocesses (holder, skipper, racer) and asserts the lock file survives the skipper's exit and still names the holder.
This was referenced Jul 26, 2026
This was referenced Aug 5, 2026
fdaviddpt
added a commit
that referenced
this pull request
Aug 5, 2026
test_skip_path_does_not_delete_holders_lock_file asserted that P1's save.lock survived P2's skip, on the stated grounds that P1 was still running — and nothing established that. P1 held the lock for a fixed STUB_EXTRACT_SLEEP=3.0; when that window closed before P2 finished, P1 released the lock correctly and the assertion fired accusing P2 of a teardown it never performed. One leg of twelve, macos-3.11. The test had two outcomes where it needed three. An absent lock has an innocent reading and a guilty one that produce the identical filesystem state; only P1's liveness separates them. "P1 had already exited" is neither a pass nor a failure, and now reports itself as inconclusive via pytest.skip(... COVERAGE LOST ...) — the #293 idiom from tests/test_lock_primitive.py. The timing dependency is removed rather than widened. P1 now blocks in its stub extract on a release file the test creates, so its hold covers P2 and P3 by construction; the stub keeps a 60s cap. All environments are built before P1 starts — each _make_env rewrote the same ten scripts and the session jsonl into the tree P1 was executing from, which was both a hazard and most of the work that had to fit in the window. The gate is consulted only on the ambiguous branch: a lock still present is unambiguous. A bare poll() would not have sufficed there either — it reads None throughout save-session.sh's EXIT trap — so liveness is believed only after a 2s grace, paid solely when about to fail or skip. The P3 half rested on the identical unstated premise and got the same treatment. Both inconclusive cases are now constructed deliberately by new tests rather than waited for. Verified: reverting #168 in cleanup() (unconditional rm -rf "$LOCK_DIR") still fails with the accusation intact, so the invariant is not weakened; disabling either liveness gate turns both new tests red with exactly the #312 misattribution. No product change.
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.
The bug
scripts/save-session.shinstallstrap cleanup EXITbefore it attemptsto acquire
save.lock, andcleanup()unlinks$LOCK_FILEunconditionally:The trap is already armed when a process discovers another live PID holds
the lock and exits 0 to skip — so that exit also runs
cleanup(), whichdeletes
$LOCK_FILEeven though this process never held it. The file itjust deleted belongs to the still-running holder.
A third process racing in immediately afterward finds no lock file at all
(the noclobber write succeeds cleanly), acquires it, and now runs
concurrently with the original holder: two live
save-session.shprocesses, two Haiku calls against overlapping transcript spans, two
save-positionwrites racing each other.Why it matters
Many concurrent sessions/lanes sharing one
REMEMBER_DIRis routine(several worktree lanes, or two accounts, writing to the same project).
save.lockis the single serialization point the rest of the pipeline(extraction, prompt-building, the Haiku call, position writes) all assume
is exclusive; this bug defeats it under three or more concurrent
invocations.
Reproduction
Three real processes, same
REMEMBER_DIR:save.lock, and is mid-extraction (slow).skipping", and exits 0.
cleanup(), which deletessave.lock— even though P1 (PID recorded in the file) is still running.
and now runs concurrently with P1.
The fix
A
HAVE_LOCKflag,falseby default, set totrueon both pathsthat actually take ownership of the lock — the fresh
noclobberwrite,and the stale-lock takeover (dead PID) — and never set on the skip path.
cleanup()only unlinks$LOCK_FILEwhenHAVE_LOCKis true. Temp-filecleanup (
${CLEANUP_FILES[@]}) stays unconditional — only the lock file'sremoval is now ownership-gated.
Test evidence
New file:
tests/test_save_session_lock_ownership.py, three realsubprocesses.
Before (unpatched
main, HEADd9480ec):After (patched):
Regression suite
This machine has no
jq, somain's baseline already has ~38jq-dependent failures unrelated to this change (see PR "jq-free config
fallback" for detail). Judging by delta, not absolute count:
main, HEADd9480ec):38 failed, 625 passed, 58 skipped38 failed, 626 passed, 58 skipped(identical pre-existingfailures, +1 pass for this patch's new test)
Backward compatibility
None needed — single-process behavior (the overwhelming common case) is
unchanged; only the three-or-more-concurrent-process interleaving
changes, and only by no longer corrupting it.
This PR is independent of the other four bug-fix PRs I'm submitting
alongside it and can be reviewed/merged in any order.