Repository navigation
Description
Summary
tests/unit/test_1771c_schedules_cas_edges.py::test_a13_exact_cutoff_row_is_not_expired samples the wall clock twice and fails when the second ticks between the two samples. Measured at 5 failures in 150 consecutive runs (3.3%).
Because backend-unit-test.yml runs the suite 6 times per PR (3 seeds x 2 sides), the chance of at least one failure is 1 - 0.967^6 ≈ 18% — so roughly one PR in five gets a red regression diff with nothing wrong in the diff. Hit on #1902, whose changes are portal-token code and a Vue component.
Root cause
now = datetime.now(timezone.utc)
exact = (now - timedelta(hours=24)).strftime("%Y-%m-%dT%H:%M:%S") + ".000000Z"
older = _iso(now - timedelta(hours=24, seconds=5))
insert_execution("a13-exact", status="queued", queued_at=exact)
insert_execution("a13-older", status="queued", queued_at=older)
assert ops.expire_stale_queued(24) == 1
assert status_of("a13-exact") == "queued"exact is computed from the clock at setup. expire_stale_queued builds its own threshold from the clock at query time, via SQL strftime. When the wall second rolls over between them, the threshold advances one second, exact is now strictly older, the row is expired, expire_stale_queued returns 2, and both assertions fail.
The property under test is real and worth keeping — the ≤1s tolerance band from the Invariant-#16 format mismatch (strftime('%Y-%m-%dT%H:%M:%S') vs utc_now_iso()'s …:SS.ffffffZ). The defect is that the test derives its fixture from a clock it does not control.
Reproduction
cd tests
for i in $(seq 1 150); do
python -m pytest "unit/test_1771c_schedules_cas_edges.py::test_a13_exact_cutoff_row_is_not_expired" \
-q -p no:randomly --no-header 2>&1 | grep -q "1 passed" || fails=$((fails+1))
done
# FAILURES: 5 / 150Suggested fix
Freeze the clock the assertion depends on rather than widening a tolerance. Either:
- Pin the time source
expire_stale_queuedreads for the duration of the test (freezegun, or monkeypatching the accessor), so setup and query observe the same instant; or - Have the test compute
exactfrom the threshold the code will actually use rather than re-deriving it fromnow— read the cutoff the implementation produces, then insert a row at exactly that value.
(2) keeps the test honest about the real boundary without needing a clock library, and cannot drift from the implementation the way a second independent datetime.now() does.
What I would avoid: nudging exact by a few hundred ms to make the race unlikely. That converts a 3.3% failure into a rarer one and drops the "exactly at the cutoff" property the test exists to pin.
Note
Also worth checking the sibling cases in the same file for the same two-clock-samples shape — this is a pattern, and only the exact-boundary case is tight enough to lose the race often enough to notice.
Activity
webmixgamer commentedon Aug 3, 2026
/claim
github-actions commentedon Aug 3, 2026
Claimed by @webmixgamer. Label
status-in-progressadded.Next steps:
feature/1909-<short-slug>Fixes #1909fix(tests): de-race the a13 exact-cutoff boundary test with a single …
fix(tests): make the a13 clock guard behavioural, not tautological
fix(tests): de-race the a13 exact-cutoff boundary test with a single …
fix(canary): stop three false-positive classes drowning the harness (…
fix(canary): make E-01 lease-aware so a healthy pull turn stops pagin…