refactor: replace RecursiveMutex m_cs_banned with Mutex (and rename)#24092
refactor: replace RecursiveMutex m_cs_banned with Mutex (and rename)#24092w0xlt wants to merge 3 commits intobitcoin:masterfrom
m_cs_banned with Mutex (and rename)#24092Conversation
-BEGIN VERIFY SCRIPT-
s() { sed -i 's/m_cs_banned/m_banned_mutex/g' $1; }
s src/banman.cpp
s src/banman.h
-END VERIFY SCRIPT-
|
The second commit is UB. Can you explain why the third commit is safe to do? |
|
What does UB mean?
The third commit is necessary to avoid double lock error for
|
Undefined behavior. See https://en.cppreference.com/w/cpp/language/ub and https://bitcoincore.reviews/17639 for more. |
I understand that it is necessary to avoid the UB. I was asking why it is safe to release the lock in between. |
The way I understand the risk behind releasing a lock in between functions is, say:
However, say another function acquires lock between steps 2 and 3 and updates the value of m_banned somehow, then we will get a false value return for banmap in step 3. I shall take another look at the codebase and see if this scenario is possible. |
|
I'd suggest to apply Clang Thread Safety Analysis (TSA) annotations. Also |
`BanMan::SweepBanned()` is only called internally in `src/banman.cpp`. `BanMan::GetBanned()` locks 'm_banned_mutex' and then calls `BanMan::SweepBanned()` without releasing the lock. This commit adds a new funtion `SweepBanned(bool isLockHeld)` to handle this.
5289e99 to
99bf859
Compare
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
99bf859 to
fd6c51b
Compare
|
Closed in favor of #24097. |
This PR is related to #19303 and gets rid of the RecursiveMutex
m_cs_banned.The last commit prevents double lock for
m_banned_mutexby changingBanMan::GetBanned()to lock it only afterBanMan::SweepBanned()releases it.