Skip to content

wallet: Handle or explicitly ignore WalletBatch write failures - #35998

Open
achow101 wants to merge 10 commits into
bitcoin:masterfrom
achow101:walletdb-nodiscard
Open

achow101 wants to merge 10 commits into
bitcoin:masterfrom
achow101:walletdb-nodiscard

Conversation

@achow101

Copy link
Copy Markdown
Member

A common theme in the wallet is that many database write failures are ignored, when they should probably be handled, or at least documented that a failure is being ignored. This PR marks all database functions in WalletBatch as [[nodiscard]] so that a compiler will tell us if a return value is implicitly ignored. All of the calls to those functions are updated to either deal with failure, or explicitly ignore it with a rationale.

Based on #35752 which handles failures for the encryption functions.

@DrahtBot

DrahtBot commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
Concept ACK l0rinc, rkrux, jeanpablojp, polespinasa

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36897 (wallet, migration: Contain all legacy wallet and migration related things in its own subdirectory, namespace, and classes by achow101)
  • #36823 (wallet, migration: Skip legacy wallet records rather than erasing them, and drop unused record erase functions by achow101)
  • #36042 (build: Bump g++ minimum supported version to 13 by maflcko)
  • #36031 (wallet: Remove mapMasterKeys and enforce that only one encryption key can exist by achow101)
  • #35786 (wallet: drop spent parents redundant cache invalidation and notification by furszy)
  • #35444 (wallet: make descriptor SPKM mutex non-recursive by w0xlt)
  • #35302 (Silent Payments: Sending (take 2) by Eunovo)
  • #27865 (wallet: Track no-longer-spendable TXOs separately by achow101)

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.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/wallet/export.cpp: Unable to write the a tx record → Unable to write the tx record [the wording is grammatically broken]

2026-11-24 20:36:55

@l0rinc

l0rinc commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Concept ACK, thanks for taking over.
Please see the latest push in #35752 (comment), feel free to adjust it any way you like.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task Windows native, fuzz, VS: https://github.com/bitcoin/bitcoin/actions/runs/32908277972/job/97996971269
LLM reason (✨ experimental): CI failed because the fuzz target rpc crashed with exit code 3221225477 (Windows access violation / segfault).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@rkrux

rkrux commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Concept ACK f8b9da8 because it enforces the handling of database errors, thereby ensuring consistency between in-memory behaviour and databases behaviour.

Comment thread src/wallet/walletdb.cpp Outdated
// there will likely be write failures in the future. Better to stop loading the wallet
// and not let the user use it at this time.
pwallet->WalletLogPrintf("Error: Unable to update the wallet last client version");
return DBErrors::CORRUPT;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly, a mild issue like @jeanpablojp's disk full/permissions read-write restrictions reporting BErrors::CORRUPT may misguide the user giving then unnecessary tensions. For the others, this may fly, for loading the database IMHO, a log might probably help instead of locking them out with the error ...Wallet corrupted

Comment thread src/wallet/wallet.cpp Outdated
auto address{EncodeDestination(dest)};
if (entry.purpose) batch.WritePurpose(address, PurposeToString(*entry.purpose));
if (entry.label) batch.WriteName(address, *entry.label);
if (entry.purpose) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes a lot of sense to work with the all-or-nothing logic here. A purpose or name failure (less tragic) means that the more tragic ones (WriteAddressReceiveRequest and WriteAddressPreviouslySpent may also fail.

Return values for some writes can be ignored, if they fail, no harm
occurred.
RunWithinTxn calls TxnAbort, but if this fails, the WalletBatch will go
out of scope anyways, which will call destructors that reach
SQLiteBatch::Close, which will also call TxnAbort, and has more handling
of abort failures.
Handle write failures for WriteOrderPosNext and WriteTx
Mark WriteBestBlock as [[nodiscard]] and explicitly ignore errors, or
handle them.
All WalletBatch operations that interact with the database (read, write,
erase, transactions) have bool returns values that callers need to check
as database operations may fail.

@polespinasa polespinasa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super concept ACK

reviewed d0ecbdc

DecIndex();
}
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
(void)WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 8cc88b5 wallet: Explicitly mark ignored write failures

nit: I would probably add here also a comment saying that write failure is ok.

Comment thread src/wallet/walletdb.cpp
if (own_txn && !TxnBegin()) return false;
if (!WriteIC(encrypted_key, secret, /*fOverwrite=*/false) || !EraseIC(plaintext_key) || (own_txn && !TxnCommit())) {
if (own_txn) TxnAbort();
if (own_txn) (void)TxnAbort();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 401a4bd wallet: Ignore TxnAbort failure in RunWithinTxn

nit: I would also add the comment added for the other case here too :)

WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
if (!WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor)) {
DecIndex();
return util::Error{_("Error: Unable to update database with new address")};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in 52841e7 wallet: Handle descriptor update write failures

This needs changes on how the error propagates. RPCs such as getnewaddress would previously succeed and now fail with error code RPC_WALLET_KEYPOOL_RAN_OUT which does not seem accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants