Repository navigation
Conversation
#168 stopped a non-owner deleting the lock, but left the other half open. The takeover branch was a plain overwrite: log "lock" "stale lock (PID $LOCK_PID dead), taking over" echo $$ > "$LOCK_FILE" HAVE_LOCK=true Two processes that both observe the same dead PID both take over, both set HAVE_LOCK, and both proceed — two concurrent saves, two Haiku calls, two position writes. That is the corruption #168's description names, reached by the stale path instead of the contention path. Reproduced with two real processes against a pre-seeded dead-PID lock in 4 of 5 runs. Now: unlink, re-create under noclobber so only one contender can make the file, then re-read and stand down unless it holds our own PID. Whoever writes last wins and everyone else exits. Never two holders. Not a true compare-and-swap — between the verify and the first real work another takeover could still clobber the file. That window is microseconds and confined to this branch. mkdir would give real atomic-create semantics and is the honest long-term fix; it changes the lock representation, so it is not this commit. Also guards `LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)` with `|| true`. It is a plain top-level assignment, so under set -e its status is cat's, and the lock file can be gone by the time it is read. Pre-existing, but the new `rm -f` makes the window easy to hit: the loser exited 1 with an ERR-trap log in ~2 of 12 runs where it should have skipped cleanly with 0. Never let a second process through — HAVE_LOCK is not yet set, so cleanup() is a no-op — but it turned a normal outcome into a logged failure. Test spawns two real processes against a dead-PID lock, 8 iterations, and asserts at most one runs extract/call-haiku. Reverted, it fails at iteration 0 with both processes extracting. 5 consecutive runs green (40 iterations) with strict exit-code assertions. Co-Authored-By: Max <noreply>
|
Correcting this PR: the central claim in the description is false. Adversarial review disproved it empirically. I wrote "whoever writes last wins and everyone else exits — never two holders." That holds for exactly two contenders, which is all my test exercises. Beyond that it breaks:
The verify step ( Worse, and I missed it entirely: And Closing this rather than patching it. The approach is wrong at the root: The real fix uses primitives that actually are atomic:
That changes the lock representation, so it belongs in its own change with its own tests at N=2, 4, 8 and 10 — the concurrency levels that exposed this. The Thanks to the review for pushing past the two-process case. I would have shipped this. |
Follow-up to #168, which closed one half of the lock-ownership problem and left the other open.
The bug
The stale-lock takeover branch was a plain overwrite with no compare-and-swap:
Two processes that both observe the same dead PID both take over, both set
HAVE_LOCK, and both proceed — two concurrent saves, two Haiku calls, two position writes. That is the corruption #168's own description names, reached by the stale path rather than the contention path.Reproduced with two real processes against a pre-seeded dead-PID lock in 4 of 5 runs.
The fix
Unlink, re-create under
noclobberso only one contender can create the file (the loser skips this cycle), then re-read and stand down unless it holds our own PID. Whoever writes last wins, everyone else exits. Never two holders.Not a true CAS, and the code says so. Between the verify and the first real work another takeover could still clobber the file — microseconds wide, confined to this branch.
mkdirhas real atomic-create semantics and is the honest long-term fix, but it changes the lock representation, so it is not this PR. Worth its own issue.Second defect, found while testing
LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)is a plain top-level assignment, so underset -eits exit status iscat's — and the lock file can be gone by the time it is read. Pre-existing, but this PR's ownrm -fmakes the window easy to hit: the loser exited 1 with an ERR-trap log in ~2 of 12 runs where it should have skipped cleanly with 0.It never let a second process through (
HAVE_LOCKis not yet set, socleanup()is a no-op on the lock), but it turned a normal outcome into a logged failure. Guarded with|| true.Verification
save-session.shprocesses against a dead-PID lock, 8 iterations per run, asserting at most one runsextract/call-haiku.both processes proceeded past the stale lock takeover (extract ran 2 times), with both call logs showing the full pipeline.|| trueguard.Checked and cleared
[ "${REMEMBER_DEBUG:-1}" = "1" ] && log ...immediately beforeexit 0looked like aset -ehazard. It is not — bash exempts a short-circuited final member of an&&list. Verified empirically for all three occurrences withREMEMBER_DEBUG=0.Co-Authored-By: Max