Repository navigation
fix(tests): assert the adopt race on _lock_try_adopt, not on lock_acquire (#291) - #292
Merged
Merged
Conversation
…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>
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.
Investigation of #291. The lock is not changed.
scripts/lib-lock.shis byte-identical tomain. 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:
One winner adopts. Every later winner steals.
The window, exactly
There is no window between "clear the stranded marker" and "take the lock". The
mvatscripts/lib-lock.sh:216is 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 getsENOENTregardless of where it was aiming. The second winners never came through_lock_try_adoptat all.They came through
_lock_try_steal, and they were right to. The test's contenders are: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_acquirewhen 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 forsleep 0.15before 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 -0still finds the winner.Red, then green
lock_acquire)_lock_try_adopt){'12': 2})The contenders now call
_lock_try_adoptdirectly — 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
mvtormdirleaves 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_roundhas the same latent shape with a wider tolerance: its winner releases after 150 ms, so a contender scheduled later than that wins by plainmkdirand 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
ERRORintests/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 doestest_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 onmain— CI on this PR is the cheaper answer.Docs
CHANGELOG.mdunder Unreleased → Fixed, leading with the fact that the lock is unchanged and why.Closes #291