Skip to content

fix(tests): assert the adopt race on _lock_try_adopt, not on lock_acquire (#291) - #292

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/291-adopter-race
Aug 1, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/291-adopter-race

Conversation

@fdaviddpt

@fdaviddpt fdaviddpt commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Investigation of #291. The lock is not changed. scripts/lib-lock.sh is byte-identical to main. The defect is in the test, and the two readings the issue declined to rank are now separated by measurement rather than by argument.

Reproduced, at a rate

Staggering two of the four contenders by 150–200 ms — an ordinary scheduling delay for a forked subshell on a loaded two-core runner — made the old test form fail 1 run in 3, with AssertionError: rounds with more than one winner: {'12': 2}: the same shape as CI's {'24': 2}. Unstaggered, 340 rounds under 80-way CPU oversubscription produced zero doubles, which is why the runner sees this about once in twelve runs and a laptop never does.

Wrapping the three acquisition routes and recording which one produced each win named the mechanism outright:

win route=adopt pid=77160
win route=steal pid=77161
win route=steal pid=77162
win route=steal pid=77165

One winner adopts. Every later winner steals.

The window, exactly

There is no window between "clear the stranded marker" and "take the lock". The mv at scripts/lib-lock.sh:216 is sound, and the fact that the two adopters rename to different destinations (adopt.dead.$$) changes nothing: rename(2) unlinks the source, so the second one gets ENOENT regardless of where it was aiming. The second winners never came through _lock_try_adopt at all.

They came through _lock_try_steal, and they were right to. The test's contenders are:

contend() { if lock_acquire "$1" 0; then echo "win $2" >> "$winners"; fi; }

No critical section. No lock_release. A winner writes its pid and its subshell exits immediately, leaving the lock held by a pid that no longer names a live process — which is the definition of a stale lock, and recovering it is the entire point of #182 and #198. A contender the OS did not schedule until after that exit finds it, judges it stale, wins the rename, and takes over. Correctly. The harness recorded that handover as a second winner.

"One winner per round" was never an invariant of lock_acquire when the winners do not stay alive. The sibling test at the top of the file gets away with the same shape only because its winners hold for sleep 0.15 before releasing.

Product or test — test, and here is what distinguishes them

If the lock were wrong, two processes would hold it at the same time. They never do. The second holder holds it because the first has ceased to exist. The distinguishing evidence is the route instrumentation above plus the fact that the double only ever appears once the first winner's subshell has exited — below roughly a 50 ms stagger it does not appear at all, because kill -0 still finds the winner.

Red, then green

old form (lock_acquire) new form (_lock_try_adopt)
150–200 ms stagger, loaded laptop 1 in 3 runs RED ({'12': 2}) 5/5 green under 40-way oversubscription
0.3/0.4/0.5 s stagger 4 winners in one round 1 winner

The contenders now call _lock_try_adopt directly — the operation the test's own name and docstring are about, and the one with no steal path. It never consults liveness: once a pid exists the entry guard turns every later adopter away, whether or not the adopter that wrote it is still running. Single-winner by construction rather than by timing, which is the standard the lock itself is held to.

The stagger is kept in the shipped test, so the interleaving that broke the old form is exercised on purpose rather than met once in twelve runs by accident. Cost: ~8 s. The "every round was won at all" assertion is untouched, so a build where stranded debris wedges the lock still fails rather than passing with zero winners.

A companion test, test_an_adopter_that_exits_leaves_a_lock_the_next_contender_takes_over, pins the behaviour that was mistaken for the bug. Acting on the other reading of #291 — making acquisition refuse a lock whose holder is dead — would reinstate the permanent, silent outage #198 was filed for. If that test ever goes red, the change that turned it red is the defect.

What this does not fix, said plainly

Mutating the marker clear from mv to rmdir leaves the whole file green — and did so before this change as well (verified against the original test form). So no teeth were removed here, but that line's single-winner argument is not currently pinned by any test. Noted rather than fixed; it is a separate question.

The sibling test_at_most_one_winner_per_round has the same latent shape with a wider tolerance: its winner releases after 150 ms, so a contender scheduled later than that wins by plain mkdir and is counted as a concurrent holder. Measured: a 0.4 s stagger yields 2 winners. It has not fired in CI and fixing it properly means asserting overlap rather than counting acquisitions — a redesign of the file's primary test, and a design call rather than a cleanup. Flagged here; happy to file it separately.

Suite

1288 passed, 43 skipped, 1 error in 1601 s (pytest -q, macOS).

The one error is a teardown-phase ERROR in tests/test_worktree_memory.py::TestWorktreeHandoff::test_legacy_worktree_emits_handoff_to_main_checkout, in a file this diff does not touch. It passes in isolation (12 passed), as does test_post_tool_hook_stdin_size.py, which failed in an earlier full run taken while the machine was still under the contention load from the reproduction work. I could not capture its traceback and am not claiming it is pre-existing on main — CI on this PR is the cheaper answer.

Docs

CHANGELOG.md under Unreleased → Fixed, leading with the fact that the lock is unchanged and why.

Closes #291

…uire (#291)

test_two_adopters_racing_on_stranded_debris_produce_one_winner failed once on
main with {'24': 2}. scripts/lib-lock.sh is unchanged and deliberately so: the
lock never admitted two writers.

The contenders called lock_acquire and exited immediately -- no critical
section, no release -- so a winner left a pid file naming a dead process. A
contender scheduled after that exit found a stale lock and took it over via
_lock_try_steal, which is the primitive's contract (#182, #198). The harness
counted that correct handover as a second winner.

Reproduced before changing anything: staggering two of the four contenders by
150-200ms made the old form fail 1 run in 3, same {'N': 2} shape as CI. Route
instrumentation named it -- one win via adopt, every later one via steal.

Contenders now call _lock_try_adopt, the operation the test's own name is
about, which never consults liveness: once a pid exists the entry guard turns
later adopters away. The stagger is kept so the interleaving is exercised on
purpose. 5/5 green under 40-way CPU oversubscription.

A companion test pins the takeover semantics that were mistaken for the bug,
so a future "fix" to the lock goes red on purpose.

Co-Authored-By: Max <noreply>
@fdaviddpt fdaviddpt changed the title fix(tests): assert the adopt race on _lock_try_adopt, not on a scheduling outcome (#291) fix(tests): assert the adopt race on _lock_try_adopt, not on lock_acquire (#291) Aug 1, 2026
@fdaviddpt
fdaviddpt merged commit 830c224 into main Aug 1, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/291-adopter-race branch August 1, 2026 20:20
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.

test_two_adopters_racing_on_stranded_debris_produce_one_winner failed on main — two adopters cleared the same stranded marker and both took the lock

1 participant