Skip to content

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
djh58 merged 2 commits into
3.x.xfrom
djh58/prism-b573-fanout-edge-cases
Sep 29, 2026
Merged

djh58 merged 2 commits into
3.x.xfrom
djh58/prism-b573-fanout-edge-cases

Conversation

@djh58

@djh58 djh58 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

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.

  1. Head-of-line on persistent refusal. A completion refused after a good attempt (3.x.x: a CTV fanout claim leaks for up to 120 s when finish_fanout fails after a successful attempt (revision bump in between) #569) hands the claim back with its old next_broadcast_attempt_at, so the next pass claims the same row first again. If it is refused every time, every pass ends there.
  2. A failed attempt whose failure record is lost. If process() fails and finish(…, 'failed') fails too, the claim is held until it expires, and then the row is retried at once. The backoff is lost.
  3. Shutdown mid-attempt holds the claim. The candidate submit loop drops its in-flight work at shutdown and keeps the claim, so another frontend waits out the 120 s lease before it can land a won block. That happens on every rolling restart. The CTV broadcaster ignores shutdown for its whole pass, is aborted after the 30 s drain, and keeps its claim the same way.
  4. Refused sends leave no attempt row. A real sendrawtransaction whose 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

# Where Change
3 Coordinator::submit_loop At shutdown, after the in-flight work is dropped, the claim is handed back through the existing release_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 takes NO 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.
3 broadcaster::run → new run_pass run passes 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 with release_fanout_claim. It records nothing and leaves the schedule alone. A durable completion is never interrupted. run_once keeps its signature.
2 new Ledger::release_failed_fanout The fallback for a lost finish(…, 'failed'). It records the failed attempt (row, counters, last_*) and sets the same backoff finish_fanout would have (10·(count+1) s, capped at 3600), then clears the claim. It takes neither SETTLEMENT_LOCK nor the active-parent check, and leaves settlement_status alone.
1 + 4 new Ledger::requeue_refused_fanout (replaces the #569 call site) A refused completion hands its row back due at once but behind every row already due (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 a submitted attempt with the send's result and completion 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:

  • Every new write is fenced by the holder's token (… AND claim_token=$2), in one transaction behind writable(). 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.
  • The next claim runs 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_fanout and the revision checks in apply_progress are unchanged.
  • A failed release (cluster halted, database unreachable) is logged and falls back to the lease expiry, as today. Shutdown releases are bounded at 5 s, well inside the 30 s drain.
  • finish_fanout and the hand-backs build the attempt counters, backoff and history insert-and-prune from one set of helpers in ledger/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.
  • No migration. No Prometheus metric changes, so no promtool or alert-generator updates. Item 4 reuses the existing submitted attempt status and counters. scripts/prism_pool_acquires.json registers the one new begin() site.

Nothing was deferred: all four items are fixed here.

Tests

Test Item What it pins Mutant (run, then reverted) → failure
ctv_fanout_bookkeeping::a_refused_completion_requeues_its_fanout_behind_the_rows_already_due 1 A real attempt is held at its tip re-check, the revision is bumped, and the completion is refused. Every row has been due for a minute. The refused row is released, no attempt is recorded, and it is due again, but another frontend's next claim takes a different row. The next pass settles every row. M1: requeue keeps the old schedule → the 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_backoff 2 Every attempt fails (no live covenant output). The first failure record loses its connection after executing (execution-proxy fault on a marker trigger), so its transaction aborts. Afterwards every row is released, with exactly one failed attempt 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's release_failed_fanout and requeue_refused_fanout change nothing on a row another frontend took over. M2: fallback is a plain release → 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 claim
ctv_fanout_bookkeeping::a_shutdown_mid_attempt_hands_the_fanout_claim_back 3 A pass is held inside an attempt, on a node call that never answers, when shutdown is set. The pass returns 0 promptly, the row is unclaimed with no attempt recorded and its schedule untouched, and another frontend claims the same row at once. A pass that starts stopped claims nothing. M3b: no release → the shutdown left its claim held. M3c: the attempt is not interrupted → the pass waited for its attempt after shutdown
candidate_lease_tests::shutdown_tests::submit_loop_shutdown_hands_the_claim_back_for_an_immediate_takeover 3 The submit loop offers a block, waits for build capacity, and is shut down. The row stays offered with its evidence, unclaimed, with last_error = claim released at shutdown. The successor claims it without any expiry and lands it with exactly one submitblock. M3a: no release → the shutdown left its claim held
ctv_fanout_bookkeeping::a_send_whose_completion_is_refused_is_recorded_as_an_attempt 4 A real built-in-fee sendrawtransaction is held, the revision is bumped, and the completion is refused. The row is released with one submitted attempt: the counters, last_broadcast_submit_result with the send's txid, and completion not persisted: … payout revision changed … as the error. Its status stays broadcastable and no backoff is written. M4: send not passed → the refused send was not counted as a submitted attempt
refresh_liveness::ctv_releases_the_claim_when_a_revision_bump_refuses_a_successful_attempt (updated) 1 After a refused completion, the recheck is now due at once (next_broadcast_attempt_at <= clock_timestamp()) rather than NULL. 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-line mod include. 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 lib candidate_lease_tests (20, with #570's standby cases) and the lib broadcaster/fanout tests. With a real qbitd, after each commit, the live_regtest cases real_ctv_maturity_broadcast_and_confirmation and cpfp_tests::* (4/4). Also cargo 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

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() => { … } in Coordinator::submit_loop. By the time that arm runs, tokio::select! has already dropped the in-flight process_candidate future and its heartbeat. The arm then calls self.ledger.release_recovery_claim(&claim, "claim released at shutdown") under tokio::time::timeout(CANDIDATE_LEASE.timeout, …), logs candidate claim release at shutdown deferred to its expiry if 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 takes NO KEY UPDATE, so it never waits behind a landing's KEY SHARE. Only the trigger and the reason string need to change. submit_loop_shutdown_hands_the_claim_back_for_an_immediate_takeover pins 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

… 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]>
@djh58

djh58 commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Deep-review ledger (head 4cdd2d78, CI green)

Stakes: high. The change is on the CTV settlement path (who may attempt a fanout, and when) and on the found-block submit loop's shutdown.

Lanes: /code-review medium, a thermo-nuclear quality review, and an adversarial review (Claude). The second adversarial model and bugbot were not run, because this lane is restricted to Claude. No bot or reviewer comments had been posted when this ledger was written.

# 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_liveness 38, ledger_postgres 183, ledger_single_execution 22 and the lib candidate_lease_tests 20.
  • Live, with a real qbitd: CTV and CPFP cases 4 of 4.
  • Checks: clippy -D warnings, cargo fmt --check and the pool-acquire census.

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