Skip to content

wallet: a failed or interrupted database write leaves memory ahead of disk (descriptor import, transaction save) #8147

Open

Description

@PastaPastaPasta

Several wallet paths inherited from Bitcoin Core update the wallet in memory before the matching database write, or spread one logical change over several writes with no transaction. When a write fails (for example a full disk) or the process stops between two writes, memory and disk disagree and nothing repairs it. A retry in the same session then sees the in-memory state, skips the write and reports success.

None of this is new, and none of it is reachable from the network. It was raised in review of #8068, whose unattended DashPay receiving-chain imports make these paths run more often than a manual importdescriptors does. #8068 documents them as limits instead of working around them, so they are collected here.

The paths

  1. A descriptor key is kept in memory before it is written. DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB fills m_map_keys / m_map_crypted_keys (and the mnemonic maps) and then writes. If the write fails, a retry in the same session hits the "already have this key" early return, never writes the key, and the import succeeds. After a reload the descriptor has no private key.

  2. CWallet::AddWalletDescriptor is not atomic. It writes the private key, then the cache entries (TopUp), then the WALLETDESCRIPTOR record, each in its own batch. A failure or a stop after the key write leaves key or cache records with no descriptor record.

  3. DescriptorScriptPubKeyMan::TopUp can stop part way. It adds scripts to m_map_script_pub_keys index by index and throws when a cache write fails, leaving part of the range populated in memory. It also ignores the result of the final WriteDescriptor, so a grown range may never be saved. A rescan that then succeeds can miss payments to the unpopulated indexes, or find payments in a range that is not saved.

  4. CWallet::AddToWallet keeps a transaction in memory before it is written. It inserts into mapWallet, wtxOrdered and the UTXO set, then calls WriteTx. If that fails, AddToWalletIfInvolvingMe throws ("DB error adding transaction to wallet, write failed") and nothing is rolled back. A rescan retried in the same session finds the transaction already present and unchanged, writes nothing and reports success; after a reload the transaction is missing.

Effect

After a failed write the wallet has to be reloaded before the operation is retried, and a wallet restored from that file may need rescanblockchain to see a payment again. Funds are not at risk; the keys derive from the seed. Path 2 is the exception in severity: until the loader fix lands, the wallet file does not load at all.

Suggested order

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions