Skip to content

fix: a fallback that spoke over its own answer, and the marker it stranded (#198) - #199

Merged
fdaviddpt merged 1 commit into
mainfrom
max/lock-dir-age-and-stranded-adopt-marker
Jul 27, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
max/lock-dir-age-and-stranded-adopt-marker

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #198. Reported by @bonyohana, with the measurement that made it undeniable.

The age bug

_lock_dir_age was written as "try BSD, fall back to GNU" in a single command substitution. The exit codes behaved. The 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 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 returned 0 — "fresh" — for a directory of any age.

Every adoption check downstream then read [ 0 -lt 30 ] and refused. So _lock_try_adopt could 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_forever set _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 stat on PATH with 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 adopt and writing the pid leaves no pid, no claim, and adopt/ present: _lock_try_steal returns early with nothing to judge and every later _lock_try_adopt fails at mkdir. 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 noclobber makes the pid redirect an O_EXCL create. 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.sh had LINES=$(… | grep -c '^\[' 2>/dev/null || echo 0). grep -c prints 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:265 and log.sh:298 both put the native flag first (verified against real gstat/gdate — the failing probe prints nothing before exiting), lib-memory-dir.sh redirects to a file rather than capturing, and no second threshold-neutering test exists in tests/.

Tests

tests/test_lock_primitive.py, 7 new cases (4 parametrized over gnu/bsd stat): real age reported under either stat; 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.

…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
fdaviddpt force-pushed the max/lock-dir-age-and-stranded-adopt-marker branch from 4afc2d3 to 19891a9 Compare July 27, 2026 09:02
@fdaviddpt
fdaviddpt merged commit d046440 into main Jul 27, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the max/lock-dir-age-and-stranded-adopt-marker branch July 27, 2026 10:10
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.

_lock_dir_age() always returns 0 on GNU/Linux — _lock_try_adopt can never fire, so the pid-less-orphan save outage is still live

2 participants