Skip to content

friglet: Start/Stop no longer wait behind an apply's network lookups (SNB-685) - #60

Closed
setavenger wants to merge 4 commits into
integrationfrom
snb-685-apply-lock-network
Closed

setavenger wants to merge 4 commits into
integrationfrom
snb-685-apply-lock-network

Conversation

@setavenger

@setavenger setavenger commented Oct 3, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #53; review only the commits after it.

Fixes SNB-685 (Bug). Part of the friglet cleanup (SNB-661).

The bug

ControlCtx::apply() took apply_lock first and then, while holding it:

  • looked up a new wallet's birthday at the oracle (resolve_start_at_tip, up to 15 s);
  • resolved the P2P host name (validate_daemon_config → resolve_peer_addr, a blocking DNS lookup).

Start and Stop take the same lock, so a Stop or Start sent during an ApplySettings with start_at_tip waited for both lookups.

The fix (minimal)

  • The two lookups now run in prepare() before the lock is taken.
  • Under the lock apply() still reads the current settings, compares, persists and requests the restart. The guarantee that a settings change never interleaves with Start/Stop or another apply is kept for the state change itself. The existing start_and_stop_wait_for_the_apply_lock test still holds.
  • One new case comes from preparing before the lock: a SetScanKey (no config of its own) prepares against a snapshot of the settings. If another apply replaced them while it waited for the lock, it drops the lock, prepares again from the newer settings and takes the lock again to check. It persists only once the settings it prepared from are the ones under the lock. The outcome is the same as running the two one after the other. Without that, it would have written the stale config back. (Second commit, from review: the first version re-prepared while holding the lock.)
  • Lock ordering: no new nesting. apply_lock (tokio) is still taken first, and settings (std) only briefly and never across an await. A request with invalid input now gets its error without waiting for the lock.

Tests

  • stop_and_start_answer_while_an_apply_waits_for_the_oracle: a fake oracle accepts the connection and never answers. An ApplySettings with start_at_tip is left waiting on it, then Stop and Start must each answer within 2 s (they take < 1 s). On the old apply(), Stop timed out: left: Err(Elapsed(())) right: Ok(Ok). With the fix it passes in 0.33 s.
  • set_scan_key_keeps_settings_changed_while_it_waited: holds apply_lock, sends SetScanKey, then lets "another apply" change the settings. Afterwards the newer settings and the new key are both in place. Without the re-prepare branch it fails, because the stale config is written back.
  • preparing_again_after_a_concurrent_apply_does_not_hold_the_lock: same race, but preparing the new settings waits on an oracle that never answers. Stop and Start still answer within a second, and nothing is persisted. With the re-prepare under the lock (first commit), Stop timed out: left: Err(Elapsed(())) right: Ok(Ok).
  • The control tests passed 3 runs in a row after the second commit (20 tests, ~4.8 s each).

Verification

Not changed

The DNS lookup is still a blocking call on a tokio worker thread (std::net::ToSocketAddrs). Its effect on Start/Stop was the lock, and that is fixed; the worker itself is SNB-690 (stacked PR).

setavenger and others added 3 commits October 3, 2026 08:47
The Electrum server built the same things in several places. One copy
of each now:

- notification(): the JSON-RPC notification line (was written out four
  times); sp_subscription(): the SP subscription object and its
  notification, shared by the push task and the subscribe handler.
- invalid_param(): the -32602 answer for a missing or mistyped first
  parameter (block.header, scripthash.subscribe/get_history,
  transaction.get); the messages are unchanged.
- save_header_sidecar(): the header-sidecar write after a backfill and
  after a reorg.
- Peer::header_by_hash() takes one hash (its only caller passed a
  one-element slice) and returns only a header with that hash, so the
  two re-checks of the same hash in ChainSource::header and
  backfill_header go.
- relay: connect_settled() (connect + settle, the 2 s literal is now
  p2p::SETTLE_WAIT, also used by ChainSource::fee_filter),
  Candidate::wire_tx() and Signals::announce() replace two or three
  copies each.
- index_unconfirmed_tx: two match arms that both did nothing are one
  condition.
- Two tests build their request with call() like the others.

The bytes on the wire are unchanged: a scripted session of 52 requests
and 8 push batches gives byte-identical output before and after.

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

Leftovers from the live scanner swap and from earlier refactors:

- ControlCtx keeps the Electrum index and the label addresses directly;
  the std Mutex around each was never written after the live
  rebuild-and-swap went away (#29). The Start comment that still
  described the swap is fixed.
- One-line wrappers around friglet-ipc are gone (default_config_path,
  default_key_file, write_config_file, write_key_file, replace_scan_key);
  apply() writes the key it already parsed instead of having
  replace_scan_key parse it again.
- pick_tip was `oracle.or(electrum)`; inlined, with its test.
- control's unix_now duplicated pending::now_secs.
- The key file's "first non-empty line" is read by one helper.
- ResolvedConfig no longer copies oracle_url, max_label_num, http_addr
  and electrum_addr out of `raw`.
- main no longer sets sp_start_height a second time right after
  rebuild_electrum_index_from_graph set it.
- control::run takes the ControlCtx instead of a generic handler; its
  only other user was get_status_roundtrip_over_control_socket, which
  repeated friglet-ipc's roundtrip test and every serve_ctx test.
- supervisor: stop_during_a_download_pauses_at_once repeated the
  cooperative-cancel tests and left a 30 s thread behind.
- fake_peer: the single-field Seen struct is a Vec.
- Stale comments: types.rs, the --oracle-url default, electrum::run.

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

apply() took apply_lock first and then looked up a new wallet's
birthday at the oracle (up to 15 s) and resolved the P2P host name
(blocking DNS) while holding it, so a Stop or Start sent during an
ApplySettings with start_at_tip waited for both.

Both lookups now run in prepare(), before the lock. Under the lock
apply() still reads the current settings, compares, persists and
requests the restart, so a settings change still never interleaves with
Start/Stop or another apply. A SetScanKey (no config of its own) whose
settings snapshot was replaced by another apply while it waited for the
lock prepares again from the newer settings, as if the two had run one
after the other.

Tests: Stop and Start answer within a second while an apply waits on an
oracle that never answers (the old code timed out after 2 s); a
SetScanKey that waited behind another apply keeps that apply's settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review: when a SetScanKey's settings snapshot had been replaced by
another apply while it waited for the lock, it prepared again (oracle
birthday lookup, blocking DNS) while holding apply_lock, so Start/Stop
could still wait behind the network in that case, and the field doc was
not true.

apply() now drops the lock, prepares again from the new settings and
takes the lock again to check; it persists only once the settings it
prepared from are the ones under the lock. Comparing and persisting
still happen under the lock only, so Start/Stop never see a half-applied
change and two applies never mix their writes.

New test: settings replaced (by a config whose preparation waits on an
oracle that never answers) while a SetScanKey waited for the lock; Stop
and Start still answer within a second, nothing is persisted. With the
re-prepare under the lock, Stop timed out after 2 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@setavenger

Copy link
Copy Markdown
Owner Author

Merged into integration through #70 on 2026-10-11 (the user live-tested the combined stack first). GitHub's stacked-PR merge rebased the stack, so this PR's commits landed with new IDs and GitHub can't mark it merged itself. The code is identical: integration now has the same tree as the tested branch friglet-deslop-live-test (08bf11d), which contains this PR's head. Closing as merged via #70.

@setavenger setavenger closed this Oct 11, 2026
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