Skip to content

3.x.x: release a CTV fanout claim whose completion is refused after a successful attempt, instead of holding it for 120 s (#569) - #571

Merged
djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b569-fanout-claim-release
Sep 29, 2026
Merged

djh58 merged 1 commit into
3.x.xfrom
djh58/prism-b569-fanout-claim-release

Conversation

@djh58

@djh58 djh58 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes #569 (split out of #562)

Problem

broadcaster::run_once claims a CTV fanout for 120 s, runs process(), then records the result with finish_fanout(). The completion goes through apply_progress, which requires the payout_revision that process() observed. When a block lands in between, the revision moves and finish_fanout fails with payout revision changed while observing chain state.

In the success branch that error left run_once with the claim still held. Nothing finished the row and nothing cleared the claim. The fanout stayed claimed, with attempt_count 0, until claim_expires_at, up to 120 s, and no server could check it again. The process()-error branch never had this problem, because it releases through finish(…, 'failed'). This is the weighted_recipients … timed out waiting for every fanout confirmed failure: that wait is 60 s, shorter than the lease.

Fix

When finish_fanout fails after a successful process(), run_once hands the claim back through the new Ledger::release_fanout_claim, then ends the pass with the same error as before. The release is an early expiry and nothing more:

  • It clears claim_token, claim_instance_id and claim_expires_at fenced by the holder's token, in one statement behind the usual writable() check. A completion that actually committed (for example one whose COMMIT reply was lost) has already cleared the token. A claim another frontend has since taken over has a different token. Neither is touched, and the method reports whether it released anything.
  • It records no attempt, and it leaves settlement_status, next_broadcast_attempt_at, the attempt counters, the scan cursor and the CPFP reservations as they were. So the row ends up exactly as an expired claim would, only without the 120 s wait.
  • The next claim runs process() from scratch: it renews the claim, observes the chain and revision again, re-verifies the covenant and parent maturity, and checks the mempool and confirmations before any send. That is the same path a claim takes after expiry today, so no new broadcast is possible that expiry could not already cause, and whatever this attempt sent is already known to the node. require_revision and require_fanout are unchanged.
  • The pass still ends on the error, so the frontend does not spin on the row. The next tick (default 10 s), or another frontend, picks it up. This completion can fail after a good attempt only in a few ways, because process() builds the status and the evidence itself. The revision moved, the claim was lost, or a database or connection error occurred: all transient. Or the parent left the active chain, which also takes the row out of claim_fanout. So a quicker retry replaces a 120 s stall rather than a backoff.
  • If the release itself fails (the cluster is halted, or the database is unreachable), it is logged as CTV claim release deferred and the claim falls back to expiry, as it does today.

A completion that loses its database connection is also a failed completion after a good attempt, so it now releases too. refresh_liveness::ctv_chunk_metrics_count_an_attempt_whose_completion_failed_to_persist asserted the old fenced-until-expiry outcome, and now expects the claim back and every row finished by the next pass. Its metric assertions are otherwise unchanged.

No migration and no new metric. scripts/prism_pool_acquires.json registers the new begin() site.

Other claim/lease paths audited (same "success, then the completion fails, claim held" shape)

Path What happens after a successful step whose completion fails Verdict
CTV fanout, success branch (run_once) claim held until expiry fixed here
CTV fanout, process() error or 90 s deadline branch finish(…, 'failed') releases the claim with backoff. If that write fails (claim lost, parent no longer confirmed and mature, cluster halted, database error), the claim is held until expiry. That completion carries no payout_revision, so a landing cannot refuse it. Not the same shape. Releasing there would drop the backoff that a failed attempt should record, so it is not trivially safe. Listed for #562.
Candidate submit loop (claim_candidate → process_candidate) any error, including finish_candidate* failing after a successful submitblock, goes to retry_candidate, which clears the claim with min(60, attempt_count) s backoff (reconciliation rows min(3600, 10·attempts)) not affected
Candidate heartbeat (with_candidate_heartbeat) a lease loss after a terminal commit is recognised as completion. The work future is dropped with the heartbeat, so no orphan renews. not affected
Operator recovery (claim_candidate_for_recovery, recover_candidate) every error path calls release_recovery_token or release_recovery_claim, token-fenced and bounded not affected
Offer reservation (reserve_in → record_offer, release_unsent_offer) runs inside the candidate claim, and its failures reach retry_candidate as above not affected
Candidate submit loop on shutdown select! drops the in-flight work, and the claim is held until expiry shutdown only, by design; listed for #562 for completeness
Session-owner reservations, instance heartbeat process-scoped ownership, released on drop or shutdown not a claim/finish pair
Job issue and persistence, job cohort fences no claim; fences are transaction-scoped row locks not applicable

Follow-ups, not fixed here (for #562):

  1. Fanout failure branch. If finish(…, 'failed') itself fails, the claim is held until it expires. Releasing it there would drop the backoff that a failed attempt should record, so it needs its own design (for example a token-fenced "retry at backoff" write) rather than this release.
  2. Candidate submit loop on shutdown. Shutdown drops the in-flight candidate work, and the claim is held until its lease expires.

Tests

Test Kind What it pins Per-PR CI time
refresh_liveness::ctv_releases_the_claim_when_a_revision_bump_refuses_a_successful_attempt (new) gated PG A real run_once attempt is held at its tip re-check, after it has read its chain view and payout revision. The revision is bumped, as a landing does, and the attempt is released. The pass fails with payout revision changed, and the fanout is unclaimed at once: no token, no instance, no expiry, broadcast_attempt_count 0, recheck still due. The next pass settles every row: all confirmed, unclaimed, recheck scheduled. A stale token cannot release a claim another frontend took over after expiry; the holder can release its own. ~1.6 s
refresh_liveness::ctv_chunk_metrics_count_an_attempt_whose_completion_failed_to_persist (updated) gated PG a completion that loses its connection hands the claim back, and the next pass finishes all rows unchanged

The new test is listed, sorted, in test/prism-gated-tests.txt. Net per-PR CI time: about +2 s, in the shard that runs refresh_liveness.

Mutants (each run, then reverted):

  • A. No release (if finished.is_err() && false): the new test fails with the refused completion left its claim held, and the updated test fails with aborted completion left 1 fenced claims. The other 4 CTV cases pass.
  • B. Release not fenced by the token (… AND claim_token IS NOT NULL): the new test fails with a stale token released another frontend's claim.

Also run: all 38 refresh_liveness tests against a private PostgreSQL 16 (fsync on, integration required); cargo fmt --check; cargo clippy -p qbit-prism-server --all-targets -D warnings; scripts/check_prism_pool_acquires.py.

Live weighted_recipients under 2 pinned CPU burners (two busy loops and the test sharing two cores, 5 runs each):

Build Passed Failures Refused completions (the #569 race) seen in server logs
this branch 4 of 5 1: the harness's 45 s RPC client timed out on a generatetoaddress batch during the difficulty ramp, before any fanout existed. #563, merged after this branch point, gives that call a 600 s deadline. 0
release disabled (base behaviour) 5 of 5 none 0

On this host the race never fired in 10 runs, with either build, so these reruns show no regression but do not exercise the fix. The 2-of-4 base failures reported while investigating #562 were seen on a differently loaded host. The fix is proven by the deterministic gated test above, which forces the race.

Live CTV and CPFP cases (cpfp_tests ×3, real_ctv_maturity_broadcast_and_confirmation, two_node_tests::real_deep_reorg…): all pass on this branch. On a saturated host (load average 22–32 on 22 cores) the deep-reorg case sometimes failed with the same fixture RPC timeout, because qbitd went silent for 45 s in the middle of generatetoaddress. In an interleaved comparison the release never ran in any of those runs: the only broadcaster errors were the pre-claim qbit is not caught up checks. With this branch's server the case passed 6 of 10 (the 4 failures were all at load ≥ 22), and with the release disabled it passed 8 of 8 (loads up to 23). Once the load fell, both builds passed every run. This is a load-sensitive fixture timeout, and not this change. #563 lengthened the equivalent call in the weighted-recipients file only, so the two-node file's generatetoaddress still uses the 45 s client. Noted for #562.

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

… successful attempt, instead of holding it for 120 s (#569)

When a block landed between a successful fanout attempt and its
completion, the payout revision bump made finish_fanout fail with
"payout revision changed while observing chain state". The error left
run_once with the claim still held, so the fanout stayed claimed, with
no attempt recorded, until its 120 s lease expired, and no server could
check it again. The attempt-failure branch already released its claim
through finish(..., 'failed'); only success followed by a refused
completion leaked.

run_once now hands such a claim back through the new
Ledger::release_fanout_claim, then ends the pass with the same error as
before. The release is an early expiry: it clears the claim fenced by
the holder's token, records no attempt and leaves the recheck schedule
alone. A completion that did commit, or a claim another frontend has
taken over, is not touched. The next claim re-verifies the chain from
scratch, as after an expiry, so broadcasts stay exactly as idempotent
as before and every revision check is unchanged.

A completion that loses its connection is also a refused completion,
so refresh_liveness's lost-completion case now expects the claim back
and every row finished by the next pass.

Tests: a gated PostgreSQL case holds a real attempt at its tip
re-check, bumps the payout revision, and asserts the refused completion
leaves the fanout unclaimed with no attempt recorded, that the next
pass settles every row, and that a stale token cannot release another
frontend's claim.

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

djh58 commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Deep-review ledger (head a7ac5be4, CI green)

Stakes: high. The change is on the CTV settlement path: who may attempt a fanout, and when.

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

# Lane Finding Disposition
1 code-review No bugs found. It checked a completion that committed but reported an error (the token is already cleared, so the release is a no-op), a takeover after expiry (the token does not match, so it is a no-op), an unwritable database (the release fails and the claim falls back to expiry), and an immediate retry after a broadcast (getmempoolentry turns it into a check-only observation, and a persisted CPFP child is replayed). No action.
2 code-review (note) A released row keeps its old next_broadcast_attempt_at, so the next pass claims it first. If its completion were refused on every attempt (the revision moving during each one), each pass would stop at that row. The old 120 s hold let other rows go ahead meanwhile. Deferred to #562. It needs a revision change inside nearly every attempt of the same row, which does not happen at normal block rates. The old hold was an accidental backoff, not a designed one. If it is wanted, the cheap follow-up is to release with next_broadcast_attempt_at = clock_timestamp(), which keeps the row claimable at once but orders it behind rows already due. That would be a separate settlement-path change with its own test.
3 thermo Put the release inside finish_fanout (the canonical ledger layer) instead of the call site. Rejected. The failed completion's transaction has aborted, so the release must be a separate transaction. A lost connection cannot be recovered inside it either. run_once owns the claim's lifetime, and the failure branch already completes at the same call site. A combined "finish or release" would hide a second write behind one call.
4 thermo release_fanout_claim returns a bool that production ignores. Rejected. The bool is the observable contract of the token fence (a stale token releases nothing), and the test pins it. The caller logs only the release failures that matter.
5 thermo The new test covers two scenarios: the refused completion, and the token fence. Rejected. Both need the same mature-fanout fixture. Sharing it saves about 1.5 s of per-PR CI, and the fence part is commented as its own step. Mutant B fails only that step.
6 thermo broadcaster.rs is over 1k lines. No action. It was 1019 lines before this PR (1030 after), so the PR does not cross the line, and the change is 11 lines in run_once.
7 adversarial No refutation found. It attacked a committed but unacknowledged completion (the token is already cleared), a takeover between expiry and release (the token fence), and a re-send by the next claimer (the mempool, confirmation and CPFP-package checks run before any send, as after an expiry). All three held. No action.
8 adversarial (note) A real sendrawtransaction attempt whose completion is refused leaves no row in qbit_ctv_fanout_broadcast_attempts, because the next claim sees the transaction in the mempool and records only a check-only observation. Deferred. It predates this PR: expiry lost the same row. It affects the attempt history only, and nothing is sent twice. Noted for #562.

Totals: 0 fixed, 3 rejected, 2 deferred, 3 with no action needed.

Follow-ups already listed in the PR body (not fixed here): a failure-branch completion that itself fails still holds the claim until it expires (releasing there would drop the backoff), and a candidate claim dropped at shutdown is held until its lease expires.

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