Skip to content

friglet: make the P2P block download cancellable, so Stop / Stop daemon / Quit act within a second (SNB-660) - #40

Merged
setavenger merged 2 commits into
integrationfrom
snb-660-cancellable-p2p-fetch
Oct 2, 2026
Merged

setavenger merged 2 commits into
integrationfrom
snb-660-cancellable-p2p-fetch

Conversation

@setavenger

Copy link
Copy Markdown
Owner

Linear: SNB-660 (related: SNB-658 / #37)

Problem

After #37, "Stop scanning" is honest: the daemon says "stopping…" until the scan loop really ends. But blindbit-lib downloaded blocks with blocking socket I/O inside the scan future: up to 90 s per attempt, 5 attempts with backoff. So the stop could take minutes, and an exiting daemon gave up on the scan after about 12 s. Live, against a node stalled mid-block, the integration daemon took 88.5 s to answer Stop.

What changes

blindbit-lib

  • BlockFetcher::with_cancel(token) (p2p.rs). The TCP connect is now async. The handshake and download run on a thread of their own, and the fetch waits for that thread under select! against the token. On cancel, or when the fetch future is dropped, a second handle shuts the socket down, which wakes the blocked read at once. The backoff sleeps between attempts are cancellable too. A cancelled fetch fails with FetchFailure::Cancelled.
  • Scanner::watch_chain_until(token) (scanning.rs). watch_chain() behaves as before. The token also ends:
    • the poll and retry waits;
    • the oracle requests;
    • the wait for the next streamed block.
  • Where the scan can stop: only between blocks, or while it waits. It never stops part way through applying a block. It returns the new ScanCancelled, which is not a stall and causes no backoff.
  • A block whose download is cancelled is not applied at all, and the next run resumes at it.
  • A witness restore cut short runs again on the next start, not 5 min later.

friglet

  • ScanSupervisor hands each run's token to the scan and waits for it to stop by itself. Only a task still running 2 s (CANCEL_GRACE) after the cancel is dropped, as before. An error a stopping task returns is not recorded as a scan failure.
  • Exit waits at most 3 s for the scan (was 2 + 10 s). The stop messages no longer blame a blocking download.

Verification

Tests (cargo test --workspace, debug):

  • cancel_tests::a_stop_during_a_stalled_block_download_ends_the_scan_at_once_and_it_resumes_there
    • A scripted peer stalls mid-block on the second of three payment blocks.
    • The scan ends within 1 s of the stall.
    • Height stays at the first block. Nothing of the second block is in the indexer, outputs, checkpoints, scanned hashes or Electrum index.
    • No stall is reported and there is no backoff.
    • Restarted, the scan fetches the block on a fresh connection and reaches the end.
  • cancel_tests::a_stop_ends_the_wait_between_download_attempts: cancels during a 30 s backoff wait.
  • cancel_tests::a_stop_ends_the_wait_for_the_next_poll: runs the real watch_chain_until and cancels during its 10 s poll wait.
  • witness_tests::a_stop_during_a_witness_restore_retries_it_when_scanning_resumes
  • control::tests::stop_during_a_stalled_p2p_download_pauses_within_a_second: the real BlockFetcher against a silent node, under the real supervisor, through the control Stop handler.
  • New supervisor tests: a cooperative stop, a task that ignores the token (dropped after the grace period), and an error while stopping.
  • Mutation check: with with_cancel made a no-op (and the poll sleep un-cancellable), all five new stop tests fail: 11.5 s, 29.7 s, 9.5 s, > 1 s, and Stop 2.0 s (the grace fallback).

Live check, signet:

  • Setup:
    • Hosted oracle, node signet.oracle.setor.dev:38333 behind a proxy that forwards the block message header and half its payload, then goes silent.
    • Fresh throwaway key.
    • The state was given one owned output whose key prefix the oracle reports spent in block 324640, so the scan must download that block.
integration (017131a) this branch
Stop during the stalled download: answered 88.5 s 19 ms (Ok)
status paused after Stop 88.5 s 20 ms
saved height after Stop — 324639 (block 324640 not committed)
Start → resumed at 324640, reached tip 324649 — 0.4 s, block re-fetched on a fresh connection
Shutdown during the stalled download → process exit — 165 ms (100 ms is the handler's reply delay); saved height 324639

Not touched: friglet-tray, friglet-ipc.

setavenger and others added 2 commits October 2, 2026 13:25
"Stop scanning", "Stop daemon" and Quit now take effect within
milliseconds, also in the middle of a block download from a node that
went silent. Before, blindbit-lib downloaded blocks with blocking socket
I/O inside the scan future (up to 90 s per attempt, 5 attempts), so the
status could say "stopping" for minutes and an exiting daemon gave up
waiting for the scan after about 12 s.

blindbit-lib:
- BlockFetcher::with_cancel(token): the TCP connect is async, the
  handshake and download run on a thread of their own and the fetch
  awaits their answer under select! with the token; on cancellation (or
  drop) the socket is shut down, which wakes the blocked read at once.
  The backoff sleeps between attempts are cancellable too. A cancelled
  fetch fails with FetchFailure::Cancelled.
- Scanner::watch_chain_until(token) (watch_chain keeps its behaviour):
  the token also ends the poll and retry waits, the oracle requests and
  the wait for the next block of the stream. The scan stops only between
  blocks or while it waits, never part way through applying a block, and
  returns the new ScanCancelled error: no stall, no backoff. A block
  whose download is cancelled is not applied at all; the next run resumes
  at it. A witness restore cut short is taken up again on the next start
  instead of after 5 min.

friglet:
- ScanSupervisor hands each run's token to the scan and waits for it to
  stop by itself; only a task still running CANCEL_GRACE (2 s) after the
  cancellation is dropped, as before. An error a stopping task returns is
  not recorded as a scan failure.
- Exit waits at most 3 s (was 2 + 10 s) for the scan; the stop messages
  no longer blame a blocking download.

Tests: a scripted P2P peer that stalls mid-block (scan ends within 1 s
of the stall, no partial commit, resume at that block), the backoff and
poll waits, a stalled witness restore, and a control-socket Stop during a
stalled download through the real supervisor (paused within 1 s). With
the fetch's cancellation removed, all five fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-660)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger
setavenger merged commit a9ff4d6 into integration Oct 2, 2026
1 check passed
@setavenger
setavenger deleted the snb-660-cancellable-p2p-fetch branch October 2, 2026 14:54
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