Skip to content

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 24, 2017

Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

@promag
Copy link
Contributor

promag commented Nov 27, 2017

utACK 9f857d5.

Are these random picks? For instance, CTxMemPool::CalculateMemPoolAncestors can assert the lock too right?

@maflcko
Copy link
Member

maflcko commented Nov 27, 2017

Why would we need the runtime check when there is a compile time check?

@practicalswift
Copy link
Contributor Author

@promag No, these are not random picks. They were the ones I could find, verify manually and passes both make check + test/functional/test_runner.py with nothing but trivial changes :-)

The lock in CTxMemPool::UpdateTransactionsFromBlock you suggested could also be removed but that would require more fine grained locking in the corresponding tests. If you have time then please post a diff of that locking change and I'll make it part of this PR :-)

@practicalswift
Copy link
Contributor Author

@MarcoFalke I replaced the LOCK(…); with AssertLockHeld(…); to make it easy to verify also for reviewers not building with Clang thread safety analysis warnings enabled. Should I remove them? :-)

@practicalswift
Copy link
Contributor Author

@promag Do you have any additional suggestions beyond CTxMemPool::CalculateMemPoolAncestors(…)? :-)

@maflcko
Copy link
Member

maflcko commented Nov 27, 2017

I think it is fine to leave them for now, but it seems arbitrary to add run time checks to a subset of methods that already have the compile time check. I know that not everyone compiles with clang, but at least for releases we do and I am sure some developers build with clang, so it should be noticed quickly.

@promag
Copy link
Contributor

promag commented Nov 27, 2017

I wasn't suggesting to remove more locks. Maybe these changes are preferable in small PR's.

@practicalswift
Copy link
Contributor Author

Added the removeConflicts(…) EXCLUSIVE_LOCKS_REQUIRED(…) annotation to txmempool.h.

@promag Would you mind re-reviewing?

@@ -522,13 +522,13 @@ bool CWalletDB::IsKeyType(const std::string& strType)
strType == "mkey" || strType == "ckey");
}

DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
DBErrors CWalletDB::LoadWallet(CWallet* pwallet) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
Copy link
Member

Choose a reason for hiding this comment

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

Adding this only in the .cpp means that the lock requirement will only be enforced for other functions in this file.

I realize that it can't be added in the header as-is due to the circular dependency, but that needs to be fixed somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@theuni Yes, I'd like to put the annotation in the header file but I need help with resolving the circular dependency. What would be the cleanest way to solve it? :-)

Copy link
Member

Choose a reason for hiding this comment

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

I'm afraid I don't see a simple solution :(

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if it's a simple solution, but it looks like you can resolve the circular dependency by declaring:

inline CCriticalSection &CWallet_cs_wallet(CWallet *pwallet) LOCK_RETURNED(pwallet->cs_wallet)
{
    return pwallet->cs_wallet;
}

in wallet.h and

CCriticalSection &CWallet_cs_wallet(CWallet *pwallet);
class CWalletDB {
    DBErrors LoadWallet(CWallet* pwallet) EXCLUSIVE_LOCKS_REQUIRED(CWallet_cs_wallet(pwallet));
}

in walletdb.h.

@@ -539,10 +539,10 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
RemoveStaged(setAllRemoves, false, MemPoolRemovalReason::REORG);
}

void CTxMemPool::removeConflicts(const CTransaction &tx)
void CTxMemPool::removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Copy link
Member

Choose a reason for hiding this comment

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

No need for the dupe here, header is enough.

@practicalswift
Copy link
Contributor Author

@theuni @promag @MarcoFalke Updated to address feedback. Please re-review :-)

@maflcko
Copy link
Member

maflcko commented Feb 22, 2018

utACK 96b6899e2c9b4a86698a6ea64460d045fde68c43

@sipa
Copy link
Member

sipa commented Jul 21, 2018

utACK 96b6899e2c9b4a86698a6ea64460d045fde68c43

@@ -524,7 +524,7 @@ DBErrors CWalletDB::LoadWallet(CWallet* pwallet)
bool fNoncriticalErrors = false;
DBErrors result = DB_LOAD_OK;

LOCK(pwallet->cs_wallet);
AssertLockHeld(pwallet->cs_wallet);
Copy link
Member

Choose a reason for hiding this comment

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

This will no longer compile due to the missing lock annotation in the header.

Could just remove it for now?

@DrahtBot
Copy link
Contributor

The last travis run for this pull request was 148 days ago and is thus outdated. To trigger a fresh travis build, this pull request should be closed and re-opened.

@DrahtBot DrahtBot closed this Jul 21, 2018
@DrahtBot DrahtBot reopened this Jul 21, 2018
@practicalswift
Copy link
Contributor Author

@MarcoFalke Now skipping the change to src/wallet/walletdb.cpp as suggested.

Please re-review :-)

@maflcko maflcko merged commit 01a06d6 into bitcoin:master Jul 22, 2018
maflcko pushed a commit that referenced this pull request Jul 22, 2018
… thread

01a06d6 Avoid locking mutexes that are already held by the same thread (practicalswift)

Pull request description:

  Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Tree-SHA512: e2fb85882e8800892fd8e8170f3c13128d6acfeb14d7b69fb9555f2b7ad0884fb201cf945b8144ffaf6fb1253c28af7c8c6c435319a7ae30ca003f28aa645a98
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 27, 2019
Summary:
Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Backport of Bitcoin Core PR11762
bitcoin/bitcoin#11762

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

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

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`.

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: Fabien, #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D4145
jonspock pushed a commit to jonspock/devault that referenced this pull request Dec 24, 2019
Summary:
Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Backport of Bitcoin Core PR11762
bitcoin/bitcoin#11762

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

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

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`.

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: Fabien, #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D4145
jonspock pushed a commit to jonspock/devault that referenced this pull request Dec 26, 2019
Summary:
Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Backport of Bitcoin Core PR11762
bitcoin/bitcoin#11762

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

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

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`.

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: Fabien, #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D4145
jonspock pushed a commit to devaultcrypto/devault that referenced this pull request Dec 27, 2019
Summary:
Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Backport of Bitcoin Core PR11762
bitcoin/bitcoin#11762

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

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

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`.

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: Fabien, #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D4145
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 29, 2020
…he same thread

01a06d6 Avoid locking mutexes that are already held by the same thread (practicalswift)

Pull request description:

  Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Tree-SHA512: e2fb85882e8800892fd8e8170f3c13128d6acfeb14d7b69fb9555f2b7ad0884fb201cf945b8144ffaf6fb1253c28af7c8c6c435319a7ae30ca003f28aa645a98
@practicalswift practicalswift deleted the remove-double-locks branch April 10, 2021 19:35
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
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Jan 29, 2022
…he same thread

01a06d6 Avoid locking mutexes that are already held by the same thread (practicalswift)

Pull request description:

  Avoid locking mutexes that are already held by the same thread. These are reentrant mutexes, but still no need to lock them more than once per thread :-)

Tree-SHA512: e2fb85882e8800892fd8e8170f3c13128d6acfeb14d7b69fb9555f2b7ad0884fb201cf945b8144ffaf6fb1253c28af7c8c6c435319a7ae30ca003f28aa645a98
@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.

8 participants