Skip to content

3.x.x: make the candidate_storm drain-cost and offer_lifecycle claim tests deterministic - #498

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flaky-tests
Sep 23, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b-flaky-tests

Conversation

@djh58

@djh58 djh58 commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Makes two recurring prism-native-postgres flakes deterministic without changing what either test asserts. Both were races against sqlx 0.8's asynchronous connection return: dropping a PoolConnection spawns a task that pings the server before the connection is idle again, and a Transaction dropped on an error path only queues its ROLLBACK for that task to flush.

candidate_storm::drained_rows_cost_the_same_statements_at_the_baseline_and_storm_cardinalities

Symptom. a pooled connection opened inside the measured window (3.x.x push on 2026-09-18, shard 3; the binary had been running 3.7 s beside the other three storm tests on the 2-vCPU runner).

Root cause. The ledger pool is built with max_connections(6) and sqlx defaults. The drain is strictly sequential, so every commit → next begin boundary races the spawned return task. The pool only ever opened two connections; when both were in return flight at a boundary, acquire found the idle queue empty, saw size < max, and opened a third whose after_connect statements (set_config, the fsync check) were counted against the row in flight. Draining warmup rows cannot settle this because the pool's size after warmup is whatever the races so far produced, never the maximum.

Evidence. A local diagnostic build printed the pool's size()/num_idle() before each row and the row's statements when the proxy accepted a new connection. Under a 2-core stress loop (two CPU hogs pinned with the test and PostgreSQL), 4 of 10 runs failed exactly this way: every window started at size=2; the extra connection opened with size=2, idle=0 (or idle=1 with the second connection's return lagging across the row), and the row cost 28 statements with the two after_connect statements at a transaction boundary (before the claim in two runs, before observe_chain_view after four node RPC round trips in one, before the abandonment in one).

Fix. saturate_ledger_pool acquires every slot of the ledger pool (the maximum read from pool.options()), asserts size() == max, and releases them before the warmup rows. At the maximum, an acquire that finds no idle connection waits for the return instead of opening one. drain_set additionally asserts the pool is still at its maximum when a measured window opens, so a closed connection is reported as that rather than as the row that reopened it. The statement counts, the per-row equality, the cross-cardinality equality and the pinned constant are untouched.

offer_lifecycle::every_unfinished_state_is_claimable_retained_and_reserved_only_once

Symptom. offered not claimable (claim_candidate(60) returned None; 3.x.x pushes on 2026-09-17, 09-18, 09-22 and PR CI; on 09-22 it failed 0.8 s into the binary while the other test thread was still creating and migrating its fixture database).

Root cause. The failing claim follows expire(hash), which follows two calls the test expects to be refused: the second record_offer (its transaction took SELECT ... FOR NO KEY UPDATE) and finish_candidate(&claim, false, ..) (require_claim took SELECT ... FOR KEY SHARE before refusing). Both return Err by dropping the transaction, so their row locks stand until the spawned return task flushes the queued ROLLBACK and the server processes it. The expiry UPDATE takes FOR NO KEY UPDATE, which waits for the first lock but does not conflict with FOR KEY SHARE, so it commits while the abandonment's transaction can still hold the row; Ledger::claim_lane_sql selects FOR UPDATE SKIP LOCKED, and FOR UPDATE conflicts with FOR KEY SHARE, so the lane skips the row. This is why only this site fails: the other claim after a refusal has three more round trips before it, and the two later ones are preceded by retry_candidate, whose own FOR UPDATE waits for the pending rollback. It is not a product race: the due-work probe's unlocked selection consuming a slot while the lane skips is the documented approximate service ratio, and the next poll claims the row.

Evidence. A local diagnostic build queried pg_stat_activity the moment the claim returned None at this site, then checked the row lock and retried the claim. The race reproduced once in 40 pre-fix runs of the whole binary pinned to two cores (10 plain, 10 with CPU hogs, 20 with CPU hogs plus a loop creating and dropping databases on the same server). The capture: one backend idle in transaction whose last statement was require_claim's claim fence (the refused finish_candidate transaction, still holding its FOR KEY SHARE lock), the expiry UPDATE backend already idle and committed, the claim's backend idle after COMMIT; immediately afterwards SELECT ... FOR UPDATE SKIP LOCKED returned the row and a retried claim_candidate(60) succeeded. The mechanism is sqlx 0.8.6's Transaction::drop → start_rollback (queue only) and PoolConnection::drop → spawned return_to_pool (ping, then release), plus the PostgreSQL row-lock conflict table.

Fix. expire_lease(pool, hash) replaces the two identical expire closures (this test and the signer-rotation test). It takes SELECT ... FOR UPDATE, which conflicts with every row lock, before the same UPDATE, in one fixture-pool transaction, so it commits only once no earlier transaction on the row is open and the claim that follows finds nothing to skip. Every assertion, including the immediate claim after expiry, is unchanged. rustfmt reformats the rest of the signer-rotation test body now that the closure's long line no longer stops it; those hunks are formatting only.

Loop evidence

Every run is the whole test binary pinned to two cores with PostgreSQL 16 beside it, as on the 2-vCPU CI runner. "Stress" adds two CPU hogs on those cores; "churn" adds a loop creating and dropping databases on the same server.

binary condition runs failures
candidate_storm, before plain 10 0
candidate_storm, before stress, PostgreSQL at normal priority 10 4 (the guard)
candidate_storm, before stress, PostgreSQL reniced to the tests' level 10 0
offer_lifecycle, before plain 10 0
offer_lifecycle, before stress 10 0
offer_lifecycle, before stress + churn 20 1 (offered not claimable)
candidate_storm, fixed plain 1 0
candidate_storm, fixed stress, PostgreSQL reniced 100 0
candidate_storm, fixed stress, PostgreSQL at normal priority (the 4-of-10 condition) 30 0
offer_lifecycle, fixed plain 1 0
offer_lifecycle, fixed stress + churn 100 0
offer_lifecycle, fixed stress, PostgreSQL at normal priority 10 0

Checks

  • both test binaries pass (plain and under stress, above)
  • cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings: clean
  • cargo fmt --all --check: clean
  • scripts/check_gate_manifest.py against the two binaries' 23 gated test ids on their own manifests: all executed, none skipped; the full-suite manifest is proven by this PR's prism-native-postgres job
  • not run locally: the rest of the workspace test suite

test/prism-gated-tests.txt is unchanged (no test id changed).

🤖 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.

…tests deterministic

Both tests raced sqlx's asynchronous connection return. When a
PoolConnection is dropped, sqlx 0.8 spawns a task that pings the server
and only then puts the connection back on the idle queue; a Transaction
dropped on an error path only queues its ROLLBACK, which that same task
flushes. Neither test synchronized with that task.

candidate_storm::drained_rows_cost_the_same_statements_at_the_baseline_and_storm_cardinalities
failed with "a pooled connection opened inside the measured window". The
ledger pool's maximum is 6 but the drain only ever opened 2 connections;
when both were in return flight at a transaction boundary, the next
acquire found the idle queue empty and opened a third, whose
after_connect statements were charged to the row in flight (28
statements instead of 26). Draining warmup rows cannot settle this, so
saturate_ledger_pool now opens every slot before the first measured
window and drain_set asserts the pool is still at its maximum when a
measured window opens. At the maximum, the same acquire waits for the
return instead.

offer_lifecycle::every_unfinished_state_is_claimable_retained_and_reserved_only_once
failed with "offered not claimable". The refused finish_candidate just
before had locked the row FOR KEY SHARE in a transaction whose ROLLBACK
was still queued; the expiry UPDATE (FOR NO KEY UPDATE) does not conflict
with that lock, while the claim lane's FOR UPDATE SKIP LOCKED does, so the
lane skipped the row. expire_lease now takes the row FOR UPDATE before the
expiry UPDATE, so it commits only after every earlier transaction on the
row has rolled back. The same helper replaces the identical closure in the
signer-rotation test.

No assertion changed. The claim path's SKIP LOCKED gap is the documented
approximate service ratio and the next poll retries it, so this is not a
product race.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@djh58

djh58 commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

Review ledger (final)

Head reviewed: 5853c98a, a test-only change to two integration tests. Stakes: low, since no product code changes. This PR removes two recurring CI flakes that have been failing unrelated PRs.

Lanes

Lane Status
Author verification Each fix root-caused with diagnostics. Stress loops before the fix: candidate_storm failed 4 of 30 runs and offer_lifecycle failed 1 of 40. After the fix: 0 of 131 and 0 of 111, including 30 runs under the exact failing condition. Clippy, fmt and the gate checker are clean; the gated-test list is unchanged.
Independent review (coordinator) Run. No findings.
CI on 5853c98a Green, 20/20.
Codex adversarial Not run; capacity unavailable.

Review notes

  • candidate_storm: saturate_ledger_pool acquires every slot of the ledger pool before the warm-up rows and asserts the pool is at its maximum. drain_set re-asserts it at each measured window. At the maximum, an acquire that finds the idle queue empty waits for sqlx's asynchronous return instead of opening a connection, so no measured row can be charged after_connect statements. The per-row, cross-cardinality and pinned-constant assertions are unchanged. A connection closed mid-test now reports as that, instead of being misattributed to the row that reopened it.
  • offer_lifecycle: expire_lease takes the row FOR UPDATE before expiring it. That conflicts with the FOR KEY SHARE and FOR NO KEY UPDATE locks a refused transaction holds until sqlx flushes its queued ROLLBACK, so the following claim no longer sees a transiently locked row through SKIP LOCKED. Neither failure was a product race.
  • Other change: rustfmt re-wrapped an adjacent test that shares the removed closure. No behaviour change.

Dispositions: no open findings.

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