Keep wallet security, backup and recovery work off the GUI thread #967

Open
opened 2026-10-02 17:29:33 +00:00 by johnny9 · 2 comments
johnny9 commented 2026-10-02 17:29:33 +00:00 (Migrated from github.com)

Parent: #578. Extend or consolidate #625 for backup and #627 for password operations instead of creating duplicate work.

Setting or changing a wallet password and saving a backup can block the interface during key derivation, wallet locking or database I/O. Restore and migration also need safe preflight and completion handling, even when their main jobs already run in the background.

Cover encryptWallet(), changeWalletPassphrase(), backupWallet(), security-state refresh, unlock/action/relock, restore file checks, migration encryption checks and returned wallet metadata.

Run blocking work outside the GUI thread. Expose pending, success and failure states; prevent duplicate submission; and keep security getters cached. Perform temporary unlock and required relock with the protected action, including failure paths. Retain resources until work finishes and handle secret request data carefully.

Acceptance should cover delayed backup, wrong passwords, failed writes, duplicate clicks, restore/migration preflight, relock after failure and closing during an operation. Coordinate catalogue and close behavior with #577; message signing remains under #580.

Parent: #578. Extend or consolidate #625 for backup and #627 for password operations instead of creating duplicate work. Setting or changing a wallet password and saving a backup can block the interface during key derivation, wallet locking or database I/O. Restore and migration also need safe preflight and completion handling, even when their main jobs already run in the background. Cover `encryptWallet()`, `changeWalletPassphrase()`, `backupWallet()`, security-state refresh, unlock/action/relock, restore file checks, migration encryption checks and returned wallet metadata. Run blocking work outside the GUI thread. Expose pending, success and failure states; prevent duplicate submission; and keep security getters cached. Perform temporary unlock and required relock with the protected action, including failure paths. Retain resources until work finishes and handle secret request data carefully. Acceptance should cover delayed backup, wrong passwords, failed writes, duplicate clicks, restore/migration preflight, relock after failure and closing during an operation. Coordinate catalogue and close behavior with #577; message signing remains under #580.
jarolrod commented 2026-10-15 06:33:13 +00:00 (Migrated from github.com)

Found a migration case to add here: finishing one wallet’s migration can close another wallet’s password prompt and hide its failure.

Reproduced on native macOS / Qt 6.9.0, qt6 at d567e3eb2497, using disposable regtest wallets.

  1. With a regular wallet loaded in the GUI, have two unloaded legacy BDB wallets available in the picker: unencrypted A and encrypted B. A needs to take long enough to migrate to do the next steps. I created A with Bitcoin Core 28.0 and -keypool=20000.
  2. Select A and click Update wallet. The confirmation closes while migration continues.
  3. Before A finishes, select B, click Update wallet, enter an incorrect password and click Unlock and update.
  4. Wait for both attempts to finish.

A finishes and becomes selected, but B’s password dialog disappears too. B remains unmigrated and there’s no password error or retry prompt. Trying B alone with the same incorrect password leaves the dialog open with the error, as expected.

Selecting B clears the shared migration status. A’s success then clears pendingMigrationPath and closes both dialogs, so B’s failure gets ignored because the pending path is empty.

We should keep the active migration state intact and prevent a second migration from starting, or tie each completion to its own operation. Starting another wallet load shouldn’t reset that protection.

Found a migration case to add here: finishing one wallet’s migration can close another wallet’s password prompt and hide its failure. Reproduced on native macOS / Qt 6.9.0, `qt6` at [d567e3eb2497](https://github.com/bitcoin-core/gui-qml/commit/d567e3eb2497df3be5d63f4dc1f554b3bf51f59f), using disposable regtest wallets. 1. With a regular wallet loaded in the GUI, have two unloaded legacy BDB wallets available in the picker: unencrypted A and encrypted B. A needs to take long enough to migrate to do the next steps. I created A with Bitcoin Core 28.0 and `-keypool=20000`. 2. Select A and click **Update wallet**. The confirmation closes while migration continues. 3. Before A finishes, select B, click **Update wallet**, enter an incorrect password and click **Unlock and update**. 4. Wait for both attempts to finish. A finishes and becomes selected, but B’s password dialog disappears too. B remains unmigrated and there’s no password error or retry prompt. Trying B alone with the same incorrect password leaves the dialog open with the error, as expected. Selecting B clears the shared migration status. A’s success then clears `pendingMigrationPath` and closes both dialogs, so B’s failure gets ignored because the pending path is empty. We should keep the active migration state intact and prevent a second migration from starting, or tie each completion to its own operation. Starting another wallet load shouldn’t reset that protection.
jarolrod commented 2026-11-13 15:11:40 +00:00 (Migrated from github.com)

Two more results of the same root on the unencrypted route, at 2bd5a61e8304: nothing is gated while Update wallet runs, and clicking the same wallet again ends in a false "Failed to open wallet".

While the update runs, the picker rows, Add wallet and File → Add Wallet, Close Wallet, Backup Wallet and Exit stay enabled, and no row shows a loading state. An ordinary wallet load from the picker shows "Loading…" and disables the row, Add wallet and File → Add Wallet and Close Wallet. The update sets only walletMigrationInProgress, and the gates read walletLoadInProgress.

To reproduce the false failure, with a legacy wallet whose update takes several seconds:

  1. Click the wallet in the picker and click Update wallet.
  2. While it runs, open the picker and click the same wallet again. It is already listed as an ordinary closed wallet, so the click starts a load and not a second update prompt.

When the update ends, an alert says "Failed to open wallet / Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?" The wallet is open and selected, and listwallets has it. Its picker row stays red after OK, after reopening the picker and after clicking the row again. It cleared when another closed wallet was opened.

The second click's load is queued behind the update, and starting it resets the update's status while Core is still migrating:

github.com/bitcoin-core/gui-qml@2bd5a61e83/qml/walletqmlcontroller.cpp (L1182-L1185)

Seen in 4 of 4 runs at 2bd5a61e8304 where the second click landed during the update, 2 offscreen and 2 native, with the click 2 to 6 s in, and once offscreen at eed1f426fcb6. In one more native run the click came after the update had finished and nothing went wrong.

macOS arm64 / Qt 6.9.0, isolated regtest. The update lasted about 9 to 14 s on a hand-built legacy wallet with 20,001 keys. With a 15,000-key wallet written by Bitcoin Core 29.0 the update took about 10.5 s and the second click gave the same alert, 2 of 2, offscreen. Both clicks went through the test bridge; I have not made the second click by hand.

Two more results of the same root on the unencrypted route, at `2bd5a61e8304`: nothing is gated while **Update wallet** runs, and clicking the same wallet again ends in a false "Failed to open wallet". While the update runs, the picker rows, **Add wallet** and File → Add Wallet, Close Wallet, Backup Wallet and Exit stay enabled, and no row shows a loading state. An ordinary wallet load from the picker shows "Loading…" and disables the row, **Add wallet** and File → Add Wallet and Close Wallet. The update sets only `walletMigrationInProgress`, and the gates read `walletLoadInProgress`. To reproduce the false failure, with a legacy wallet whose update takes several seconds: 1. Click the wallet in the picker and click **Update wallet**. 2. While it runs, open the picker and click the same wallet again. It is already listed as an ordinary closed wallet, so the click starts a load and not a second update prompt. When the update ends, an alert says "Failed to open wallet / Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of Bitcoin Core?" The wallet is open and selected, and `listwallets` has it. Its picker row stays red after OK, after reopening the picker and after clicking the row again. It cleared when another closed wallet was opened. The second click's load is queued behind the update, and starting it resets the update's status while Core is still migrating: https://github.com/bitcoin-core/gui-qml/blob/2bd5a61e8304d5143e32f1ce977a2de2aceb6413/qml/walletqmlcontroller.cpp#L1182-L1185 Seen in 4 of 4 runs at `2bd5a61e8304` where the second click landed during the update, 2 offscreen and 2 native, with the click 2 to 6 s in, and once offscreen at `eed1f426fcb6`. In one more native run the click came after the update had finished and nothing went wrong. macOS arm64 / Qt 6.9.0, isolated regtest. The update lasted about 9 to 14 s on a hand-built legacy wallet with 20,001 keys. With a 15,000-key wallet written by Bitcoin Core 29.0 the update took about 10.5 s and the second click gave the same alert, 2 of 2, offscreen. Both clicks went through the test bridge; I have not made the second click by hand.
Sign in to join this conversation.
No description provided.