Skip to content

3.x.x: make the landing_metrics lost-commit deadline and api_database migration tests deterministic (#528, #532) - #537

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flakes-528-532
Sep 28, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flakes-528-532

Conversation

@djh58

@djh58 djh58 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Makes two recurring prism-native-postgres flakes deterministic. Test-only: no production code, setting or lock timeout changes, and no assertion is removed or weakened.

#528 landing_metrics::lost_commit_reply_timeouts_are_not_revision_work_failures

Symptom. Error: Stratum closed before a response (CI run 36449857065, shard 0, on #524, which does not touch this binary).

Root cause. The test holds a session's initial-job build at admission (a zero-permit semaphore) until its initial_job_timeout_seconds of 0.05 s. It then adds a permit, sends mining.get_health and waits on the same session for a mining.notify. The listener's session loop closes a session that still has no job on the first one-second timer tick after initial_job_timeout_seconds has passed (stratum.rs, the timer.tick() arm). This is intended product behaviour: a session that got no work by its deadline is dropped. So the permit, the health reply, the retried build and its persist all had to finish within about one second of connecting. The build and the persist are each bounded by the same 0.05 s, too. Unloaded, the notify arrives about 70 ms after connecting. On a busy 2-vCPU runner it sometimes arrives after the tick, and the test reads EOF.

The sibling actual_build_deadline_counts_once_and_later_notify_is_degraded has the same shape and fails the same way (below). A retried build that overruns 0.05 s also turns its TIMEOUTS == 1 check into a flake. Both are fixed here.

Fix. The delivery that follows the deadline now comes from deliver(&f.a): the file's existing helper, a listener with the default deadlines on the same frontend. The deadline itself is still a real initial-job build held at admission on the 0.05 s listener. The landing metrics under test (PENDING, UNKNOWN, TIMEOUTS and the published/degraded/superseded results) are frontend-wide, not per session, so every assertion after the delivery still checks the same state. deliver also asserts that each delivered job carries the ledger's current payout revision. What the tests no longer show is that the held session itself receives work after its deadline. The listener closes that session by design. A session held at admission that gets its job once released is covered by stratum_protocol::delivery_stats_follow_admission_progress_generation_and_disconnect.

Evidence (every run pins the test binary and the private PostgreSQL 16 (fsync on) to the same two cores):

run condition runs failures
lost_commit… alone, before 2 CPU burners on the two cores 100 0
both tests, before 2 burners + a CREATE/DROP DATABASE loop on the same server 100 6, all Stratum closed before a response (4 lost_commit…, 2 actual_build…)
both tests, after same 100 0
injected 1.1 s pause before the release, before unloaded 1 + 1 2 (both tests, Stratum closed before a response)
same pause before deliver, after unloaded 1 + 1 0

The tests still bite. They were run against two product mutants in metrics/landing.rs:

  • A: deadline_hit always attributes. lost_commit… fails with a deadline hit while only unknown tracking remained was counted as a revision-work failure.
  • B: a deadline hit never marks the block degraded. actual_build… fails at count(&f.a.metrics, "degraded") == 1. (0.0 vs 1.0), the check after the new delivery.

#532 api_database: concurrent schema migrations hit the 5 s lock timeout

Symptom. canceling statement due to lock timeout at the Ledger::connect(.., true) unwrap in one test, when the file's two tests run concurrently (PR #531 run 36454542958, and a 3.x.x push, run 36167606027).

Contended object. Both tests share one database, each in its own schema. A pg_locks poller recorded ungranted locks with their holders while the two tests overlapped. Three database-wide waits showed up:

waiter lock holder subject to the 5 s lock_timeout?
migrate_schema's pg_advisory_xact_lock($1) advisory MIGRATION_LOCK 0x505249534d000001, a single database-wide key the other test's migration transaction, for its whole length yes (ledger connection)
the test's own load of 001_share_ledger.sql advisory hashtext('qbit_audit_publication_sequence_migration') the other test's 001 load no (plain test pool)
the online runner's CREATE INDEX CONCURRENTLY virtualxid the other test's open migration transaction no (the runner sets lock_timeout 0)

Only the first can time out. The second test's migration waits for the whole of the first. With two CPU burners on the two cores, that wait was 1.65–2.75 s (median 2.17 s; 26 waits over 25 measured runs). A full shard on a 2-vCPU runner pushes it past 5 s.

Fix. A static tokio::sync::Mutex in the test file serialises the two tests' Ledger::connect(.., true) calls, and only those. The production PRISM_DATABASE_LOCK_TIMEOUT_MS default is unchanged. (CI runs test binaries one at a time, so no other binary migrates into this database concurrently.)

Evidence:

binary condition runs failures MIGRATION_LOCK waits seen
before 2 burners 100 0 every measured run waited (see above)
before 2 burners + database churn 62 0 —
before PRISM_DATABASE_LOCK_TIMEOUT_MS=300 (amplifier, test process only) 10 10, canceling statement due to lock timeout —
after same amplifier 10 0 —
after 2 burners 107 0 0

CI runtime

No measurable change. Both binaries were run whole, pinned to two cores, twice before and twice after the fix:

binary before after
landing_metrics (41 tests) 38.7 s / 37.6 s 37.5 s / 38.0 s
api_database (2 tests) 4.1 s / 4.5 s 4.1 s / 4.5 s

The serialised migration costs nothing, because the second migration already waited for the first.

Checks

  • Both binaries pass whole, and their gate manifests list every gated test.
  • cargo clippy --locked -p qbit-prism-server --test landing_metrics --test api_database -- -D warnings: clean.
  • cargo fmt --all --check: clean.
  • test/prism-gated-tests.txt is unchanged, because no test id changed.
  • Not run locally: the rest of the workspace suite.

Closes #528
Closes #532

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

… migration tests deterministic (#528, #532)

landing_metrics (#528): lost_commit_reply_timeouts_are_not_revision_work_failures
and its sibling actual_build_deadline_counts_once_and_later_notify_is_degraded
hold a session's initial-job build at admission until its 0.05 s deadline,
then release the admission permit and wait for that session's notify. The
listener closes a session that still has no job on its first one-second
timer tick past initial_job_timeout_seconds, so the release, the retried
build and its persist (each bounded by the same 0.05 s) had to finish within
about a second of connecting. On a loaded 2-vCPU runner they did not, and
the test read "Stratum closed before a response". The later delivery now
comes from deliver(), a listener with the default deadlines, on the same
frontend; every metric assertion is unchanged.

api_database (#532): both tests migrate their own schema in one shared
database. Ledger::connect(.., true) takes the migration advisory lock, whose
key (0x505249534d000001) is database-wide, under the 5 s lock timeout, so
the second migration waited for the whole of the first and a loaded runner
pushed that past 5 s. The tests now run their Ledger::connect calls one at a
time behind a static tokio mutex. The production lock timeout is unchanged.

Test-only. Closes #528. Closes #532.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58
djh58 requested a review from kiwidream September 28, 2026 19:00
@djh58

djh58 commented Sep 28, 2026

Copy link
Copy Markdown
Member Author

Review ledger (#528, #532)

This PR changes tests only, so under the current policy it got a proportionate check rather than a full deep review: one /code-review medium pass over the diff, the 2-core stress evidence in the PR body, and product mutants.

Review findings

# Finding Disposition
— /code-review medium: no findings. —
1 Coverage note, not a defect: actual_build_deadline_counts_once_and_later_notify_is_degraded no longer shows that the held session itself receives a notify after its deadline, because the later notify now comes from another session. Accepted and documented in the PR body. The listener closes that session by design once it has no job past the deadline. stratum_protocol::delivery_stats_follow_admission_progress_generation_and_disconnect covers a session held at admission that gets its job once released.

The review also checked the following and found them sound:

  • The static tokio::sync::Mutex is safe across the two tests' separate runtimes, and a panic while it is held unwinds and releases it.
  • CI runs test binaries one at a time, so a lock inside this binary is enough.
  • The held session cannot retry and push TIMEOUTS to 2: its next pass comes from the one-second tick, which closes it first.
  • The shutdown wait cannot hang.

Bots: [code]smith skipped. No other review comments.

Evidence summary (details in the PR body)

CI

All required checks green on the PR head.

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