mirror of
https://github.com/bitcoin-core/gui-qml.git
synced 2026-12-02 08:03:39 +00:00
Move startup and shutdown work off the GUI thread #980
No reviewers
Labels
No labels
Android
Bug
Build system
Design suggestion
Doc
Needs design review
Needs rebase
Stage
UX
Up for grabs
Upstream
Wallet
duplicate
enhancement
good first issue
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin-core/gui-qml!980
Loading…
Reference in a new issue
No description provided.
Delete branch "pr/qt6-async-01-lifecycle"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Startup and shutdown can stop event processing while waiting for backend locks, storage or workers. Run initialization and interruption on owned workers. Stop query producers and drain accepted work before releasing Core, while keeping the GUI event loop available.
Add shared worker queues and asynchronous thread retirement. Adapt existing wallet workers so this PR can shut down safely before the later wallet feature changes are applied.
Regression tests cover blocked initialization, worker retirement, discarded results and wallet shutdown.
Fixes #788
Fixes #959
Doing Cmd+Q can hang onboarding and startup indefinitely
[Assisted by codex]
Cmd+Q or Dock Quit during onboarding exits its nested event loop, but the new final drain repeatedly calls loop.exec() after Qt has set its application quit flag. These calls return immediately, so the queued drain completion never runs and the GUI spins indefinitely. RunBackendStage has the same problem at backendstage.h:33, even after its worker finishes. Reproduced using native macOS termination with the exact PR executor code. Defer application Quit throughout bootstrap and draining, or run these stages within the main application event loop. The existing window-close test does not exercise native Quit.
Wallet notification cleanup can deadlock migration rollback
[Codex assisted]
Shutdown queues destruction of pending notification-owned wallet interfaces behind the active controller command. Migration rollback can itself be waiting for those interfaces to disappear: Core calls
WaitForDeleteWalletwhen a watch-only wallet loaded successfully but a later solvables-wallet load failed. Meanwhile consumeWalletNotifications stops consuming after shutdown starts. This creates a circular wait and prevents walletsDrained. A controlled loader test reproduced it; releasing pending interfaces through an independent executor made the test pass. Retire these references independently of the migration queue. The real Core rollback path was traced, but a real migration failure was not injected.ACK d2f4efd39b7a7eab2e521d5b69b006d6e83a687b.
Both the startup hang and deadlocks are resolved.
Im not liking the async join class and trying to clean this up a bit but it should be good to go very shortly
commenting that this was addressed starting in 89d613f6d3ae with the quit handler, with further startup-dialog handling in 858fc91d0125.
commenting that this is addressed in 89d613f6d3ae by retiring pending wallet notifications on a separate executor, independently of the migration command.
Just some thoughts if we keep the async join here: could the worker notify the GUI when it's done instead of polling every 1 ms? Would also be nice to keep the ownership and drain tracking in one place, so it's easier to follow when everything has actually finished.
Yeah the polling was the first thing to clean up. It should be using an event instead.
Can see if the worker registry can make it clear
This introduces a reset regression on macOS. The window settings model still reads and writes through default QSettings, which uses the organization domain,
bitcoincore.org. This constructor explicitly usesBitcoinCore, so we're clearing a different preferences store.Tested on macOS with Qt 6.9:
regtest=1in bitcoin.conf, without passing-regteston the command line.-resetguisettings, complete onboarding, and check Window behavior.The base (
a91f4b4774f2) clears both preferences; this head leaves both enabled. Passing-regtestexplicitly hides the issue because the early reset already clears the right network's settings. With config-only network selection, we depend on this later reset.This constructor also forces
NativeFormat, ignoring the format and redirected location used by-test-settings-dir. That part isn't macOS-specific. I separately reproduced the organization mismatch with native macOS settings, so the reset failure isn't just a test-harness artifact.Moving this work onto the worker makes sense. Could we preserve Qt's effective settings identity and only supply the resolved network's application name? Something like:
I built and tested this change with the same GUI reproduction, and both preferences reset correctly. It keeps the work on the worker and lets Qt handle the platform differences. Would be good to add coverage for the config-only network case too.
Tested ACK f4a4cc4a774eaf2d97294cd3154185000939a9b4Rechecked the reset regression and startup/shutdown paths on macOS with Qt 6.9. My concerns are addressed. The GUI stays responsive during slow backend work, and I didn’t see a meaningful performance regression against the base.
Edit: withdrawing ack here as I’ve found some new issues
I tried the fatal-error path after shutdown had already started, and the dialog opens with its Shut down button disabled.
Reproduced on macOS with Qt 6.9 by injecting an exception just before appShutdown(). The C++ shutdown path disables QQuickWindow::contentItem(), but this re-enables ApplicationWindow.contentItem. Those are different items, so the outer one stays disabled. Re-enabling the same item from C++ made the button work.
The QML test misses this because it disables the inner item too. I’d have the regression test exercise the actual C++ shutdown path so it catches that difference.
std::exit() still runs static destructors, which seems to defeat the intent of skipping cleanup after a fatal exception.
I injected an exception after real Core initialization, with RPC enabled, then acknowledged the fatal dialog. On macOS/Qt 6.9 with a debug build, I got a segfault during thread-pool teardown/logging in one run and the HTTP server’s joinable-thread assertion in another.
Changing this to std::_Exit(EXIT_FAILURE) made the same checks exit with status 1. If we can’t safely continue or drain Core here, that seems like the appropriate primitive.
The current mock-based fatal-exit tests don’t exercise this static teardown. Would be good to cover the real initialized node too.
Minor: this message doesn’t seem to reach the wallet selector.
I held an Activity read/fee preview open, closed the wallet, then tried opening it again before the work finished. The row looks closed and is still clickable, but clicking it gives no feedback. Retrying after cleanup works.
The selector uses the wallet-load-state signals for its error display, while this only sets walletLoadError. Emitting the existing load-error state made the failure visible in a control build.
I wouldn’t block on this, but showing a closing state—or surfacing this message—would make it clear why the wallet isn’t opening yet.
Updated to a592a2122fa07725f112a29c5fd9fd3c1c2fcaf9 with fixes and regression coverage for all three recent findings:
QQuickWindow::contentItem()that the C++ shutdown path disables. The real GUI test enters that shutdown path and verifies the fatal button is enabled while normal navigation stays disabled.std::_Exit(EXIT_FAILURE)to bypass unsafe static teardown. Real initialized-node tests keep RPC/HTTP workers active and require exit status 1; anatexitsentinel also catches regressions in the child-process tests.WalletListModel, checks two failed retries, then verifies successful reopening and a cleared error after retirement.The coverage audit also found and fixed an initialization/shutdown ordering bug: a shutdown queued behind initialization must skip
appShutdown()if initialization throws. Tests cover both exception types, ordinary initialization failure, worker readiness failure, late wallet-command completion, and completion callbacks that destroy their executor.A pre-existing startup issue is also fixed: an already-onboarded profile selected by config now skips onboarding when the default directory is absent. This has six isolated GUI cases registered with CTest and twelve settings/command-line precedence cases.
do we need to have a timer for this?
comments shouldnt be necessary, lets remove and see if readability needs to be improved or if these comments were just excess.
drained what? lets make this clearer
Renamed to
workDrainedin 21b5111d8c8c1c81292259b2ce09d6503df3f5de, including both emission paths, the shutdown coordinator connection, and the existing typed/runtime signal spies. The node, executor and shutdown tests plus real GUI startup/shutdown with IPC pass in normal, ASan/LSan/UBSan/integer and TSan builds. Pinned clang-tidy passed; supplemental lint has no new findings.The explanatory shutdown comment has been removed from
NodeModel::requestShutdown()in the current head (21b5111d8c8c1c81292259b2ce09d6503df3f5de). Closing this as addressed.I'm still reviewing this PR, but I had a question in the meantime: does it also fix #788, or am I missing something?
From the crash report, it looks like the app exits through a native quit (Cmd+Q / Dock Quit) without calling
appShutdown(), soNodeContext::background_init_threadis still joinable when~NodeContextruns andstd::terminateis called. WithQmlQuitHandlerroutingQEvent::Quitinto the shutdown coordinator, every exit path should now go throughappShutdown()first.If that's right, it might be worth adding "Fixes #788" to the description.
@AndreaDiazCorreia Added
Fixes #788to the PR description.This dropped the
PrintExceptionContinuecall. When I trigger the test fatal exception, the popup shows it but nothing ends up indebug.log. Was that on purpose?The error callback doesn't keep the pending work like the success one does. If the first history load fails right after a new transaction notification, I only see that one transaction afterwards, with no error shown. Should the error path keep the work too?
tACK 4a7a3148
All issues are addressed. I agree it can be merged.
Please rebase.
rebased
re-ACK
beb55f04e3