Skip to content

bug: test_a13_exact_cutoff_row_is_not_expired is wall-clock flaky (~3.3%), reddens regression diff on unrelated PRs #1909

Closed

Description

@dolho

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 / 150

Suggested fix

Freeze the clock the assertion depends on rather than widening a tolerance. Either:

  1. Pin the time source expire_stale_queued reads for the duration of the test (freezegun, or monkeypatching the accessor), so setup and query observe the same instant; or
  2. Have the test compute exact from the threshold the code will actually use rather than re-deriving it from now — 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

  1. added
    priority-p2
    type-bug
    theme-infrastructure
    on Jul 31, 2026
  2. added
    status-ready
    complexity-low
    on Aug 3, 2026
  3. removed
    status-ready
    on Aug 3, 2026
  4. webmixgamer commented on Aug 3, 2026

    @webmixgamer
    Contributor

    /claim

  5. added
    status-in-progress
    on Aug 3, 2026
  6. github-actions commented on Aug 3, 2026

    @github-actions

    Claimed by @webmixgamer. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/1909-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #1909
  7. added 3 commits that reference this issue on Aug 3, 2026
    766b1d4
    ccf8ad1
    9d8aacd
  8. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 3, 2026
  9. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p2theme-infrastructuretype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions