Repository navigation
fix(app): close promptly while a network switch is in progress - #1162
Draft
Claudius-Maginificent wants to merge 5 commits into
Draft
Claudius-Maginificent wants to merge 5 commits into
Claudius-Maginificent wants to merge 5 commits into
Conversation
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>
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. Comment |
Collaborator
|
🕓 Review not started yet because this PR is a draft.
Commit 3dbe287. Normal review starts when eligible; priority review starts as soon as a slot is available. |
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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WunYYhY7P2FJqHVgkHiXtT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
BackendTaskruns on a blocking thread (spawn_blocking+block_on,app/task_dispatch.rs).run_switch_networkchecked the TaskManager's cancellation token only around the SPV start. It did not check it aroundAppContext::neworensure_wallet_backend, which can take tens of seconds on a first visit.TaskManager::shutdown_asynctherefore waited out both of its 10 s phases (utils/tasks.rs). Shutdown then reportedBackendTasksTimedOutand skippedWalletBackend::shutdown.What changed
run_switch_networkraces the test gate against the exit token and checks the token afterAppContext::newand after wiring the wallet backend. When the app is closing, it returnsTaskManagerShuttingDown, which the shutdown drains and never shows to the user. The token fires only at app shutdown, so normal switching is unaffected.WalletBackend::newraces its initialisation against the token. The per-wallet bootstrap stops between wallets, never inside one, becauseregister_walletdoes the only durable write.PlatformWalletManager::newon, which spawns the wallet-event adapter, every teardown goes throughshutdown_bounded(): forget secrets, then platform-wallet's ownshutdown()(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.main.rsbounds the runtime shutdown withshutdown_timeout(5 s). A windowless process can no longer hold the secret-vault lock and block a relaunch. AppState and database teardown run insideblock_on, before this limit starts.rs-platform-wallet-storagesecrets/file). Wallet metadata lives in SQLite.finish_shutdown_after_tasksstill skipsWalletBackend::shutdownafter 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_persistorandcreate_wallet_from_seed_bytestake no cancellation token, so the fix usesselect!with the boundedshutdown().load_from_persistoronly reads.Droponly cancels the adapter;shutdown()is the lossless drain, so the fix uses it.Testing
closing_the_app_during_a_pending_switch_does_not_wait_for_it:on_exitwith a pending switch took 20.0 s before the fix and about 11 ms after (the test's limit is 5 s).a_build_interrupted_by_exit_stops_and_releases_its_storage: failed before the fix, passes after.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, thatshutdown_boundedcompletes 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_teardownalso checks that the post-SPV teardown ranshutdown_boundedonce.migration_gate(24),network_chooser(6),switch_network(3),wallet_backend::(485) andcontext::wallet_lifecycle(150) pass. Clippy and fmt are clean.🤖 Generated with Claude Code
🤖 Co-authored by Claudius the Magnificent AI Agent