refactor: replace RecursiveMutex `m_cs_banned` with Mutex (and rename) #24092

pull w0xlt wants to merge 3 commits into bitcoin:master from w0xlt:refactor_replace_recursive_mutex_m_cs_banned changing 2 files +50 −33
  1. w0xlt commented at 7:27 AM on January 18, 2022: contributor

    This PR is related to #19303 and gets rid of the RecursiveMutex m_cs_banned.

    The last commit prevents double lock for m_banned_mutex by changing BanMan::GetBanned() to lock it only after BanMan::SweepBanned() releases it.

  2. scripted-diff: rename m_cs_banned -> m_banned_mutex
    -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-
    431722c491
  3. refactor: replace RecursiveMutex m_banned_mutex with Mutex b43ad37dc4
  4. fanquake added the label Refactoring on Jan 18, 2022
  5. MarcoFalke commented at 7:34 AM on January 18, 2022: member

    The second commit is UB. Can you explain why the third commit is safe to do?

  6. w0xlt commented at 9:39 AM on January 18, 2022: contributor

    What does UB mean? The second commit follows the same format adopted in cac8366 .

    Can you explain why the third commit is safe to do?

    The third commit is necessary to avoid double lock error for m_banned_mutex. BanMan::SweepBanned() is only called internally in src/banman.cpp.

    1. It is called in BanMan::BanMan but this function does not acquire lock, so there is no need to change it.
    2. It is called in BanMan::DumpBanlist and this is the same case as above.
    3. BanMan::GetBanned acquired lock for mutex before calling BanMan::SweepBanned so a double lock occurred. With RecursiveMutex, it wasn´t a problem. But using mutex, the lock acquisiton needs to be moved after BanMan::SweepBanned is completed and its lock released.
  7. jonatack commented at 9:57 AM on January 18, 2022: member

    What does UB mean?

    Undefined behavior. See https://en.cppreference.com/w/cpp/language/ub and https://bitcoincore.reviews/17639 for more.

  8. MarcoFalke commented at 10:18 AM on January 18, 2022: member

    The third commit is necessary

    I understand that it is necessary to avoid the UB. I was asking why it is safe to release the lock in between.

  9. shaavan commented at 11:35 AM on January 18, 2022: contributor

    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:

    1. SweepBanned is called.
    2. LOCK is set up, m_banned is updated, LOCK is released.
    3. GetBanned sets up LOCK; the value of m_banned is copied to banmap.

    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.

  10. hebasto commented at 12:07 PM on January 18, 2022: member

    I'd suggest to apply Clang Thread Safety Analysis (TSA) annotations. In particular, void SweepBanned() EXCLUSIVE_LOCKS_REQUIRED(m_banned_mutex);.

    Also BanMan::SweepBanned could have two versions: the first one which requires the lock is hold, and the second one which requires the lock is not hold.

  11. prevent double lock for 'm_banned_mutex'
    `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.
    fd6c51b04d
  12. w0xlt force-pushed on Jan 18, 2022
  13. hebasto commented at 5:31 PM on January 18, 2022: member

    Please consider an alternative #24097. @w0xlt I've taken your commits into #24097.

  14. DrahtBot commented at 6:02 PM on January 18, 2022: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #24097 (Replace RecursiveMutex m_cs_banned with Mutex, and rename it by hebasto)
    • #19825 (rpc: simpler setban and new ban manipulation commands by dhruv)

    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.

  15. w0xlt force-pushed on Jan 18, 2022
  16. w0xlt commented at 9:30 PM on January 18, 2022: contributor

    Closed in favor of #24097.

  17. w0xlt closed this on Jan 18, 2022

  18. DrahtBot locked this on Jan 18, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 00:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me