Repository navigation
Description
This is from a ci failure, but I think it can happen in reality as well. It would be trivial to fix the test, but maybe this should be fixed in the wallet?
The basic idea is that some block connection events on the scheduler thread may be processed while the wallet is failing to load due to a pre-prune locator. Thus, on the next load, the corrupt locator is used and the rescan is silently skipped.
From https://github.com/bitcoin/bitcoin/actions/runs/42834240037/job/129901508274?pr=36827#step:11:23736 shows that the balance is wrong:
File "D:\a\bitcoin\bitcoin/test/functional/wallet_assumeutxo.py", line 271, in run_test
assert_equal(w.getbalance(), 34)
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "D:\a\bitcoin\bitcoin\test\functional\test_framework\util.py", line 95, in assert_equal
raise AssertionError("not(%s)" % " == ".join(str(arg) for arg in (thing1, thing2) + args))
AssertionError: not(15.00000000 == 34)
LLM summary of the bug (experimental)
Timeline:
- dumb_sync_blocks() submits blocks through height 359.
- Their BlockConnected callbacks are queued on the scheduler.
- At 15:06:17.180791, the test performs the deliberately failing loadwallet("w").
- Loading w registers it for validation callbacks before it discovers that the required pre-snapshot blocks are unavailable.
- The scheduler then delivers already-queued notifications for blocks 357–359 to this newly registered wallet:
15:06:17.184199 BlockConnected height=357
15:06:17.184613 [w] AddToWallet ... height=357
15:06:17.184868 BlockConnected height=359
15:06:17.184911 [w] Releasing wallet w..
- The callback at height 357 finds a wallet transaction, so CWallet::blockConnected() persists the wallet’s best-block locator at 357.
- The load still fails as expected because blocks needed before the snapshot are unavailable. But the failed load has now mutated w on disk.
- After snapshot validation completes, the final loadwallet("w") trusts that poisoned locator and rescans only 357–399:
[w] Rescanning last 42 blocks (from block 357)
[w] mapWallet.size() = 15
That produces exactly the observed 15 payments: heights 357, 360, …, 399.
So the problem is not getbalance, and not snapshot validation completing too early. It is a race between wallet-load failure and queued validation callbacks. The callback registration in AttachChain() happens before the
availability preflight; its failing path disconnects and drains notifications, but draining allows the already-queued callbacks to update and persist the wallet.
A robust fix belongs in wallet loading: perform the “are all rescan blocks available?” preflight before registering the wallet for validation notifications, or otherwise ensure callbacks delivered during a failed attach
cannot write wallet state. A test-only synchronization barrier before the expected-failing loadwallet would avoid the flake, but would mask the underlying persistence bug.
Activity
achow101 commentedon Nov 10, 2026
Can you reliably reproduce the race?
Yudis-bit commentedon Nov 14, 2026
Since the availability preflight runs before any blocks are rescanned, early notification registration can be dropped.
The race reproduces when blocks submitted prior to
loadwalletare still being processed on the validation queue. InAttachChain(),handleNotifications()registers the wallet at line 3080 before checking block availability on disk. WhileAttachChain()holdscs_walletduring thehaveBlockOnDiskbackward scan, the scheduler picks up the in-flight block (height 357 inwallet_assumeutxo.py) and blocks oncs_wallet. OnceAttachChain()fails and releasescs_wallet, the scheduler acquires the lock andCWallet::blockConnected()writes the height 357 locator to disk beforeDisconnectChainNotifications()finishes draining.On subsequent load, the wallet trusts that locator and skips blocks 300–356. Deferring
handleNotifications()until after the availability check passes prevents the failing load from registering:achow101 commentedon Nov 14, 2026
This introduces a different race. A block that arrives after the tip height is retrieved but before notifications are registered results in that block being missed. Early registration is to prevent this race.
Yudis-bit commentedon Nov 15, 2026
Could we keep early registration to prevent the tip race, and gate the notification callbacks on an
m_is_attachedflag set undercs_walleton attach success? A failing load then drops drained notifications without touching disk.maflcko commentedon Nov 17, 2026
Sure, to repro the test issue:
Not sure how often it would happen in reality during IBD catch-up. Maybe just the test can be fixed for now and then this issue can be closed or revisited later?
Yudis-bit commentedon Nov 17, 2026
A failed wallet load must not persist locator state to disk.
Fixing only the test leaves on-disk corruption reachable in production: whenever
AttachChainaborts on pruned data or in-progress assumeutxo sync, drained callbacks from concurrent blocks can write a locator at tip. The write does not even require a relevant payment, sinceblockConnectedtriggersWriteBestBlock()periodically every 144 blocks regardless. On subsequent load, the wallet skips the missing historical range, silently omitting confirmed transactions.Gating
blockConnectedandblockDisconnectedonm_is_attached(set undercs_walletupon successful return ofAttachChain) ensures failed loads drop in-flight notifications without mutating disk.