Skip to content

fix(app): close promptly while a network switch is in progress - #1162

Draft
Claudius-Maginificent wants to merge 5 commits into
v1.0-devfrom
fix/shutdown-cancels-network-switch
Draft

Claudius-Maginificent wants to merge 5 commits into
v1.0-devfrom
fix/shutdown-cancels-network-switch

Conversation

@Claudius-Maginificent

@Claudius-Maginificent Claudius-Maginificent commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR: If you close Dash Evo Tool while it is switching networks, it now exits right away. Before, the window stayed open for about 20 seconds first.

User story

As an Everyday User, when I close the app in the middle of a network switch, I want it to close promptly so I can open it again right away.

Scenario

Base flow

Switch to a network you haven't used before, then close the window while "Switching to Testnet…" is still showing.

Actual behavior

The window shows "Shutting down background tasks — please wait…" for about 20 seconds before it closes. If the app was closing through its fallback path, the window does not respond at all during that time. Even after the window closes, the process can keep running.

Expected behavior

The app closes within a moment, and a new start works right away.

Detailed discussion

Root cause (DET, not upstream)

  • Every BackendTask runs on a blocking thread (spawn_blocking + block_on, app/task_dispatch.rs).
  • run_switch_network checked the TaskManager's cancellation token only around the SPV start. It did not check it around AppContext::new or ensure_wallet_backend, which can take tens of seconds on a first visit.
  • On close, TaskManager::shutdown_async therefore waited out both of its 10 s phases (utils/tasks.rs). Shutdown then reported BackendTasksTimedOut and skipped WalletBackend::shutdown.
  • This was already the case before fix(network): block the app while a network switch is in progress #1154.

What changed

  • The switch stops when the app closes. run_switch_network races the test gate against the exit token and checks the token after AppContext::new and after wiring the wallet backend. When the app is closing, it returns TaskManagerShuttingDown, which the shutdown drains and never shows to the user. The token fires only at app shutdown, so normal switching is unaffected.
  • Wallet backend construction stops too. WalletBackend::new races its initialisation against the token. The per-wallet bootstrap stops between wallets, never inside one, because register_wallet does the only durable write.
  • No half-built backend is simply dropped. From PlatformWalletManager::new on, which spawns the wallet-event adapter, every teardown goes through shutdown_bounded(): forget secrets, then platform-wallet's own shutdown() (stops SPV, drains coordinators, joins the adapter), bounded at 5 s. This applies to an interrupted build, a backend that was already wired, and one whose SPV had already started.
  • The finished process exits. main.rs bounds the runtime shutdown with shutdown_timeout(5 s). A windowless process can no longer hold the secret-vault lock and block a relaunch. AppState and database teardown run inside block_on, before this limit starts.
  • Vault writes are atomic, so neither the exit nor the 5 s timeout can tear one. The secret store writes a temp file in the same directory, then fsync, rename and a parent-directory fsync (rs-platform-wallet-storage secrets/file). Wallet metadata lives in SQLite.
  • Kept on purpose. finish_shutdown_after_tasks still skips WalletBackend::shutdown after a task timeout (fix(shutdown): stop wallet backend on GUI close and clear stale shutdown-channel log #905), because other long backend tasks can still overrun.

Cancel safety (pinned platform-wallet 575d961)

  • PlatformWalletManager::new, configure_shielded, load_from_persistor and create_wallet_from_seed_bytes take no cancellation token, so the fix uses select! with the bounded shutdown().
  • Opening the persister and upgrading it run synchronously, inside an IMMEDIATE transaction.
  • load_from_persistor only reads.
  • Upstream Drop only cancels the adapter; shutdown() is the lossless drain, so the fix uses it.

Testing

  • Kittest closing_the_app_during_a_pending_switch_does_not_wait_for_it: on_exit with a pending switch took 20.0 s before the fix and about 11 ms after (the test's limit is 5 s).
  • Unit test a_build_interrupted_by_exit_stops_and_releases_its_storage: failed before the fix, passes after.
  • Unit test a_build_interrupted_mid_initialization_stops_and_shuts_down: a hook pauses the build right after the platform-wallet manager exists, then the app exits. The test checks that the build stops, that shutdown_bounded completes once, and that the wallet database can be reopened. A mutation that drops the manager instead turns it red.
  • switch_network_registers_wired_backend_before_cancellation_teardown also checks that the post-SPV teardown ran shutdown_bounded once.
  • migration_gate (24), network_chooser (6), switch_network (3), wallet_backend:: (485) and context::wallet_lifecycle (150) pass. Clippy and fmt are clean.

🤖 Generated with Claude Code

🤖 Co-authored by Claudius the Magnificent AI Agent

lklimek and others added 3 commits October 28, 2026 15:30
Closing the app while a first-visit network switch was still building
the new network's context made the exit wait out the task manager's
whole shutdown budget, two 10 s phases, before the window could close.
The switch task runs on a blocking thread and never looked at the
shutdown signal while building the context and wiring the wallet
backend.

The build now races the task manager's cancellation token. On exit the
switch stops at its next await, shuts down a backend that was already
wired, and returns TaskManagerShuttingDown, which shutdown drains
without showing. The token is cancelled only at exit, storage opens and
upgrades run synchronously between awaits, the wallet load is read-only,
and the upstream manager's Drop stops its event adapter, so nothing
abandoned here can trip a later start.

After the window closes, the runtime now shuts down with a 5 s bound
instead of waiting without limit for blocking work the app's shutdown
already gave up on. SQLite rolls back any transaction that process exit
cuts short.

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

When the app closed during a network switch, the switch's wallet backend
build was abandoned by dropping its future. Its upstream wallet manager
was then dropped without shutdown, and the upstream Drop only cancels
the wallet-event adapter, so buffered events can be lost. Upstream
documents `PlatformWalletManager::shutdown` as the lossless teardown.

`WalletBackend::new` now races its own initialization (shielded store
setup, sidecar hydration, persisted-wallet load) against the app's exit
signal. On exit, or on any other failure after the manager exists, it
tears the manager down through `WalletBackend::shutdown`, bounded at 5 s.
None of those steps writes the wallet database itself, so stopping at
an await is safe. The per-wallet bootstrap, which does write upstream
registration and key bindings, now stops between wallets and never
inside one. The network switch no longer drops the build; it checks the
exit signal between its steps and shuts down a backend it had wired.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A switch cancelled after it started chain sync shut its wallet backend
down without a time limit. It now uses the same bounded upstream
teardown as an interrupted build, renamed `shutdown_bounded` because it
also covers started backends. A teardown that overruns the bound is
reported. The app's exit drain joins whatever is left, because the
context was already registered.

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

coderabbitai Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thepastaclaw

thepastaclaw commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

🕓 Review not started yet because this PR is a draft.

  • Request normal review — click when the PR is ready for review.
  • Request priority review — click to move this review to the front of the queue.

Commit 3dbe287. Normal review starts when eligible; priority review starts as soon as a slot is available.

lklimek and others added 2 commits October 28, 2026 16:33
The earlier test cancelled before the build started, so the build's
initialization was never polled. A test-only hook now pauses the
initialization once the upstream manager exists, and a new test closes
the app while the build is paused there. It checks that the build stops,
that the manager went through the bounded upstream shutdown (counted by
the hook), and that the wallet database is released. The pre-start test
and the switch's post-chain-sync teardown test now check that count too.
A mutation that drops the manager instead of shutting it down fails both
build tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Claudius-Maginificent Claudius-Maginificent added priority: medium Medium priority priority: high High priority — address soon and removed priority: medium Medium priority labels Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority: high High priority — address soon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants