Skip to content

Measure overlap, not acquisitions, in the lock concurrency test - #307

Merged
fdaviddpt merged 1 commit into
mainfrom
test/293-lock-overlap-not-acquisitions
Aug 4, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
test/293-lock-overlap-not-acquisitions

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #293.

test_at_most_one_winner_per_round counted 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 plain mkdir — 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: EPOCHREALTIME is unset, and date +%s%N prints 1785838792N — a literal N. The best portable clock on stock macOS is whole seconds, which is why lib-lock.sh's own recorder already degrades to exactly that there. -nt is 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/exit to one shared log; the replay fails if anyone entered while somebody was still inside.

Ordering needs no resolution. O_APPEND writes land in syscall order, and that order is pinned to the lock by causality rather than the scheduler: enter is written after lock_acquire returns, exit before lock_release is 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; }:

E       AssertionError: 25/25 rounds at N=10 had two processes inside the critical section at once — the lock is not
        mutually exclusive. First: (0, [('2', ['1']), ('3', ['1', '2']), ('4', ['1', '2', '3']), ...])
FAILED tests/test_lock_primitive.py::test_no_two_holders_overlap[2]
FAILED tests/test_lock_primitive.py::test_no_two_holders_overlap[4]
FAILED tests/test_lock_primitive.py::test_no_two_holders_overlap[8]
FAILED tests/test_lock_primitive.py::test_no_two_holders_overlap[10]
4 failed, 1 passed, 22 deselected in 14.19s

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:

E       AssertionError: 4 processes acquired the lock in this round — the lock is not mutually exclusive
E       assert 4 <= 1

GREEN: 28 passed in 130.02s. Rest of the suite 1405 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 == n on 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 → rmdir item: an equivalent mutant, not missing coverage

The issue asks to pin _lock_try_adopt's single-winner argument, noting the mutant survives. It does — and it survives correctly.

rmdir is equally single-winner here: exactly one of N concurrent callers removes the directory, the rest get ENOENT. The comment reasoned "a rename fails for everyone but the first" as though rmdir would 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 the noclobber pid 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 mv actually buys — found while disproving the stated reason — is that it clears a marker directory that is not empty, where rmdir would fail and wedge the lock it exists to unwedge.

Also corrected

lib-lock.sh:16 described 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.md under [Unreleased] / Changed, citing #293, #291, #303. README is not involved — it discusses save.lock semantics but never this test file.

#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>
@fdaviddpt
fdaviddpt merged commit dc5669b into main Aug 4, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the test/293-lock-overlap-not-acquisitions branch August 4, 2026 12:01
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_at_most_one_winner_per_round counts acquisitions, not overlap — 2 winners at a 0.4s stagger

1 participant