Review report

bitcoin/bitcoin: PR #34193

1 major finding · 3 minor findings · 1 suggestion

Earlier concerns remain unresolved; see the Run archive for their assessments.

Findings

Link

[major] · A failed final copy removes the wallet without restoring its backup

If the final SQLite copy fails after the original BDB file is removed, migration deletes the staged database and returns before automatic restoration, leaving the wallet unusable until its backup is restored manually.

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

Details and suggested fix

MigrateToSQLite unlinks origin_db_path before copying the staged SQLite database into place. A destination error such as ENOSPC, EDQUOT or EIO can therefore occur after the original is gone, especially when an external wallet resides on a different filesystem from the staging directory. The copy-error branch calls fail(), which deletes the staged database and returns false. MigrateLegacyToDescriptor immediately returns that error, bypassing its cleanup and RestoreWallet block. The destination can be absent or partial; the earlier .legacy.bak still preserves the keys, but recovery requires manual restoration. No production occurrence of this final-copy failure was identified. Keep the staged database and distinguish post-unlink failures so the caller cleans up any partial destination and restores the backup instead of taking the early return.


Link

[minor] · A failed final database reopen aborts the process

After installing the SQLite file, an ordinary reopen or verification failure reaches Assert and aborts the node or GUI instead of returning a recoverable migration error.

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

Details and suggested fix

MakeDatabase(dst_wallet_dir, ...) performs a fresh SQLite open with verification enabled. Its writability probe creates another file, so a successful copy does not guarantee that reopening succeeds under inode or space exhaustion. Open and verification failures can return nullptr, which Assert turns into a process abort rather than an exception handled by the migration catch. The original BDB and staged SQLite files have already been removed, leaving SQLite storage with legacy records and requiring backup restoration before retrying migration. Base also asserted initial database creation, but this adds a separate fallible reopen after installation. No production occurrence of this reopen failure was identified. Handle a null result through migration rollback and retain a recoverable copy until reopening succeeds.


Link

[minor] · Failure cleanup removes the temporary directory before closing SQLite

Write or original-file removal failures run temporary cleanup with the SQLite connection still open, so a retained rollback journal can prevent directory removal and leave artifacts after migration fails.

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

Details and suggested fix

The write-failure and original-removal-failure branches call fail() while new_db still owns the connection. remove_tmp() deletes only wallet.dat and the directory, ignores both errors, and never retries after database destruction. With exclusive locking and the default rollback journal mode, SQLite retains the journal until close. On POSIX, that journal can prevent directory removal even after the main file is unlinked, leaving an empty temporary directory once the connection closes. Repeated attempts can accumulate these directories. Exact Windows leftovers depend on deletion-sharing behavior that was not verified. Close or reset new_db before cleanup, then remove its database files and temporary directory.


Link

[minor] · Concurrent migrations can delete another migration's staged database

Two concurrent migrations with the same wallet leaf name and timestamp can pass the temporary-path check, after which the failed creator deletes the other migration's staged database.

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

Details and suggested fix

A trusted RPC client can concurrently migrate distinct wallets such as a/w and b/w; their wallet locks do not serialize the operations. Both calls can select w_sqlite_<seconds>.tmp and pass the existence check before either creates it. Once one creates the database, the other's MakeDatabase can return FAILED_ALREADY_EXISTS, but fail() still removes that shared wallet.dat. On POSIX, the first connection can continue using the unlinked file, then remove its original and fail the final pathname-based copy without automatic restoration. Same-second backup-name collisions already existed, but this adds a separate staged-file ownership race. No such concurrent production use was identified. Reserve the temporary directory atomically and run cleanup only after this invocation has acquired ownership.


Optional improvements

Link

[suggestion] · Permission hint assumes errno numbering for filesystem errors

Comparing err_db.value() with errno constants can omit the permission hint when filesystem errors use another category's numbering; std::errc comparisons preserve the intended portable classification.

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

Details and suggested fix

fs::remove supplies a std::error_code, whose numeric value is meaningful only within its category. Comparing .value() with EACCES, EPERM and EROFS assumes errno numbering, so an implementation reporting Win32 error values can omit the permission hint for an access-denied failure. Compare err_db directly against std::errc::permission_denied, std::errc::operation_not_permitted and std::errc::read_only_file_system instead. Those comparisons use portable error conditions and preserve POSIX behavior. The MinGW filesystem error category used for release builds was not verified.


Coverage details