Repository navigation
3.x.x: hand fanout and candidate claims back at shutdown, keep a lost failure's backoff, count refused sends and requeue refused fanouts behind due rows (#573) - #576
Merged
Conversation
… failure's backoff, count refused sends and requeue refused fanouts behind due rows (#573) Four claim and attempt bookkeeping edge cases the #569 audit found in the CTV broadcaster and the candidate submit loop. None loses funds or duplicates a broadcast; each delayed a payout or lost diagnostic state. Shutdown mid-attempt (3). The candidate submit loop dropped its in-flight work at shutdown and kept the claim, so on every rolling restart another frontend waited out the 120 s lease before it could land a won block. It now hands the claim back through the existing token- and state-fenced release_recovery_claim, which keeps the state and schedule exactly as an expiry would. The CTV broadcaster ignored shutdown for a whole pass and was aborted after the 30 s drain, again holding its claim. run() now passes shutdown into the new run_pass: it stops claiming between fanouts, and inside an attempt it abandons the attempt, as the 90 s deadline already does, and releases the claim. Both releases are bounded at 5 s and fall back to the expiry. Lost failure record (2). When process() failed and finish(.., 'failed') failed too, the claim was held until expiry and the row was then retried at once. The new Ledger::release_failed_fanout records the failed attempt and hands the claim back with the backoff finish would have set. It takes neither the settlement lock nor the active-parent check, and leaves the settlement status alone. Refused sends (4). A real send whose completion was refused left no attempt row, since the next claim finds the fanout in the mempool and records only an observation. The refused completion's hand-back now records it as a submitted attempt with the refusal as its error. A refused check-only observation still records nothing. Head-of-line (1). A refused completion now hands its row back due at once but behind every row already due (next_broadcast_attempt_at = clock_timestamp()), so a row refused on every attempt cannot stop the next pass from reaching the others. Every new write is fenced by the holder's token: a completion that did commit, or a claim another frontend took over, is not touched. The next claim re-verifies the chain from scratch as after an expiry, so broadcasts are exactly as idempotent as before, and require_revision and require_fanout are unchanged. No migration, no metric change. Tests: five gated PostgreSQL cases, one per item and one per loop for the shutdown item, each failing against its mutant; a token-fence step fails when the fence is removed. #569's refused-completion case now expects the row due at once rather than NULL. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… requeue order against earlier-due rows (#573 review) Review follow-ups on the #573 hand-backs, with no change to what they do: - finish_fanout and the hand-backs now build the attempt counters, the backoff and the history insert-and-prune from one set of helpers in ledger/fanout.rs (attempt_columns, ATTEMPT_BACKOFF, record_attempt_history), so the two paths cannot drift. The check-only rule is one is_check_only used by both finish_fanout and requeue_refused_fanout, which now takes the attempt's result and decides for itself whether it was a send. The hand-back takes a typed LostAttempt and a static schedule. - A requeued send's recorded error reads "completion not persisted", since the requeue also covers a completion lost to a database error, not only a refusal. - The shutdown releases log whether they failed or timed out. - The candidate shutdown comment no longer claims more than the release does: it changes when a successor may take the row, not what the successor then does. - The requeue-order test now makes every competing row due a minute ago, so a requeue that keeps an earlier schedule (a COALESCE mutant) fails it; before, NULL schedules let such a mutant pass. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member
Author
Deep-review ledger (head
|
| # | Lane | Finding | Disposition |
|---|---|---|---|
| 1 | code-review | No bugs found. It checked the send detection (only the real send returns a non-check-only result), the lost-failure backoff (SET reads the old count, so it matches finish_fanout), the token fences (the history insert runs only when the fenced update matched), the requeue order against claim_fanout's ORDER BY, the broadcaster's biased shutdown select, and the candidate release (the reservation is written before submitblock, and the release is fenced on the token and an unfinished state). |
No action. |
| 2 | thermo | The attempt accounting was duplicated between finish_fanout and the new hand-back: the counters, the 32-row cap, the status counts, the backoff, and the history insert and prune. |
Fixed in 4cdd2d78. One set of helpers in ledger/fanout.rs now serves both paths: attempt_columns, ATTEMPT_BACKOFF and record_attempt_history. Otherwise finish_fanout's SQL is unchanged. |
| 3 | thermo | hand_back_fanout takes an anonymous tuple and a runtime SQL fragment. Fold release_fanout_claim into it. |
Fixed in part in 4cdd2d78. It now takes a typed LostAttempt and a &'static str schedule. release_fanout_claim is kept as it is (rejected). It is #569's public, tested contract, and folding it in would add a branch without removing any SQL. |
| 4 | thermo | The broadcaster re-implemented the check-only rule, and the recorded error said "refused" even for database errors. | Fixed in 4cdd2d78. requeue_refused_fanout takes the attempt's result and applies the shared is_check_only, which finish_fanout uses too. The label is now completion not persisted: …. |
| 5 | thermo | The shutdown-release warnings drop the cause. | Fixed in 4cdd2d78. Both sites now log a failure (with the error) separately from a timeout. |
| 6 | thermo | Three shutdown idioms in broadcaster.rs. |
Fixed in 4cdd2d78. A comment on stopping says why a closed, never-set channel never resolves. |
| 7 | thermo | The ~110-line mature-fanout fixture is copied from refresh_liveness. |
Deferred to #579. This PR deliberately avoided editing shared test support while other lanes were writing tests alongside. |
| 8 | adversarial | The requeue-order test could not catch a requeue that keeps an earlier schedule, because every competing row had a NULL schedule. COALESCE(next_broadcast_attempt_at, clock_timestamp()) passed, although in production it brings the head-of-line bug back. |
Fixed in 4cdd2d78. The test now makes every row due a minute earlier. That mutant (M1b) now fails with the refused fanout was claimed first again, ahead of 4 other due rows. |
| 9 | adversarial | A shutdown that lands while sendrawtransaction is in flight drops the attempt. That send is then not counted: the next claim sees it in the mempool and records only an observation. |
Deferred (coordinator sign-off). Only the diagnostics are affected: the fanout is deterministic and a saved CPFP child is replayed, so nothing is broadcast twice and nothing is lost. The alternative is to hold the claim through the send, which gives back the 120 s takeover this PR removes. Recording "abandoned" attempts for every shutdown, observations included, would over-count instead. |
| 10 | adversarial (note) | Not caused by this PR: a graceful shutdown between the offer reservation's commit and submitblock drops a found block, because an OfferReserved row is never offered again. The old break had the same hole, and #529's standby wait widens it. |
Deferred to #578 (filed by the coordinator and handed to the lane building the deferred candidate-claim release on submit_loop). This PR's release changes only when the successor may take the row, not what it does. The code comment was corrected so it no longer claims more than that. |
Totals: 6 fixed (one in part), 1 rejected (folding release_fanout_claim), 3 deferred (#579, #578, and the diagnostic gap in row 9), 1 with no action needed.
Verification after the fixes:
- Mutants: 9 of 9 are caught: M1, M1b, M2, M2b, M3a, M3b, M3c, M4 and MF.
- PostgreSQL 16: the new tests,
refresh_liveness38,ledger_postgres183,ledger_single_execution22 and the libcandidate_lease_tests20. - Live, with a real qbitd: CTV and CPFP cases 4 of 4.
- Checks: clippy
-D warnings,cargo fmt --checkand the pool-acquire census.
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.
Closes #573 (follow-up to #569 / #571)
Problem
The #569 audit of claim and attempt bookkeeping found four edge cases. None loses funds or duplicates a broadcast; each delays a payout or loses diagnostic state.
next_broadcast_attempt_at, so the next pass claims the same row first again. If it is refused every time, every pass ends there.process()fails andfinish(…, 'failed')fails too, the claim is held until it expires, and then the row is retried at once. The backoff is lost.sendrawtransactionwhose completion is refused records nothing. The next claim finds the fanout in the mempool and records only a check-only observation, so the attempt history and counters under-count it.Fix
Coordinator::submit_looprelease_recovery_claim. That release is fenced on the token and on an unfinished state, keeps the state and schedule exactly as an expiry would, and takesNO KEY UPDATE, so it never waits behind the landing it cut short. A reservation or offer the attempt made is recovered by the next claimer without a second offer, the same as after an expiry.broadcaster::run→ newrun_passrunpasses its shutdown receiver in. The pass stops claiming between fanouts, and inside an attempt it abandons the attempt (the 90 s deadline already drops it the same way) and releases the claim withrelease_fanout_claim. It records nothing and leaves the schedule alone. A durable completion is never interrupted.run_oncekeeps its signature.Ledger::release_failed_fanoutfinish(…, 'failed'). It records the failed attempt (row, counters,last_*) and sets the same backofffinish_fanoutwould have (10·(count+1)s, capped at 3600), then clears the claim. It takes neitherSETTLEMENT_LOCKnor the active-parent check, and leavessettlement_statusalone.Ledger::requeue_refused_fanout(replaces the #569 call site)next_broadcast_attempt_at = clock_timestamp()). The retry backoff is not extended. If the attempt sent the fanout (its result is not check-only), the send is recorded as asubmittedattempt with the send's result andcompletion not persisted: <error>as its error (the requeue also covers a completion lost to a database error). The status and chain evidence the refused completion would have written are left alone.Guarantees kept:
… AND claim_token=$2), in one transaction behindwritable(). A completion that did commit (for example, a lost COMMIT reply) has already cleared the token. A claim another frontend has taken over has a different token. Neither is touched, the attempt row is only inserted when the fenced update matched, and each method reports whether it released anything.process()from scratch, as after an expiry: it renews the claim, re-reads the chain and revision, and checks confirmations and the mempool before any send. No broadcast is possible that an expiry could not already cause.require_revision,require_fanoutand the revision checks inapply_progressare unchanged.finish_fanoutand the hand-backs build the attempt counters, backoff and history insert-and-prune from one set of helpers inledger/fanout.rs(attempt_columns,ATTEMPT_BACKOFF,record_attempt_history,is_check_only), so the two paths cannot drift.finish_fanout's SQL is otherwise unchanged.submittedattempt status and counters.scripts/prism_pool_acquires.jsonregisters the one newbegin()site.Nothing was deferred: all four items are fixed here.
Tests
ctv_fanout_bookkeeping::a_refused_completion_requeues_its_fanout_behind_the_rows_already_duethe refused fanout was claimed first again, ahead of 4 other due rows. M1b: requeue keeps an earlier schedule (COALESCE(next_broadcast_attempt_at, …)) → the same failure, because every competing row is made due a minute earlier.ctv_fanout_bookkeeping::a_failed_attempt_whose_failure_is_lost_keeps_its_backofffailedattempt row and counter, the 10 s backoff and the attempt's own error. The faulted row keeps its settlement status. Token fence: a stale holder'srelease_failed_fanoutandrequeue_refused_fanoutchange nothing on a row another frontend took over.the pass attempted 6 of 5 rows(it spins on the row). M2b: no fallback →the claim was held. MF: hand-back not token-fenced →a stale token handed back another frontend's claimctv_fanout_bookkeeping::a_shutdown_mid_attempt_hands_the_fanout_claim_backthe shutdown left its claim held. M3c: the attempt is not interrupted →the pass waited for its attempt after shutdowncandidate_lease_tests::shutdown_tests::submit_loop_shutdown_hands_the_claim_back_for_an_immediate_takeoverofferedwith its evidence, unclaimed, withlast_error = claim released at shutdown. The successor claims it without any expiry and lands it with exactly onesubmitblock.the shutdown left its claim heldctv_fanout_bookkeeping::a_send_whose_completion_is_refused_is_recorded_as_an_attemptsendrawtransactionis held, the revision is bumped, and the completion is refused. The row is released with onesubmittedattempt: the counters,last_broadcast_submit_resultwith the send's txid, andcompletion not persisted: … payout revision changed …as the error. Its status staysbroadcastableand no backoff is written.the refused send was not counted as a submitted attemptrefresh_liveness::ctv_releases_the_claim_when_a_revision_bump_refuses_a_successful_attempt(updated)next_broadcast_attempt_at <= clock_timestamp()) rather thanNULL. Otherwise unchanged.The five new tests are listed, sorted, in
test/prism-gated-tests.txt. The ids were checked against a local gate manifest. The new fanout tests are a new binary,tests/ctv_fanout_bookkeeping.rs, so this PR touches no shared test file beyond the one-line update above and a two-linemodinclude. The CI shard planner puts that binary in shard 2.Per-PR CI time: about +6 s. That is ~4.6 s for the four fanout cases, which serialize on the fixture lock (~1.1 s each), ~0.9 s for the candidate case, and ~2 s to build and link the new binary locally.
Also run against a private PostgreSQL 16 (fsync on, integration required): all of
refresh_liveness(38),ledger_postgres(183),ledger_single_execution(22), the libcandidate_lease_tests(20, with #570's standby cases) and the lib broadcaster/fanout tests. With a real qbitd, after each commit, thelive_regtestcasesreal_ctv_maturity_broadcast_and_confirmationandcpfp_tests::*(4/4). Alsocargo fmt --check,cargo clippy -p qbit-prism-server --all-targets -D warnings,check_prism_pool_acquires.py,check_gate_env_reads.py,check_prism_settings.py,check_deleted_test_map.py, and the gate/shard Python tests.Follow-ups
submitblockdrops a found block. That window predates this PR: the oldbreakdropped the same work. This PR only hands the claim back sooner, and the successor does what it would have done after the expiry.refresh_liveness, intotests/support.For lane S (#529 follow-up)
Lane S is building a deferred candidate-claim release on the same
submit_loop. The shutdown path this PR adds is the arm_ = shutdown.changed() => { … }inCoordinator::submit_loop. By the time that arm runs,tokio::select!has already dropped the in-flightprocess_candidatefuture and its heartbeat. The arm then callsself.ledger.release_recovery_claim(&claim, "claim released at shutdown")undertokio::time::timeout(CANDIDATE_LEASE.timeout, …), logscandidate claim release at shutdown deferred to its expiryif the release fails or times out, and breaks. A deferred release can reuse that call as it is. It is fenced on the token and on an unfinished state, keeps the state and schedule, and takesNO KEY UPDATE, so it never waits behind a landing'sKEY SHARE. Only the trigger and the reason string need to change.submit_loop_shutdown_hands_the_claim_back_for_an_immediate_takeoverpins the behaviour.#560 (e2e scenario manifest) is still open, and none of these tests is a
live_regtest/load or nightly test, so no manifest entry is needed.🤖 Generated with Claude Code