-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Avoid locking mutexes that are already held by the same thread #11762
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
Conversation
utACK 9f857d5. Are these random picks? For instance, |
Why would we need the runtime check when there is a compile time check? |
@promag No, these are not random picks. They were the ones I could find, verify manually and passes both The lock in |
@MarcoFalke I replaced the |
@promag Do you have any additional suggestions beyond |
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. |
I wasn't suggesting to remove more locks. Maybe these changes are preferable in small PR's. |
9f857d5
to
dc6cd0b
Compare
Added the @promag Would you mind re-reviewing? |
src/wallet/walletdb.cpp
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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? :-)
There was a problem hiding this comment.
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 :(
There was a problem hiding this comment.
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.
src/txmempool.cpp
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
dc6cd0b
to
efc69c8
Compare
efc69c8
to
96b6899
Compare
@theuni @promag @MarcoFalke Updated to address feedback. Please re-review :-) |
utACK 96b6899e2c9b4a86698a6ea64460d045fde68c43 |
utACK 96b6899e2c9b4a86698a6ea64460d045fde68c43 |
src/wallet/walletdb.cpp
Outdated
@@ -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); |
There was a problem hiding this comment.
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?
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. |
96b6899
to
01a06d6
Compare
@MarcoFalke Now skipping the change to Please re-review :-) |
… 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
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
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
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
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
…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
zcash: cherry picked from commit 01a06d6 zcash: bitcoin/bitcoin#11762
zcash: cherry picked from commit 01a06d6 zcash: bitcoin/bitcoin#11762
…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
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 :-)