Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutdown: fix crash on shutdown with reindex-chainstate #12349

Merged
merged 1 commit into from Feb 15, 2018

Conversation

theuni
Copy link
Member

@theuni theuni commented Feb 4, 2018

Fixes the assertion error reported here: #12349 (comment)

@fanquake fanquake added this to the 0.16.0 milestone Feb 4, 2018
@maflcko
Copy link
Member

maflcko commented Feb 4, 2018

Tests need update (test_bitcoin: coins.cpp:205: bool CCoinsViewCache::Flush(): Assertion `cachedCoinsUsage == 0' failed.)

@theuni
Copy link
Member Author

theuni commented Feb 5, 2018

@TheBlueMatt pointed out that this shouldn't be new for 0.16, and I've now reproduced with 0.15.1.

So, this probably shouldn't be an 0.16 blocker.

@maflcko maflcko removed this from the 0.16.0 milestone Feb 5, 2018
@TheBlueMatt
Copy link
Contributor

I ran into this a few times during testing, which was somewhat surprising so maybe it is somehow easier to hit now? Not sure why that would be...still, worth fixing if there's an easy fix for 0.16 (eaiser fix is probably to make the ActivateBestChain if (ShutdownRequested()) check a if (ShutdownRequested() && chainActive.Tip() != nullptr) check).

@laanwj laanwj added the GUI label Feb 6, 2018
@laanwj laanwj added this to the 0.16.1 milestone Feb 6, 2018
@TheBlueMatt
Copy link
Contributor

I prefer the fix in #12367 as much simpler.

@maflcko
Copy link
Member

maflcko commented Feb 6, 2018

Closing for now

@maflcko maflcko closed this Feb 6, 2018
laanwj added a commit that referenced this pull request Feb 8, 2018
dd2de47 Fix fast-shutdown crash if genesis block was not loaded (Matt Corallo)
1c9394a Fix fast-shutdown hang on ThreadImport+GenesisWait (Matt Corallo)

Pull request description:

  The second commit is a much simpler alternative fix for the issue fixed in #12349. To test I made ShutdownRequested() always StartShutdown() after a certain number of calls, which turned up one other hang, fixed in the first commit.

Tree-SHA512: 86bde6ac4b8b4e2cb99fff87dafeed02c0d9514acee6d94455637fb2da9ffc274b5ad31b0a6b9f5bd7b700ae35395f28ddb14ffc65ddda3619aa28df28a5607d
@eklitzke
Copy link
Contributor

Without this change, I still get a segfault from bitcoind master (commit c997f88) reliably. To reproduce:

  • Run bitcoind -reindex-chainstate -daemon=0
  • Immediately type Ctrl-c, and wait for bitcoind to stop
  • bitcoind segfaults with this stack trace:
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007f514a479381 in __GI_abort () at abort.c:79
#2  0x00007f514a46f8fa in __assert_fail_base (fmt=0x7f514a5eac28 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=assertion@entry=0x55efac7413fd "!hashBlock.IsNull()", file=file@entry=0x55efac7413f4 "txdb.cpp", line=line@entry=90, 
    function=function@entry=0x55efac741c40 <CCoinsViewDB::BatchWrite(std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to<COutPoint>, std::allocator<std::pair<COutPoint const, CCoinsCacheEntry> > >&, uint256 const&)::__PRETTY_FUNCTION__> "virtual bool CCoinsViewDB::BatchWrite(CCoinsMap&, const uint256&)") at assert.c:92
#3  0x00007f514a46f972 in __GI___assert_fail (assertion=0x55efac7413fd "!hashBlock.IsNull()", file=0x55efac7413f4 "txdb.cpp", line=90, 
    function=0x55efac741c40 <CCoinsViewDB::BatchWrite(std::unordered_map<COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to<COutPoint>, std::allocator<std::pair<COutPoint const, CCoinsCacheEntry> > >&, uint256 const&)::__PRETTY_FUNCTION__> "virtual bool CCoinsViewDB::BatchWrite(CCoinsMap&, const uint256&)") at assert.c:101
#4  0x000055efac41d7e1 in CCoinsViewDB::BatchWrite (this=0x55efacf5e9d0, mapCoins=std::unordered_map with 0 elements, hashBlock=...) at txdb.cpp:90
#5  0x000055efac5c3aae in CCoinsViewBacked::BatchWrite (this=0x55efc1c0d150, mapCoins=std::unordered_map with 0 elements, hashBlock=...) at coins.cpp:28
#6  0x000055efac5c4c7a in CCoinsViewCache::Flush (this=0x55efacf6de10) at coins.cpp:204
#7  0x000055efac4882a3 in FlushStateToDisk (chainparams=..., state=..., mode=FLUSH_STATE_ALWAYS, nManualPruneHeight=0) at validation.cpp:2099
#8  0x000055efac4887ed in FlushStateToDisk () at validation.cpp:2118
#9  0x000055efac1ad8c6 in Shutdown () at init.cpp:229
#10 0x000055efac19706a in AppInit (argc=2, argv=0x7fff3ef9cb68) at bitcoind.cpp:174
#11 0x000055efac19767a in main (argc=2, argv=0x7fff3ef9cb68) at bitcoind.cpp:186

@maflcko maflcko reopened this Feb 13, 2018
@maflcko maflcko modified the milestones: 0.16.1, 0.16.0 Feb 13, 2018
@theuni
Copy link
Member Author

theuni commented Feb 13, 2018

Thanks for helping to reproduce, eklitzke! Indeed I managed to force this with this patch:

--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1520,6 +1520,7 @@ bool AppInitMain()
                         break;
                     }
                 }
+                fRequestShutdown = true;

                 if (!is_coinsview_empty) {
                     uiInterface.InitMessage(_("Verifying blocks..."));

And running with:

gdb --args ./bitcoind -connect=0 -testnet -reindex-chainstate

I pushed a simple change that I think is obviously safe. I believe @TheBlueMatt would prefer a more elegant fix, but imo this is a reasonable option for the 0.16 branch. I defer to @wumpus on backporting.

Note that at first glance it may make more sense to add the checks in init.cpp around the FlushStateToDisk()s, but the first is called without cs_main and I avoided messing with locks for the sake of simplicity.

@ryanofsky
Copy link
Contributor

@TheBlueMatt, in IRC you wrote "we could revert to the initial suggestion of #12349 and just short-circuit flushing, but I really hate that" https://botbot.me/freenode/bitcoin-core-dev/msg/96765327/

Could you explain more? I haven't looked in detail, but at first glance this looks like a simple, obvious fix that doesn't rely on action at a distance like the one in #12367.

@wumpus
Copy link

wumpus commented Feb 13, 2018

You're deferring to the wrong wumpus!

@laanwj
Copy link
Member

laanwj commented Feb 15, 2018

utACK

We absolutely should have tests for this! it's a bit silly to merge a fix for something (#12367) then need another fix for the same thing, within the same rc phase.

@theuni
Copy link
Member Author

theuni commented Feb 15, 2018

@laanwj Yea, this is a mess :(

As a small data point, though, we believed this to be qt only and couldn't hit it with rc3. It was @eklitzke's bitcoind backtrace that made it easy to reproduce.

I just tested with current master and can confirm that the bug and fix are still valid.

Any suggestions for testing? Only thing I can think of is an rpc test that runs bitcoind with something like -stopafterblockimport, but that'd only be testing a really specific code path.

@promag
Copy link
Member

promag commented Feb 15, 2018

Tested ACK ceaefdd. Can't reproduce the crash with this change.

@laanwj
Copy link
Member

laanwj commented Feb 15, 2018

utACK ceaefdd

Any suggestions for testing? Only thing I can think of is an rpc test that runs bitcoind with something like -stopafterblockimport, but that'd only be testing a really specific code path.

@TheBlueMatt's suggestion was pretty good

<BlueMatt> yes, one thing I did during testing was just make ShutdownRequested() start shutdown after being called N times
<BlueMatt> and just restart with an incrementing N
<BlueMatt> this is something we could automate, but would largely not have caught the qt issues

(but not in this PR, obviously)

@laanwj laanwj merged commit ceaefdd into bitcoin:master Feb 15, 2018
laanwj added a commit that referenced this pull request Feb 15, 2018
ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: #12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca
laanwj pushed a commit that referenced this pull request Feb 15, 2018
Credit @eklitzke for reproducing.

Github-Pull: #12349
Rebased-From: ceaefdd
Tree-SHA512: bdc614d3c3fba23147be9528c581e25bbf1f0c359b525b4a05472ab42484724a8b34c8b3ed151f3ff23e48235e972950f9daa155d9ca3c4a9de6d61bf0591b4b
HashUnlimited pushed a commit to chaincoin/chaincoin that referenced this pull request Mar 16, 2018
Credit @eklitzke for reproducing.

Github-Pull: bitcoin#12349
Rebased-From: ceaefdd
Tree-SHA512: bdc614d3c3fba23147be9528c581e25bbf1f0c359b525b4a05472ab42484724a8b34c8b3ed151f3ff23e48235e972950f9daa155d9ca3c4a9de6d61bf0591b4b

Conflicts:
	src/validation.cpp
markblundeberg pushed a commit to markblundeberg/bitcoin-abc that referenced this pull request May 9, 2019
Summary:
ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: bitcoin/bitcoin#12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca

Backport of Core PR 12349
https://github.com/bitcoin/bitcoin/pull/12349/files
Discovered while working on PR 12367 (D2906)
Depends on D2906

Test Plan:
`make check`

On master, apply this patch:
```
diff --git a/src/init.cpp b/src/init.cpp
index 56520fe..8d7371d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2139,6 +2139,7 @@ bool AppInitMain(Config &config,
                         break;
                     }
                 }
+                fRequestShutdown = true;

                 if (!fReindex && !fReindexChainState) {
                     uiInterface.InitMessage(_("Verifying blocks..."));
```
Then run:
`gdb --args ./src/bitcoind -connect=0 -testnet -reindex-chainstate`
A crash should be encountered.

Apply the same patch above on this diff and run the same command above.
GDB should report a successful program exit.

Reviewers: deadalnix, Fabien, #bitcoin_abc

Reviewed By: Fabien, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D2907
jonspock pushed a commit to devaultcrypto/devault that referenced this pull request Jun 5, 2019
Summary:
ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: bitcoin/bitcoin#12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca

Backport of Core PR 12349
https://github.com/bitcoin/bitcoin/pull/12349/files
Discovered while working on PR 12367 (D2906)
Depends on D2906

Test Plan:
`make check`

On master, apply this patch:
```
diff --git a/src/init.cpp b/src/init.cpp
index 56520fe..8d7371d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2139,6 +2139,7 @@ bool AppInitMain(Config &config,
                         break;
                     }
                 }
+                fRequestShutdown = true;

                 if (!fReindex && !fReindexChainState) {
                     uiInterface.InitMessage(_("Verifying blocks..."));
```
Then run:
`gdb --args ./src/bitcoind -connect=0 -testnet -reindex-chainstate`
A crash should be encountered.

Apply the same patch above on this diff and run the same command above.
GDB should report a successful program exit.

Reviewers: deadalnix, Fabien, #bitcoin_abc

Reviewed By: Fabien, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D2907
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2020
dd2de47 Fix fast-shutdown crash if genesis block was not loaded (Matt Corallo)
1c9394a Fix fast-shutdown hang on ThreadImport+GenesisWait (Matt Corallo)

Pull request description:

  The second commit is a much simpler alternative fix for the issue fixed in bitcoin#12349. To test I made ShutdownRequested() always StartShutdown() after a certain number of calls, which turned up one other hang, fixed in the first commit.

Tree-SHA512: 86bde6ac4b8b4e2cb99fff87dafeed02c0d9514acee6d94455637fb2da9ffc274b5ad31b0a6b9f5bd7b700ae35395f28ddb14ffc65ddda3619aa28df28a5607d
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2020
…instate

ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: bitcoin#12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2020
dd2de47 Fix fast-shutdown crash if genesis block was not loaded (Matt Corallo)
1c9394a Fix fast-shutdown hang on ThreadImport+GenesisWait (Matt Corallo)

Pull request description:

  The second commit is a much simpler alternative fix for the issue fixed in bitcoin#12349. To test I made ShutdownRequested() always StartShutdown() after a certain number of calls, which turned up one other hang, fixed in the first commit.

Tree-SHA512: 86bde6ac4b8b4e2cb99fff87dafeed02c0d9514acee6d94455637fb2da9ffc274b5ad31b0a6b9f5bd7b700ae35395f28ddb14ffc65ddda3619aa28df28a5607d
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 14, 2020
…instate

ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: bitcoin#12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 15, 2020
dd2de47 Fix fast-shutdown crash if genesis block was not loaded (Matt Corallo)
1c9394a Fix fast-shutdown hang on ThreadImport+GenesisWait (Matt Corallo)

Pull request description:

  The second commit is a much simpler alternative fix for the issue fixed in bitcoin#12349. To test I made ShutdownRequested() always StartShutdown() after a certain number of calls, which turned up one other hang, fixed in the first commit.

Tree-SHA512: 86bde6ac4b8b4e2cb99fff87dafeed02c0d9514acee6d94455637fb2da9ffc274b5ad31b0a6b9f5bd7b700ae35395f28ddb14ffc65ddda3619aa28df28a5607d
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 15, 2020
…instate

ceaefdd fix possible shutdown assertion with -reindex-shutdown (Cory Fields)

Pull request description:

  Fixes the assertion error reported here: bitcoin#12349 (comment)

Tree-SHA512: db8e2a275f92a99df7f17852d00eba6df996e412aa3ed3853a9ea0a8cb9800760677532efd52f92abbf2cdcc4210957a87a5f919ac998d46c205365a7a7dffca
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants