Repository navigation
3.x.x: make the candidate_storm drain-cost and offer_lifecycle claim tests deterministic - #498
Merged
Merged
Conversation
…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]>
Member
Author
Review ledger (final)Head reviewed: Lanes
Review notes
Dispositions: no open findings. |
This was referenced Sep 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes two recurring
prism-native-postgresflakes deterministic without changing what either test asserts. Both were races against sqlx 0.8's asynchronous connection return: dropping aPoolConnectionspawns a task that pings the server before the connection is idle again, and aTransactiondropped on an error path only queues itsROLLBACKfor that task to flush.candidate_storm::drained_rows_cost_the_same_statements_at_the_baseline_and_storm_cardinalitiesSymptom.
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 everycommit→ nextbeginboundary races the spawned return task. The pool only ever opened two connections; when both were in return flight at a boundary,acquirefound the idle queue empty, sawsize < max, and opened a third whoseafter_connectstatements (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 atsize=2; the extra connection opened withsize=2, idle=0(oridle=1with the second connection's return lagging across the row), and the row cost 28 statements with the twoafter_connectstatements at a transaction boundary (before the claim in two runs, beforeobserve_chain_viewafter four node RPC round trips in one, before the abandonment in one).Fix.
saturate_ledger_poolacquires every slot of the ledger pool (the maximum read frompool.options()), assertssize() == 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_setadditionally 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_onceSymptom.
offered not claimable(claim_candidate(60)returnedNone; 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 secondrecord_offer(its transaction tookSELECT ... FOR NO KEY UPDATE) andfinish_candidate(&claim, false, ..)(require_claimtookSELECT ... FOR KEY SHAREbefore refusing). Both returnErrby dropping the transaction, so their row locks stand until the spawned return task flushes the queuedROLLBACKand the server processes it. The expiryUPDATEtakesFOR NO KEY UPDATE, which waits for the first lock but does not conflict withFOR KEY SHARE, so it commits while the abandonment's transaction can still hold the row;Ledger::claim_lane_sqlselectsFOR UPDATE SKIP LOCKED, andFOR UPDATEconflicts withFOR 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 byretry_candidate, whose ownFOR UPDATEwaits 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_activitythe moment the claim returnedNoneat 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 backendidle in transactionwhose last statement wasrequire_claim's claim fence (the refusedfinish_candidatetransaction, still holding itsFOR KEY SHARElock), the expiryUPDATEbackend already idle and committed, the claim's backend idle afterCOMMIT; immediately afterwardsSELECT ... FOR UPDATE SKIP LOCKEDreturned the row and a retriedclaim_candidate(60)succeeded. The mechanism is sqlx 0.8.6'sTransaction::drop→start_rollback(queue only) andPoolConnection::drop→ spawnedreturn_to_pool(ping, then release), plus the PostgreSQL row-lock conflict table.Fix.
expire_lease(pool, hash)replaces the two identicalexpireclosures (this test and the signer-rotation test). It takesSELECT ... FOR UPDATE, which conflicts with every row lock, before the sameUPDATE, 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.
offered not claimable)Checks
cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings: cleancargo fmt --all --check: cleanscripts/check_gate_manifest.pyagainst the two binaries' 23 gated test ids on their own manifests: all executed, none skipped; the full-suite manifest is proven by this PR'sprism-native-postgresjobtest/prism-gated-tests.txtis unchanged (no test id changed).🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.