Repository navigation
fix: a fallback that spoke over its own answer, and the marker it stranded (#198) - #199
Merged
Merged
Conversation
…anded (#198) _lock_dir_age was "try BSD, fall back to GNU" in one command substitution. The exit codes behaved; stdout did not. On GNU, `stat -f` is filesystem status, so `%m` is read as a filename that does not exist: the command exits 1 and the fallback runs, but the real path has already printed its filesystem block to stdout. The capture concatenated that block with the correct mtime, the digits-only guard rejected the result, and the function reported 0 — "fresh" — for a directory of any age. Every adoption check downstream read `[ 0 -lt 30 ]` and refused, so no pid-less orphan was ever adopted on Linux and the permanent save outage adoption exists to prevent was still live. The probes are captured separately now, GNU-native flag first. The suite was green over it because the adoption test set _LOCK_ADOPT_AFTER=0, making the guarded comparison false and letting the test pass through the broken age rather than exercising it. The new tests shim `stat` on PATH with both platforms' semantics, so the Linux defect reproduces on macOS, and adoption is asserted at the shipped threshold against a directory of known age. Secondary defect from the same report: an adopter killed between `mkdir adopt` and writing the pid strands the marker, and every later adopter then fails at mkdir — the same permanent outage, one level up. A marker aged past the threshold is now cleared by rename and NOT recreated: a first cut that recreated it let an adopter which lost the clearing race displace the winner's fresh marker, measured at 4 double-wins in 40 rounds. The pid write is separately guarded with noclobber (O_EXCL). That is not what closed the double-win — dropping the recreate did, and removing noclobber leaves the concurrent test green over 300 rounds at 8-way contention. What it closes is the adopter-versus-steal interleaving: _lock_try_steal's rename leaves `pid` briefly absent, the adopter's entry guard reads that as "no holder", and the adopter would otherwise clobber the pid the steal winner just wrote. That has its own deterministic test, which injects a pid in exactly that window and is verified red without the guard. Also: only the process that created the adopt marker removes it. An unguarded rmdir on the losing path deletes the winner's live marker — reproducible, and harmless only by accident of the pid write having already settled the lock. And `LINES=$(… | grep -c '^\[' || echo 0)` in run-tests.sh, found by auditing for the reported defect's shape: grep -c prints its count and still exits 1 when that count is zero, so both branches fired and the value was "0\n0". Full suite 875 passed, 41 skipped. Reported by @bonyohana, with the measurement that made it undeniable. Co-Authored-By: Max <noreply>
fdaviddpt
force-pushed
the
max/lock-dir-age-and-stranded-adopt-marker
branch
from
July 27, 2026 09:02
4afc2d3 to
19891a9
Compare
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.
Closes #198. Reported by @bonyohana, with the measurement that made it undeniable.
The age bug
_lock_dir_agewas written as "try BSD, fall back to GNU" in a single command substitution. The exit codes behaved. The stdout did not: on GNU,stat -fis filesystem status, so%mis read as a filename that does not exist — the command exits 1 and the fallback duly runs, but the real path has already printed its filesystem block to stdout. The capture concatenated that block with the correct mtime, the digits-only guard rejected the result, and the function returned0— "fresh" — for a directory of any age.Every adoption check downstream then read
[ 0 -lt 30 ]and refused. So_lock_try_adoptcould never fire on Linux at the shipped threshold, and the permanent, silent save outage it exists to prevent was still live on what is probably most installs.The probes are captured separately now, GNU-native flag first so the noisy call is skipped on the more common platform.
Why the suite was green over it
test_a_pidless_orphan_is_adopted_not_blocked_foreverset_LOCK_ADOPT_AFTER=0, which makes the guarded comparison[ 0 -lt 0 ]false — so the test passed through the broken age rather than exercising it. Green on a code path that cannot work as shipped.The new tests shim
statonPATHwith both platforms' semantics, so the Linux defect reproduces on macOS and neither platform's answer depends on which runner happened to execute it. Adoption is asserted at the shipped threshold against a directory of known age, not with the threshold zeroed.The stranded marker
Same report, secondary. An adopter killed between
mkdir adoptand writing the pid leaves no pid, no claim, andadopt/present:_lock_try_stealreturns early with nothing to judge and every later_lock_try_adoptfails atmkdir. The same permanent outage adoption exists to answer, one level up.The first fix for this was wrong and the tests caught it. Clearing the marker cannot be made atomic with the check that judged it stranded: an adopter that loses the clearing race has already read the marker as debris, so it goes on to displace the fresh marker the winner is holding, and both adopt. Measured at 4 double-wins in 40 rounds.
So the marker is now advisory and the pid write is the single-winner test — cleared by rename (fails for everyone but the first), then
set -o noclobbermakes the pid redirect anO_EXCLcreate. Exactly one adopter can install a pid however the clearing race turned out, and adoption only runs when there is no pid, so refusing to overwrite one is never wrong there.Found by auditing for the shape
scripts/run-tests.shhadLINES=$(… | grep -c '^\[' 2>/dev/null || echo 0).grep -cprints its count and still exits 1 when that count is zero, so both branches fired and the value was"0\n0"— the same stdout-contamination mechanism. Consequence is one garbled figure in a self-test log line; nothing branches on it.The rest of the repo was swept for the same two shapes and is clean:
log.sh:265andlog.sh:298both put the native flag first (verified against realgstat/gdate— the failing probe prints nothing before exiting),lib-memory-dir.shredirects to a file rather than capturing, and no second threshold-neutering test exists intests/.Tests
tests/test_lock_primitive.py, 7 new cases (4 parametrized over gnu/bsdstat): real age reported under eitherstat; adoption at the shipped threshold; a stranded marker does not wedge the lock; a fresh marker is left alone; and 40 rounds of 4 concurrent adopters racing on stranded debris asserting exactly one winner per round.That last assertion started as
len(wins) <= ROUNDS, which zero winners would satisfy — the same shape as the bug being fixed. It counts per round now, and asserts every round was won.Written first and run red before the fix. Full suite green after: 874 passed, 41 skipped.
Version bumped to 0.8.9 with a CHANGELOG entry, per #133.