Skip to content

Make three timing-dependent unit tests deterministic - #1237

Merged
GLinnik21 merged 4 commits into
mainfrom
fix/check-timing-flakes
Nov 4, 2026
Merged

GLinnik21 merged 4 commits into
mainfrom
fix/check-timing-flakes

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Three unit tests that kept make check red on main in code no PR touches. Each had a cause that can be named; none is a production bug, and all changes are test-only (the observe seam in ff_acquisition.rs is cfg(any(test, feature = "test-support"))).

1. ff::hls_reserve_tests::a_pause_projection_spends_neither_reserve_nor_transport_liveness

Cause: a wall-clock bound shorter than the scheduler can promise. The test built a 50 ms TransportWatchdog (wall-clock inactivity by design), slept 2 ms and expected the watchdog not to be due. Any stall of 50 ms between creating it and observe_hls_deadline reading Instant::now() makes transport_due true and the verdict HLS transport stalled. Every writer of playpos_ns/TX.paused in the module already holds testlock::serial(); the playhead clock is not wall-based, so nothing else is involved.

Fix. The test's subject is the playhead-funded reserve, so the three tests that assert a live watchdog now build it with LIVENESS_NEVER_DUE (one hour). The body moved into pause_projection_verdict_after(stall), and a second test runs it with a 120 ms stall.

  • Red (stall twin against the 50 ms budget, 3 of 3 runs): a pause projection classified a boundary: BlockingWake { owner: Reserve, reserve_spent: false, transport_due: true }. reserve_spent: false rules out a stray playpos_ns writer.
  • Green after the fix: 3 of 3.
  • Natural rate before: 1 of 30 module runs (the failing run's module took 18.13 s, a swap stall: test result: FAILED. 26 passed; 1 failed; ... finished in 18.13s), then 0 of 150 once the machine was quiet. After: 0 of 150 and 0 of 100 ambient, 0 of 100 under 40 CPU hogs. test result: ok. 28 passed; 0 failed for the module.

2. ff::stall_guard_tests::a_hold_accepted_during_a_blocked_body_read_ends_that_read

Cause: an ordering assumption in the observe seam. The test waits for the runtime's second body Continue on a channel, then publishes the hold from a second thread and asserts the first answer after it is Stop. The reading thread blocks for one CHECK_SLICE (100 ms) after that answer and then asks again, so a publisher descheduled for longer let a recheck that still answered Continue land first. Failure observed locally: left: Some((Body, Continue { .. })) right: Some((Body, Stop)). CPU hogs alone do not produce a 100 ms deschedule: the test alone was 0 of 60 idle and 0 of 60 under 20 hogs; it failed in one 83 s module run (three tests red, 1 of 25 runs).

Fix. observe::on_nth_continue(phase, n, action) runs the change on the watched thread itself, between the nth Continue and the next ask, and report runs it before the answer is delivered, so a receiver woken by that answer finds the change made. blocked_body_read publishes the hold that way, and so does demux_against_when's AfterContinue, which had the same shape. The pr-reviewer found those four callers: the open-leg short-deadline pair (60 ms window) and the two NotReady waits (250 ms retry). The open pair is the other two tests that failed in that 83 s run (PrimeExpired instead of StallAbort; "the hold was requested").

  • Red (new a_stalled_observer_cannot_reorder_the_hold_against_the_runtimes_rechecks, publisher stalled 3 slices, 3 of 3 runs): the same Continue vs Stop assertion.
  • Green after: 3 of 3. Module test result: ok. 35 passed; 0 failed; 0 of 40 ambient, 0 of 30 under 40 hogs.

Not fixed. The 60 ms short-deadline tests (two body, two open) still arm a wall deadline before the read or open starts, so a stall of 60 ms inside the reading thread itself can still beat them. That is those tests' design (a deadline shorter than CHECK_SLICE); removing it needs an injectable clock in TransportWatchdog/ReserveDeadlineState, which this PR does not add.

3. plex::session::persistence::db8_policy_tests::arm_candidate_assembly_puts_the_canonical_wrapper_first

Cause: an unlocked reader of a process global. The test calls path() twice (inside insert_arm_canonical_wrapper and in the assert) without testlock::serial(). path() reads the process-global persistent state root, which about a dozen tests in the binary redirect under that lock, so the reads can straddle a redirect. I checked every redirect writer in plx_plex (async_persistence, migration_tests, prefs_characterisation_tests, session_cache_tests, session_persistence_tests, persistence): all hold the lock, so the unlocked reader is the only gap.

Fix. The test takes serial().

  • Not reproduced at its natural rate: 0 of 30 full plx_plex runs (768 tests each), and 0 of 20 with a 10 ms sleep placed in the window at 16 test threads.
  • Mechanism demonstrated with a scratch harness (not committed): a lock-respecting writer toggling the root while the old unlocked body ran 20000 times saw 56, 77 and 64 mismatches in three runs. The same body under serial() saw 0 of 20000 in each.
  • Final tree: test result: ok. 768 passed; 0 failed for plx_plex.

Verification and what was not run

  • Ran on the committed tree: make test-hostsim C=plx_media T=stall_guard_tests (35 passed), the final plx_media hostsim binary looped directly as above, and make test-crate C=plx_plex (768 passed). Loops used a hog script with an EXIT trap; ps showed no survivors.
  • Not run: make check (the Mac was shared and swapping; CI is the gate), clippy/lint, make check-nodef (no code outside cfg(test)/test-support changed).
  • The full 2396-test plx_media hostsim binary was not looped: one run takes 6 minutes on this machine.

🤖 Generated with Claude Code

GLinnik21 and others added 4 commits November 5, 2026 00:17
…verrun

a_pause_projection_spends_neither_reserve_nor_transport_liveness built a
50 ms wall-clock TransportWatchdog, slept 2 ms and expected the watchdog
not to be due. Any scheduler stall of 50 ms between the two reads made
observe_hls_deadline report transport_due and the test fail with
"HLS transport stalled"; on a loaded or swapping machine that is routine.

The test's subject is the playhead-funded reserve, not liveness, so the
three tests that assert a live watchdog now share LIVENESS_NEVER_DUE
(an hour). The body moves into a helper taking the stall, and a second
test runs it with a 120 ms stall: it fails against the 50 ms budget on
every run (transport_due: true) and passes now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
a_hold_accepted_during_a_blocked_body_read_ends_that_read waits for the
runtime's second body Continue on a channel, then publishes the hold from
a second thread and asserts the first answer after it is Stop. The
reading thread is blocked for one CHECK_SLICE (100 ms) after that answer
and then asks again, so a publisher descheduled for longer let a recheck
that still answered Continue land first (observed: left Continue, right
Stop). The sibling short-deadline reads have the same window, 60 ms wide.

observe gains on_nth_continue, which runs the change on the watched
thread itself between the nth Continue and that thread's next ask, and
blocked_body_read uses it for the hold. A new test stalls the publisher
for three slices: it failed on every run before and passes now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
arm_candidate_assembly_puts_the_canonical_wrapper_first calls path()
twice, once inside insert_arm_canonical_wrapper and once in the assert,
without plx_base::testlock::serial(). path() reads the process-global
persistent state root, which every redirecting test in the binary moves
under that lock, so the two reads can straddle a redirect and disagree.

A scratch harness with a lock-respecting writer toggling the root saw
56-77 mismatches per 20000 unlocked reads and none per 20000 locked ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
demux_against_when's AfterContinue still fired from a second thread that
had taken the runtime's answer off the channel, the window the previous
commit closed for blocked body reads. The open and NotReady-wait tests
(a_hold_before_an_opens_short_deadline_settles_as_a_stall_abort,
at_the_floor_an_opens_short_deadline_keeps_its_classification and the
two NotReady waits) share it: a publisher stalled past their 60 ms
deadline or the 250 ms retry wait let the world move on before the
hold existed. Two of them failed together in one stalled local run.

AfterContinue now goes through observe::on_nth_continue, and report runs
the action before it delivers the answer, so a receiver woken by that
answer, the publisher's after_hold included, finds the change made.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@GLinnik21
GLinnik21 merged commit 95c5263 into main Nov 4, 2026
20 checks passed
@GLinnik21
GLinnik21 deleted the fix/check-timing-flakes branch November 4, 2026 21:59
GLinnik21 added a commit that referenced this pull request Nov 4, 2026
Tree of the integration branch after merging main (through #1237), the
W3c-2 move, the playurl offer-gate fix and the CI decode fixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant