Repository navigation
Measure overlap, not acquisitions, in the lock concurrency test - #307
Merged
Merged
Conversation
#293) test_at_most_one_winner_per_round counted how many processes acquired a lock in a round and asserted the count was 1. That is not a property of lock_acquire: its winner released after 150ms, so a contender the OS did not schedule until after that release took the free lock by plain mkdir — a legitimate sequential acquisition — and the harness tallied it as a concurrent winner. Reproduced on stock macOS bash 3.2.57, and worse than the issue reported: 4 contenders at a 0.4s stagger, 4 winners, zero overlap. Replaced with an overlap assertion: contenders write enter/exit to one shared append log and the replay fails if anyone was inside while somebody else still was. No clock, deliberately. On stock macOS EPOCHREALTIME is unset and date +%s%N prints a literal N, so the best portable clock is whole seconds — coarser than the intervals being measured, which would have made "disjoint" mean "indistinguishable": #293's defect in a new costume. Same hazard for file mtimes via -nt (#303). O_APPEND ordering needs no resolution and is pinned to the lock by causality, so a false overlap is unreachable and a real one is visible at any duration. Teeth demonstrated, not claimed: red at every N against a no-op lock and against a check-then-act lock. A first cut of the regression pin asserted holds == n on the staggered round and was itself scheduler-dependent — red at 3/4 under load, once in six runs. That is the same bug inside the test written to retire it, so the count is no longer asserted; the structural half moved to a sequential, scheduler-free test and the concurrent half skips loudly when it produced no re-acquisition to misread. Also corrects two comments in lib-lock.sh that no longer state the truth: the harness description in the header, and the claim that mv is what makes the adopt-marker clear single-winner. It is not — rmdir is equally single-winner there, and even rm -rf leaves 0 doubled adoptions over 60 rounds at 8-way contention, because the noclobber pid write is the gate. The mv -> rmdir mutant the issue asks to pin is therefore an equivalent mutant, not missing coverage. 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.
Closes #293.
test_at_most_one_winner_per_roundcounted lock acquisitions per round and asserted the count was 1. A contender the OS does not schedule until after the holder released takes the free lock by plainmkdir— a legitimate sequential acquisition — and the harness tallied it as a concurrent winner.Reproduced first, and it is worse than filed
The issue reports 2 winners at a 0.4s stagger. On stock macOS bash 3.2.57, four contenders started 0.4s apart, each holding 0.15s at timeout 0: 4 winners, zero overlap. Not two — all of them. Every contender finds the lock free because the previous holder released 250ms earlier.
The design in the issue does not work on the platform that filed it
#293 proposes recording enter/exit timestamps and asserting the intervals are pairwise disjoint. Measured on this machine:
EPOCHREALTIMEis unset, anddate +%s%Nprints1785838792N— a literalN. The best portable clock on stock macOS is whole seconds, which is whylib-lock.sh's own recorder already degrades to exactly that there.-ntis whole-second too (#303), so file mtimes are no escape.Whole seconds is coarser than every interval being measured, so a disjointness check would have decayed into "indistinguishable" — this issue's own defect in a new costume, on the plugin author's platform.
So there is no clock in the test at all. Each contender appends
enter/exitto one shared log; the replay fails if anyone entered while somebody was still inside.Ordering needs no resolution.
O_APPENDwrites land in syscall order, and that order is pinned to the lock by causality rather than the scheduler:enteris written afterlock_acquirereturns,exitbeforelock_releaseis called. The recorded interval is therefore contained in the true hold interval and never wider. A false overlap is unreachable; a real overlap is visible at any duration, including one shorter than any clock on this platform can name. Strictly more sensitive than the timestamp design, not a compromise.Contenders now wait rather than give up at timeout 0, so a round is N hand-offs instead of one acquisition — 10 rounds × N=10 is 100 hand-offs against the old 40 rounds' 40 acquisitions. More contention at CI parity (130s vs 117s).
Would this pass if the lock did nothing?
No. With
lock_acquire() { return 0; }:Red at every N, including N=2. A realistic check-then-act lock (
[ -d "$1" ] && return 1; mkdir "$1") is also red 4/4.And the old rule applied to the staggered round:
GREEN:
28 passed in 130.02s. Rest of the suite1405 passed, 43 skipped— the 43 are pre-existing platform skips (flock, pwsh, Git Bash), none from this change. Six consecutive stability runs of the new tests: 6/6 clean.A flake introduced and then removed, found by running it
The first regression pin asserted
holds == non the staggered round. Under load it went red at 3/4, once in six runs — because at timeout 0 a contender the OS delays into the previous holder's window correctly gives up. That is the same bug, rebuilt inside the test written to retire it.Split in two: the structural half is
test_a_sequential_reacquisition_is_not_an_overlap— one process, three acquire/release cycles, no background jobs, no sleeps, no scheduler, red under the old rule on every runner. The concurrent half asserts only the scheduler-independent property and skips loudly, naming the lost coverage, when the run produced no re-acquisition to misread.Three skips exist for the same reason: malformed event lines, too few measurable rounds, and a staggered round with fewer than two holds. None of them passes quietly.
The
mv→rmdiritem: an equivalent mutant, not missing coverageThe issue asks to pin
_lock_try_adopt's single-winner argument, noting the mutant survives. It does — and it survives correctly.rmdiris equally single-winner here: exactly one of N concurrent callers removes the directory, the rest getENOENT. The comment reasoned "a rename fails for everyone but the first" as thoughrmdirwould not. That is simply wrong.Nor is the line load-bearing. Mutated to
rm -rf— which genuinely does succeed for every caller — the adopt tests still pass, and a direct stress at 8-way contention over 60 rounds gives 0 doubled adoptions, 60/60 rounds won, identical to the original. The real gate is thenoclobberpid write below it, exactly as that comment claims.Pinning it would require first making it load-bearing. The comment is corrected instead, because "no test covers this line" and "this line has nothing to cover" are indistinguishable in a mutation report. What
mvactually buys — found while disproving the stated reason — is that it clears a marker directory that is not empty, wherermdirwould fail and wedge the lock it exists to unwedge.Also corrected
lib-lock.sh:16described the harness as holding the critical section for 0.1s and counting winners. Neither is true after this change, and a reader would have taken it as current. Marked historical and pointed at the new mechanism.Docs
CHANGELOG.mdunder[Unreleased] / Changed, citing #293, #291, #303. README is not involved — it discussessave.locksemantics but never this test file.