Skip to content

Conversation

@random-zebra
Copy link

Had this segfault when trying to shut down the wallet before the coins cache is loaded.

CCoinsViewCache::DynamicMemoryUsage (this=0x0) at coins.cpp:60
60	    return memusage::DynamicUsage(cacheCoins) +
(gdb) bt
#0  0x0000555555bb8b44 in CCoinsViewCache::DynamicMemoryUsage() const (this=0x0) at coins.cpp:60
#1  0x0000555555996bc1 in FlushStateToDisk(CValidationState&, FlushStateMode) (state=..., mode=mode@entry=FLUSH_STATE_ALWAYS) at validation.cpp:1730
#2  0x0000555555997540 in FlushStateToDisk() () at validation.cpp:1806
#3  0x000055555582516c in PrepareShutdown() () at init.cpp:266
#4  0x0000555555826005 in Shutdown() () at init.cpp:339

This is due to a bug introduced in 64c525b (we should null-check pcoinsTip before calling FlushStateToDisk at line 266, same as we do at line 283).

Upstream fixed it in bitcoin#10758 among few other things.
Backported here without ff3a219 (as we don't have RewindBlockIndex or reindex-chainstate yet).

@random-zebra random-zebra added Bug Upstream Refactoring Startup Wallet startup changes and/or improvements labels Mar 2, 2021
@random-zebra random-zebra added this to the 5.1.0 milestone Mar 2, 2021
@random-zebra random-zebra self-assigned this Mar 2, 2021
random-zebra and others added 4 commits March 2, 2021 11:33
>>> adapts bitcoin/bitcoin@eda888e

* Move the writing of fTxIndex to LoadBlockIndex - this fixes a
  bug introduced in d6af06d where
  InitBlockIndex was writing to fTxIndex which had not yet been
  checked (because LoadChainTip hadn't yet initialized the
  chainActive, which would otherwise have resulted in
  InitBlockIndex being a NOP), allowing you to modify -txindex
  without reindex, potentially corrupting your chainstate!

* Rename InitBlockIndex to LoadGenesisBlock, which is now a more
  natural name for it. Also check mapBlockIndex instead of
  chainActive, fixing a bug where we'd write the genesis block out
  on every start.
This gives LoadChainTip a return value - allowing it to indicate that
the UTXO DB ran ahead of the block DB. This just provides a nicer
error message instead of the previous mysterious
assert(!setBlockIndexCandidates.empty()) error.

This also calls ActivateBestChain in case we just loaded the genesis
block in LoadChainTip, avoiding relying on the ActivateBestChain
in ThreadImport before continuing init process.
>>> adapts bitcoin/bitcoin@1385697

* Order chainstate init more logically - first all of the
  blocktree-related loading, then coinsdb, then
  pcoinsTip/chainActive. Only create objects as needed.

* More clearly document exactly what is and isn't called in
  -reindex and -reindex-chainstate both with comments noting
  calls as no-ops and by adding if guards.

* Move LoadGenesisBlock further down in init. This is a more logical
  location for it, as it is after all of the blockindex-related
  loading and checking, but before any of the UTXO-related loading
  and checking.

* Move all of the VerifyDB()-related stuff into a -reindex +
  -reindex-chainstate if guard. It couldn't do anything useful
  as chainActive.Tip() would be null at this point anyway.
This was introduced by 3192975.
It can be triggered easily when canceling DB upgrade from
pre-per-utxo.
@random-zebra random-zebra force-pushed the 202103_non-atomic-flush-fixes branch from 886a4cc to 43cc880 Compare March 2, 2021 10:34
Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

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

nice finding, code review ACK 43cc880 .
will play with it a bit on top of 2209 before giving my final ack.

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

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

ACK 4749d52

Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

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

ACK 4749d52 and merging..

@furszy furszy merged commit 2518433 into PIVX-Project:master Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Refactoring Startup Wallet startup changes and/or improvements Upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants