Skip to content

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 6, 2017

FlushStateToDisk(...) won't do anything besides check if we need to prune if
FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
which is guarded by the mutex cs_LastBlockFile.

@practicalswift practicalswift changed the title Add missing cs_LastBlockFile lock in AcceptBlock validation: Add missing cs_LastBlockFile lock in AcceptBlock Nov 6, 2017
@promag
Copy link
Contributor

promag commented Nov 6, 2017

From what I can tell it's missing only one lock (in PruneAndFlush) maybe fix it here too?

@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from d982e13 to 60151f9 Compare November 6, 2017 15:27
@practicalswift
Copy link
Contributor Author

@promag Good point. Looks good now now?

@promag
Copy link
Contributor

promag commented Nov 6, 2017

In that case reword PR and commit, like Add missing cs_LastBlockFile locks

@practicalswift practicalswift changed the title validation: Add missing cs_LastBlockFile lock in AcceptBlock validation: Add missing cs_LastBlockFile locks Nov 6, 2017
@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from 60151f9 to f151609 Compare November 6, 2017 15:44
@practicalswift
Copy link
Contributor Author

@promag Done :-)

@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from f151609 to cc0d685 Compare November 6, 2017 16:21
@practicalswift
Copy link
Contributor Author

practicalswift commented Nov 6, 2017

@promag Had to revert to initial version. Encountered timeouts when adding locking in PruneAndFlush as suggested and haven't investigated deeper yet. What is your suggested patch? :-)

@@ -3232,6 +3232,7 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
return AbortNode(state, std::string("System error: ") + e.what());
}

LOCK(cs_LastBlockFile);
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like we could just always call FlushStateToDisk instead? It wont do anything besides check if we need to prune if FLUSH_STATE_NONE is given.

@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from cc0d685 to 96ff97c Compare November 6, 2017 20:33
@practicalswift
Copy link
Contributor Author

@TheBlueMatt Even better! PR updated. Please re-review :-)

@promag
Copy link
Contributor

promag commented Nov 6, 2017

utACK 96ff97c. Please reword PR again.

@practicalswift practicalswift changed the title validation: Add missing cs_LastBlockFile locks Call FlushStateToDisk(...) regardless of fCheckForPruning Nov 6, 2017
@@ -3232,8 +3232,7 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
return AbortNode(state, std::string("System error: ") + e.what());
}

if (fCheckForPruning)
FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment now needs updated.

@TheBlueMatt
Copy link
Contributor

utACK 96ff97cc14b483643020df2497ab06511f5d2ae8 modulo now-incorrect comment.

@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from 96ff97c to 932dcd4 Compare November 6, 2017 22:16
@practicalswift
Copy link
Contributor Author

@TheBlueMatt Comment removed :-)

@TheBlueMatt
Copy link
Contributor

utACK 932dcd49486c6616ee5bfab9588d59fd75c4f0d4

@promag
Copy link
Contributor

promag commented Nov 7, 2017

This PR is a bit messy now. Regarding the original intent, I think it should do:

@@ -2012,6 +2012,7 @@ void FlushStateToDisk() {
 }

 void PruneAndFlush() {
+    LOCK2(cs_main, cs_LastBlockFile);
     CValidationState state;
     fCheckForPruning = true;
     const CChainParams& chainparams = Params();

An alternative is to add an argument to FlushStateToDisk, like bool force_check_for_pruning to avoid touching fCheckForPruning.

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Nov 7, 2017 via email

@promag
Copy link
Contributor

promag commented Nov 7, 2017

@TheBlueMatt IMO the best approach to avoid long/recursive/shared locks is to create a snapshot of the chain and use that downstream. The only lock required is when the chain snapshot is created. A slight more advanced solution is copy-on-write, which compared to shared lock, doesn't make the writer wait for readers to finish.

@promag
Copy link
Contributor

promag commented Nov 7, 2017

@TheBlueMatt PruneAndFlush changes fCheckForPruning without the lock.

With:

@@ -2012,6 +2012,7 @@ void FlushStateToDisk() {
 }

 void PruneAndFlush() {
+    AssertLockHeld(cs_LastBlockFile);
     CValidationState state;
     fCheckForPruning = true;
     const CChainParams& chainparams = Params();

The following fails:

./test/functional/pruning.py
2017-11-07 01:02:25.034000 TestFramework (INFO): Initializing test directory /var/folders/1v/8_69hby54nj2k3n6fywt44x80000gn/T/testgwrg340t
Assertion failed: lock cs_LastBlockFile not held in validation.cpp:2015; locks held:

Note ./configure CPPFLAGS=-DDEBUG_LOCKORDER.

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Nov 7, 2017 via email

@practicalswift
Copy link
Contributor Author

We have one utACK from @TheBlueMatt. Anyone else willing to review? :-)

@promag
Copy link
Contributor

promag commented Nov 8, 2017

Testing fCheckForPruning is not needed in AcceptBlock (which BTW asserts cs_main is held) since the check is done in FlushStateToDisk.

ACK 932dcd4.

@practicalswift
Copy link
Contributor Author

Ready for merge? :-)

