Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Dec 14, 2018

Both the chain state and the transaction pool are validation specific, but access to them is protected by two locks. The two locks have the following semantics:

  • Writing to the chain state or adding transactions to the transaction pool -> Take both cs_main and mempool::cs
  • Reading either or removing transactions from the the transaction pool -> Take only the appropriate lock

@maflcko maflcko added the Docs label Dec 14, 2018
@maflcko maflcko force-pushed the Mf1812-docValLocks branch 3 times, most recently from c7a872f to fa5ed69 Compare December 14, 2018 22:26
@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 15, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #11652 (Add missing locks: validation.cpp + related by practicalswift)
  • #10443 (Add fee_est tool for debugging fee estimation code by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Concept ACK. Title prefix suggests this is documentation only change, which is not. Some nits though.

@maflcko
Copy link
Member Author

maflcko commented Dec 17, 2018

Fixed up some of the language. Any suggestions for the pull request subject line?

@promag
Copy link
Contributor

promag commented Dec 17, 2018

I think the most important change is mempool: Add missing locks to `cs_main` , the remaining change is a consequence of that.

Could also say:

  • only tests are affected
  • fix related annotations and improve documentation

@maflcko maflcko changed the title doc: Add comment to cs_main and mempool::cs mempool, validation: Explain cs_main locking semantics Dec 17, 2018
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK fa64bb42e97b6c6c58e83badca88d386f99dbcd2

@maflcko maflcko force-pushed the Mf1812-docValLocks branch 2 times, most recently from fa70aeb to fa5c767 Compare December 17, 2018 19:46
@laanwj
Copy link
Member

laanwj commented Dec 19, 2018

ACK

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK fa5c7671aa4c916b191d0423309f89b69568c7f9. The only changes since my previous review were applying suggestions.

@maflcko maflcko force-pushed the Mf1812-docValLocks branch 3 times, most recently from fa0e072 to fa5f6ca Compare December 19, 2018 20:06
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK fa5e373. Changes since last review: adding longer mempool.cs comment and adding a commit with some new locking annotations.

Also, I don't think this needs to hold up merging, but I think it would be good if a reviewer with more mempool expertise could read the new code comments and ACK if they are correct.

// lack of CValidationInterface::TransactionAddedToMempool callbacks).
void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
Copy link
Contributor

Choose a reason for hiding this comment

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

Changer order of cs and cs_main here to make locking order consistent?

void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs);
void addUnchecked(const CTxMemPoolEntry& entry, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
Copy link
Contributor

Choose a reason for hiding this comment

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

Changer order of cs and cs_main here to make locking order consistent?

@practicalswift
Copy link
Contributor

utACK fa5e373 modulo nits

Copy link
Member

@sdaftuar sdaftuar left a comment

Choose a reason for hiding this comment

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

utACK fa5e373

* Mutex to guard access to validation specific variables, such as reading
* or changing the chainstate.
*
* This may also need to be locked when updating the transaction pool, e.g. on
Copy link
Member

Choose a reason for hiding this comment

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

We call the "transaction pool" the "mempool" in so many places in this file, I find the usage of "transaction pool" to be odd...

@maflcko maflcko merged commit fa5e373 into bitcoin:master Jan 15, 2019
maflcko pushed a commit that referenced this pull request Jan 15, 2019
fa5e373 validation: Add cs_main locking annotations (MarcoFalke)
fa5c346 doc: Add comment to cs_main and mempool::cs (MarcoFalke)
fafe941 test: Add missing validation locks (MarcoFalke)
fac4558 sync: Add RecursiveMutex type alias (MarcoFalke)

Pull request description:

  Both the chain state and the transaction pool are validation specific, but access to them is protected by two locks. The two locks have the following semantics:

  * Writing to the chain state or adding transactions to the transaction pool -> Take both `cs_main` and `mempool::cs`
  * Reading either or removing transactions from the the transaction pool -> Take only the appropriate lock

Tree-SHA512: 6f6e612ffc391904c6434a79a4f3f8de1b928bf0a3e3434b73561037b395e2b40a70a5a4bd8472dd230e9eacc8e5d5374c904a3c509910cf3971dd7ff59a626c
@maflcko maflcko deleted the Mf1812-docValLocks branch January 15, 2019 18:47
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2019
Summary:
Both the chain state and the transaction pool are validation specific, but access to them is protected by two locks. The two locks have the following semantics:

Backport of Bitcoin Core PR14963
bitcoin/bitcoin#14963

Test Plan:
1. Build with Clang in Debug mode:

```
CXX=clang++ CC=clang cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug
ninja check-all
```

2. Verify that the compiler has not emitted a `thread-safety` warning.
3. Run the node: `./src/bitcoind -regtest`
4. Verify that text similar to `"Assertion failed: lock ... not held ..."` is not printed on `stderr`.

5. Run benchmarks:
```
cmake .. -GNinja
ninja bench-bitcoin
./src/bench/bitcoin-bench
```

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D4227
@str4d str4d mentioned this pull request Apr 12, 2021
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Aug 24, 2021
…antics

fa5e373 validation: Add cs_main locking annotations (MarcoFalke)
fa5c346 doc: Add comment to cs_main and mempool::cs (MarcoFalke)
fafe941 test: Add missing validation locks (MarcoFalke)
fac4558 sync: Add RecursiveMutex type alias (MarcoFalke)

Pull request description:

  Both the chain state and the transaction pool are validation specific, but access to them is protected by two locks. The two locks have the following semantics:

  * Writing to the chain state or adding transactions to the transaction pool -> Take both `cs_main` and `mempool::cs`
  * Reading either or removing transactions from the the transaction pool -> Take only the appropriate lock

Tree-SHA512: 6f6e612ffc391904c6434a79a4f3f8de1b928bf0a3e3434b73561037b395e2b40a70a5a4bd8472dd230e9eacc8e5d5374c904a3c509910cf3971dd7ff59a626c
Munkybooty pushed a commit to Munkybooty/dash that referenced this pull request Aug 24, 2021
…antics

fa5e373 validation: Add cs_main locking annotations (MarcoFalke)
fa5c346 doc: Add comment to cs_main and mempool::cs (MarcoFalke)
fafe941 test: Add missing validation locks (MarcoFalke)
fac4558 sync: Add RecursiveMutex type alias (MarcoFalke)

Pull request description:

  Both the chain state and the transaction pool are validation specific, but access to them is protected by two locks. The two locks have the following semantics:

  * Writing to the chain state or adding transactions to the transaction pool -> Take both `cs_main` and `mempool::cs`
  * Reading either or removing transactions from the the transaction pool -> Take only the appropriate lock

Tree-SHA512: 6f6e612ffc391904c6434a79a4f3f8de1b928bf0a3e3434b73561037b395e2b40a70a5a4bd8472dd230e9eacc8e5d5374c904a3c509910cf3971dd7ff59a626c
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
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.

8 participants