wallet: make migration more robust against failures

bitcoin/bitcoin: PR #34193

2 minor findings · 3 suggestions

Findings

Link

[minor] · Pre-rename refusal unnecessarily deletes intact wallet.dat

For directory and unnamed wallets, a pre-rename MoveDatabase refusal deletes and restores intact wallet.dat, adding an avoidable recovery dependency to the new staged workflow, though base already deleted the original during migration.

Location: src/wallet/wallet.cpp:3899 (head).

Details and suggested fix

A leftover wallet.dat-journal makes MoveDatabase return before renaming anything. For directory and unnamed wallets, Erase() was skipped, so m_database still identifies the intact BerkeleyRO file. Throwing here leaves success false, and rollback removes that original file before RestoreWallet copies the backup back. If copying fails or the process stops between removal and copying, the retained .legacy.bak requires manual restoration. Have MoveDatabase report whether replacement occurred, and return remove_tmp_and_fail(...) for failures before replacement when the original was not erased. Keep the throwing rollback path after plain-file erasure or a completed rename.


Link

[minor] · Build exceptions leave temporary databases in listwalletdir

An exception while building the temporary SQLite database can leave a directory that listwalletdir reports as a wallet; no production occurrence was identified.

Location: src/wallet/wallet.cpp:3872 (head).

Details and suggested fix

RunWithinTxn calls MakeBatch(), whose SQLite statement preparation can throw instead of returning false. That bypasses remove_tmp_and_fail; destroying new_db closes the connection but does not erase its files. Outer rollback still tracks the original database, not the temporary path. Once creation has written the application ID, recursive ListDatabases recognizes the leftover under the wallet directory as a SQLite wallet. A complete legacy-record copy is nevertheless rejected on loading because it lacks descriptor-wallet flags. Add an exception-safe cleanup guard for the temporary database and directory, and dismiss it after the successful move, so caught build exceptions receive the same cleanup as false returns.


Optional improvements

Link

[suggestion] · Temporary-directory cleanup lacks an atomic ownership check

Concurrent trusted migratewallet calls selecting the same temporary path in one second can delete another call's staged database because the existence check and creation are separate.

Location: src/wallet/wallet.cpp:3835 (head).

Details and suggested fix

Both calls can pass symlink_status before either creates the directory. If one then creates the database, the other's MakeDatabase can refuse the existing database, yet remove_tmp_and_fail removes every entry under their shared path. The same wallet produces this collision, as do the unnamed wallet and a top-level plain-file wallet named default_wallet. The ignored inspection error also fails to establish ownership, although persistent access denial alone does not demonstrate deletion. Reserve the path with fs::create_directory(tmp_wallet_path, ec) and return without cleanup unless this call successfully created it. MakeDatabase accepts that existing empty directory with require_create.


Link

[suggestion] · Journal tests miss file preservation and leave mocktime set

The journal tests pass after deleting and restoring wallet.dat, while test_stale_journal leaves the node's mocktime set when its expected RPC exception bypasses the helper's reset.

Location: test/functional/wallet_migration.py:246 (head).

Details and suggested fix

The journal-survival, temporary-directory, and BDB-magic assertions all pass after rollback replaces the original file with a backup copy. After copying the fixture, hard-link wallet.dat and assert os.path.samefile after refusal to distinguish preservation from delete-and-copy. Also, migrate_and_get_rpc resets mocktime only after migratewallet returns successfully. This expected failure skips that reset, so the following test inherits frozen time. Copy the fixture explicitly and call migratewallet directly here, as test_db_named_as_journal does, rather than using the success-only helper.


Link

[suggestion] · Caught migration exceptions can retain success = true

An allocation failure in GetAllScriptPubKeyMans() after DoMigration() succeeds leaves success true, so the new catch can continue through successful completion instead of selecting rollback.

Location: src/wallet/wallet.cpp:4362 (head).

Details and suggested fix

GetAllScriptPubKeyMans() builds a std::set after the assignment to success, so an insertion can throw std::bad_alloc in this window. The catch sets only error, and the following branch still uses the earlier successful result. Set success = false in the catch to make caught exceptions consistently select the restore path. The current trigger is allocation failure after migration has committed, not a normal migration error or evidence of partially migrated data.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Unresolved disagreements

Historical participants differed over whether tightly bounded deletion was enough or failed migration artifacts should never be deleted at all.

In the preceding unnamed-wallet fix, willcl-ark preferred retaining failed artifacts over risking deletion, and darosior supported exploring a never-delete approach while separating that broader work from the urgent fix. The author subsequently implemented file-scoped deletion and existing-directory restoration. This records the preservation-versus-cleanup tradeoff, but the inspected discussion does not resolve the broader objection to deleting migration artifacts.

Source 1 · Source 2 · Source 3