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 createwallet returns 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, and CreateWallet() doesn't catch it. The GUI's worker thread has no handler, so the uncaught exception ends in std::terminate, and RPC's catch-all turns it into error code -1. The directory and wallet.dat are 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(), as MakeDatabase() does, so a wallet.dat symlink 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:
cmake -B build -DBUILD_GUI=ON && cmake --build build
build/bin/bitcoin-qt -regtest -datadir=$(mktemp -d) -signer="python3 test/functional/mocks/invalid_signer.py"
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, and trezor_t/wallet.dat is 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 CreateWalletSignerFailure and CreateWalletInvalidArgs in wallet_tests create wallets through the wallet loader, which is the same call the GUI makes. wallet_signer.py now 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 dangling wallet.dat symlink 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 in wallet_multiwallet.py. An empty wallet.dat that 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:
git checkout HEAD~3 -- src/wallet/wallet.cpp
cmake --build build
build/bin/test_bitcoin --run_test=wallet_tests/CreateWalletSignerFailure,CreateWalletInvalidArgs
build/test/functional/wallet_signer.py