@practicalswift
Copy link
Contributor Author

Any chance of this getting merged or should I close?

@practicalswift practicalswift changed the title Call FlushStateToDisk(...) regardless of fCheckForPruning Avoid (previously missing) lock: Call FlushStateToDisk(...) regardless of fCheckForPruning Apr 9, 2018
@practicalswift practicalswift changed the title Avoid (previously missing) lock: Call FlushStateToDisk(...) regardless of fCheckForPruning Avoid lock: Call FlushStateToDisk(...) regardless of fCheckForPruning Apr 9, 2018
@practicalswift
Copy link
Contributor Author

Any chance of getting this merged or re-reviewed?

@sdaftuar
Copy link
Member

utACK. I'd prefer if we also added clearer documentation that FlushStateMode::NONE behaves in the way that we're relying on here (perhaps an extra comment in FlushStateToDisk?).

FlushStateToDisk(...) won't do anything besides check if we need to prune if
FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
which is guarded by the mutex cs_LastBlockFile.
@practicalswift practicalswift force-pushed the cs_LastBlockFile-fCheckForPruning branch from 18271a7 to 0000d8f Compare April 11, 2018 10:46
@practicalswift
Copy link
Contributor Author

@sdaftuar Good point! Documentation added. Please re-review :-)

@sdaftuar
Copy link
Member

Thanks, re-utACK 0000d8f

@laanwj laanwj merged commit 0000d8f into bitcoin:master Apr 11, 2018
laanwj added a commit that referenced this pull request Apr 11, 2018
…heckForPruning

0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Aug 16, 2019
…heckForPruning

Summary:
0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999

Backport of Core PR11617
bitcoin/bitcoin#11617

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3844
jonspock pushed a commit to jonspock/devault that referenced this pull request Nov 4, 2019
…heckForPruning

Summary:
0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999

Backport of Core PR11617
bitcoin/bitcoin#11617

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3844
proteanx pushed a commit to devaultcrypto/devault that referenced this pull request Nov 4, 2019
…heckForPruning

Summary:
0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999

Backport of Core PR11617
bitcoin/bitcoin#11617

Test Plan:
  make check
  test_runner.py

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D3844
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Nov 1, 2020
…s of fCheckForPruning

0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999
@practicalswift practicalswift deleted the cs_LastBlockFile-fCheckForPruning branch April 10, 2021 19:34
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 26, 2021
FlushStateToDisk(...) won't do anything besides check if we need to prune if
FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
which is guarded by the mutex cs_LastBlockFile.

zcash: cherry picked from commit 2311c7c
zcash: bitcoin/bitcoin#11617
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 26, 2021
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request May 27, 2021
FlushStateToDisk(...) won't do anything besides check if we need to prune if
FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
which is guarded by the mutex cs_LastBlockFile.

zcash: cherry picked from commit 2311c7c
zcash: bitcoin/bitcoin#11617
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request May 27, 2021
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Mar 28, 2022
…s of fCheckForPruning

0000d8f Document how FlushStateMode::NONE is handled (practicalswift)
2311c7c Call FlushStateToDisk(...) regardless of fCheckForPruning (practicalswift)

Pull request description:

  FlushStateToDisk(...) won't do anything besides check if we need to prune if
  FLUSH_STATE_NONE is given. We avoid reading the variable fCheckForPruning
  which is guarded by the mutex cs_LastBlockFile.

Tree-SHA512: 89df06256f73503a74b9e26d580ce9ed09efaef347fae1ff6a5759a2993b0db52edd2fadb65694d27e579a5aed92127753bdf41b5bb1bd516e577fcf17f17999
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants