mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:08:38 +00:00
wallet: fix crash when external signer wallet creation fails #36767
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36767
Loading…
Reference in a new issue
No description provided.
Delete branch "u3-createwallet-signer-error"
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?
When testing a hardware wallet integration, I found that when attempting to create a wallet with the wrong app open on the hardware wallet, the GUI crashes instead of showing an error message. After restarting, when attempting to create a wallet with the same name again, it fails with "Database already exists". Over RPC, there's no crash, but
createwalletreturns error code -1 and leaves the same files and directories behind.This happens when setting up an external-signer wallet. When the signer fails, setting up the wallet throws
std::runtime_error, andCreateWallet()doesn't catch it. The GUI's worker thread has no handler, so the uncaught exception ends instd::terminate, and RPC's catch-all turns it into error code -1. The directory andwallet.datare created before setup and never removed when setup fails.This uncaught exception has been encountered previously in bitcoin-core/gui#395, and bitcoin-core/gui#396 stopped the dialog from enabling the "External signer" option, but didn't handle the exception.
Commit 1 catches the exception as
LoadWalletInternal()does. The GUI shows an error dialog, and RPC returns error code -4, with the message starting with "Wallet creation failed."Commit 2 ensures that on failure, it removes only what was created. It checks what exists with
fs::symlink_status(), asMakeDatabase()does, so awallet.datsymlink is kept even if its target doesn't exist, and it logs a warning when a removal fails. The cleanup also runs for failures that don't throw, e.g. an invalid-addresstype.Commit 3 adds a release note.
How to reproduce:
Then click File, Create Wallet, Create. "External signer" is already ticked and the name is pre-filled as
trezor_t.On master:
terminate called after throwing an instance of 'std::runtime_error', the process aborts, andtrezor_t/wallet.datis left behind.With the PR: a "Create wallet failed" dialog appears, a retry shows the same dialog, and nothing is left on disk.
The tests
CreateWalletSignerFailureandCreateWalletInvalidArgsinwallet_testscreate wallets through the wallet loader, which is the same call the GUI makes.wallet_signer.pynow expects -4, and checks that a failed creation leaves nothing behind, that a retry fails the same way, and that a directory, files or a danglingwallet.datsymlink that already existed are kept; in the symlink case the database created at the link's target stays too, and the check is skipped on Windows, as inwallet_multiwallet.py. An emptywallet.datthat already existed is initialized by the attempt and kept, so the name is taken afterwards, as before this PR.You can see the tests fail without the fix:
🟠 Minor · Failed-creation cleanup can unlink a concurrently opened wallet
wallet.cpp:399-414 unlinks wallet.dat based on a pre-creation snapshot, and CreateWallet takes no name reservation (LoadWallet's g_loading_wallet_set covers loads only). On POSIX, a same-name loadwallet or createwallet that opens the database after the failed attempt closes it loses its file. The window is narrow and needs trusted clients.
Location:
src/wallet/wallet.cpp:391(head).Review coverage was incomplete.
Reviewed:
edc911450f42· Full review report · Previous reportspermalink
cc @Sjors
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.
Conflicts
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Rebased on master, no changes to the commits. The "test ancestor commits" job merges master into each commit before building, so with the old workflow file in this branch it configured master's code with clang 18 and failed the clang 19 check that #36036 added; the rebase picks up the runner change from that PR.
Pushed changes for the points raised in the automated Ralph review linked above, and added a release note.
fs::symlink_status(), asMakeDatabase()does. Awallet.datsymlink whose target doesn't exist counted as absent before, so the cleanup deleted the link and left the new database at its target; now both are kept.wallet_signer.pycovers this, skipped on Windows like the symlink checks inwallet_multiwallet.py.wallet.datthat already existed is initialized by the failed attempt and kept, so the name is taken afterwards, as before.doc/release-notes-36767.md. The "fail without the fix" snippet in the description is nowHEAD~3.Not changed: the first finding, that a
loadwalletof the same name (or of a symlink alias) could open the database between its close and the removal.RestoreWallet()has the same window, and on master the partial database is left behind permanently.Catching the
CWallet::CreateNewexception makes sense, but I think it's better to catch it inside that function.I think the issue is more broad than that? When looking at the external signer code, it seem it mixes error handling via results and exceptions, but the exceptions are never handled? E.g. the
enumeratesignersRPC has a catch, but the other RPCs do not. Also the PSBT flows are inconsistent, which seems like the GUI would crash there as well?So for the RPC I created this patch, not saying it should be taken, but I wanted to highlight the general issues.
Also @zerog975 : LLM output is not accepted if the author can not properly explain, test or could have written the change themselves. Also, LLM generated content is not allowed in comments/communications. See https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md