refactor: Replace RecursiveMutex with Mutex in CTxMemPool #19306

pull hebasto wants to merge 25 commits into bitcoin:master from hebasto:200616-mempool-mx changing 32 files +548 −283
  1. hebasto commented at 4:15 PM on June 17, 2020: member

    This PR replaces RecursiveMutex CTxMemPool::cs with Mutex CTxMemPool:cs.

    All of the related code branches are covered by appropriate lock assertions to insure that the mutex locking policy has not been changed by accident

    Related to #19303.

  2. DrahtBot added the label GUI on Jun 17, 2020
  3. DrahtBot added the label Mempool on Jun 17, 2020
  4. DrahtBot added the label Mining on Jun 17, 2020
  5. DrahtBot added the label P2P on Jun 17, 2020
  6. DrahtBot added the label Refactoring on Jun 17, 2020
  7. DrahtBot added the label RPC/REST/ZMQ on Jun 17, 2020
  8. DrahtBot added the label Tests on Jun 17, 2020
  9. DrahtBot added the label TX fees and policy on Jun 17, 2020
  10. DrahtBot added the label Validation on Jun 17, 2020
  11. DrahtBot commented at 11:20 PM on June 17, 2020: contributor

    <!--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:

    • #19826 (Pass mempool reference to chainstate constructor by MarcoFalke)
    • #19806 (validation: UTXO snapshot activation by jamesob)
    • #19791 ([net processing] Move Misbehaving() to PeerManager by jnewbery)
    • #19753 (p2p: don't add AlreadyHave transactions to recentRejects by troygiorshev)
    • #19652 (Add thread safety annotations to Mempool{Info}ToJSON() by hebasto)
    • #19647 (Add thread safety annotations to CTxMemPool methods by hebasto)
    • #19645 (Allow updating mempool-txn with cheaper witnesses by ariard)
    • #19610 (p2p: refactor AlreadyHave(), CInv::type, INV/TX processing by jonatack)
    • #19572 (ZMQ: Create "sequence" notifier, enabling client-side mempool tracking by instagibbs)
    • #19544 (refactor: Add ParseBool to rpc/util by fjahr)
    • #19498 (Tidy up ProcessOrphanTx by jnewbery)
    • #19488 (Refactor mempool.dat to be extensible, and store missing info by luke-jr)
    • #19478 (Remove CTxMempool::mapLinks data structure member by JeremyRubin)
    • #19339 (validation: re-delegate absurd fee checking from mempool to clients by gzhao408)
    • #19093 (RPC: Return transaction fee from testmempoolaccept by rajarshimaitra)
    • #18191 (Change UpdateForDescendants to use Epochs by JeremyRubin)
    • #18017 (txmempool: split epoch logic into class by ajtowns)
    • #13990 (Allow fee estimation to work with lower fees by ajtowns)
    • #12677 (RPC: Add ancestor{count,size,fees} to listunspent output by luke-jr)

    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.

  12. DrahtBot cross-referenced this on Jun 17, 2020 from issue net: Avoid redundant and confusing FAILED log by MarcoFalke
  13. DrahtBot cross-referenced this on Jun 18, 2020 from issue test: Check that peers with forcerelay permission are not asked to feefilter by MarcoFalke
  14. DrahtBot cross-referenced this on Jun 18, 2020 from issue Overhaul transaction request logic by sipa
  15. DrahtBot cross-referenced this on Jun 18, 2020 from issue refactor: replace CConnman pointers by references in net_processing.cpp by theStack
  16. fanquake removed the label GUI on Jun 18, 2020
  17. fanquake removed the label Mining on Jun 18, 2020
  18. fanquake removed the label P2P on Jun 18, 2020
  19. fanquake removed the label RPC/REST/ZMQ on Jun 18, 2020
  20. fanquake removed the label TX fees and policy on Jun 18, 2020
  21. fanquake removed the label Tests on Jun 18, 2020
  22. fanquake removed the label Validation on Jun 18, 2020
  23. DrahtBot cross-referenced this on Jun 18, 2020 from issue RPC: Return transaction fee from testmempoolaccept by rajarshimaitra
  24. DrahtBot cross-referenced this on Jun 18, 2020 from issue net: Use mockable time for ping/pong, add tests by MarcoFalke
  25. DrahtBot cross-referenced this on Jun 18, 2020 from issue coins: allow cache resize after init by jamesob
  26. DrahtBot cross-referenced this on Jun 18, 2020 from issue rpc: remove deprecated CRPCCommand constructor by MarcoFalke
  27. DrahtBot cross-referenced this on Jun 18, 2020 from issue Change UpdateForDescendants to use Epochs by JeremyRubin
  28. DrahtBot cross-referenced this on Jun 18, 2020 from issue txmempool: split epoch logic into class by ajtowns
  29. hebasto cross-referenced this on Jun 18, 2020 from issue Replace all of the RecursiveMutex instances with the Mutex ones by hebasto
  30. hebasto commented at 9:04 AM on June 18, 2020: member

    From IRC:

    <sipa> eh, i agree - that change isn't a step in the right direction @sipa Which direction is right?

  31. hebasto commented at 9:28 AM on June 18, 2020: member

    From IRC:

    <jeremyrubin> I think it's basically getting rid of a recursive mutex for code that's still designed to take a recursive mutex <gwillen> it's better than a true recursive mutex because it's not possible to recurse by accident, you have to declare at call time which behavior you want (although better if you had to declare statically at compile time) <sipa> it looks like that <jeremyrubin> The correct refactor would be to make the code not do anything fancy with locks, or to just leave it <jeremyrubin> gwillen: I think the chances of a bug or error in custom logic is higher than a recursive mutex <jeremyrubin> accidental recursion seems unlikely... <jeremyrubin> and accidental recursion would be a bug lock or no <gwillen> (sorry I mean, accidental mutex recursion, that is, calling a function while holding a mutex, not expecting the callee to also lock it, resulting in the callee violating the caller's invariants) <gwillen> (this is the fundamental problem of recursive mutexes) <gwillen> (and I assume the motivation behind hebasto's refactor)

    Correct, @gwillen :)

  32. hebasto commented at 9:31 AM on June 18, 2020: member

    Searching for concept (N)ACKs before separating digestible chunks for reviewing into smaller pulls.

  33. hebasto marked this as ready for review on Jun 18, 2020
  34. DrahtBot cross-referenced this on Jun 18, 2020 from issue Allow fee estimation to work with lower fees by ajtowns
  35. MarcoFalke commented at 11:29 AM on June 18, 2020: member

    Currently this adds a lot of code complexity. Also, it adds mental complexity to write code that doesn't crash or deadlock/UB whereas the RecursiveMutex just works (TM).

    Maybe there is a way to achieve the same without the added complexity? For example, always force the caller to take the lock for the right scope. This would also solve issues of non-atomic RPC responses or at make them more visible. Though, it makes caller code slightly more verbose.

  36. hebasto commented at 11:38 AM on June 18, 2020: member

    Currently this adds a lot of code complexity. Also, it adds mental complexity to write code that doesn't crash or deadlock/UB whereas the RecursiveMutex just works (TM).

    A function that locks RecursiveMutex there are no guarantees that protected invariants are held before locking. This adds mental complexity to read code.

    Maybe there is a way to achieve the same without the added complexity? For example, always force the caller to take the lock for the right scope. This would also solve issues of non-atomic RPC responses or at make them more visible.

    I assume these steps are much easier for non-recursive mutex, no?

  37. DrahtBot cross-referenced this on Jun 19, 2020 from issue RPC: Add ancestor{count,size,fees} to listunspent output by luke-jr
  38. DrahtBot added the label Needs rebase on Jun 19, 2020
  39. gwillen commented at 7:34 PM on June 19, 2020: contributor

    @hebasto Do you have any thoughts on "function takes bool locked" versus "split function into _locked and _unlocked variants"?

    In some of the cases here, I see that the latter would require some (maybe substantial) refactoring to avoid code duplication, and I assume that's why you didn't go that route. But that would not only eliminate conditional locking (which is scary), it would probably allow the use of RAII locks. I'm curious to hear your thoughts.

  40. hebasto commented at 12:41 PM on June 20, 2020: member

    @gwillen

    @hebasto Do you have any thoughts on "function takes bool locked" versus "split function into _locked and _unlocked variants"?

    For new code I prefer clean "split function into _locked and _unlocked variants" like #19238 (review). Unfortunately, that is not the case in this PR.

    In some of the cases here, I see that the latter would require some (maybe substantial) refactoring to avoid code duplication, and I assume that's why you didn't go that route.

    You are correct. I've used the "function takes bool locked" approach in 72f7486b5ebe96762c5d5a68849c61e58c812ffd and 21787abedd7a0808c0175a0b1d795df97fd3b970 as a quick-and-dirty solution to fix broken tests.

    But that would not only eliminate conditional locking (which is scary), it would probably allow the use of RAII locks. I'm curious to hear your thoughts.

    Hmm... In 21787abedd7a0808c0175a0b1d795df97fd3b970 I've had to drop the RAII lock in favor of a pair ENTER_CRITICAL_SECTION() - LEAVE_CRITICAL_SECTION(). That is why I dont't like this approach.

  41. hebasto force-pushed on Jun 20, 2020
  42. hebasto commented at 1:04 PM on June 20, 2020: member

    Rebased 4e00526c689c4164d02d8ca76331f3ed5da7b13c -> c9e7d011d69bb1ef965945bf90d7441165430808 (pr19306.01 -> pr19306.02) due to the conflict with #19293.

  43. DrahtBot removed the label Needs rebase on Jun 20, 2020
  44. DrahtBot cross-referenced this on Jun 20, 2020 from issue validation: re-delegate absurd fee checking from mempool to clients by glozow
  45. DrahtBot added the label Needs rebase on Jun 21, 2020
  46. hebasto force-pushed on Jun 23, 2020
  47. hebasto commented at 5:30 PM on June 23, 2020: member

    Rebased c9e7d011d69bb1ef965945bf90d7441165430808 -> 3fc8fa23fc4ff2978c89bba46f08e746b6e4c154 (pr19306.02 -> pr19306.03) due to the conflicts with #18027 and #19198.

  48. DrahtBot removed the label Needs rebase on Jun 23, 2020
  49. DrahtBot cross-referenced this on Jun 23, 2020 from issue net processing: Move orphan reprocessing to a global by jnewbery
  50. DrahtBot cross-referenced this on Jun 23, 2020 from issue build: Do not include server symbols in wallet by MarcoFalke
  51. DrahtBot added the label Needs rebase on Jun 24, 2020
  52. hebasto force-pushed on Jun 24, 2020
  53. hebasto commented at 6:17 PM on June 24, 2020: member

    Rebased 3fc8fa23fc4ff2978c89bba46f08e746b6e4c154 -> ff3d969891b0687219906f18e66c5bb499915968 (pr19306.03 -> pr19306.04) due to the conflict with https://github.com/bitcoin-core/gui/pull/11.

  54. DrahtBot removed the label Needs rebase on Jun 24, 2020
  55. DrahtBot cross-referenced this on Jun 25, 2020 from issue Use wtxid for transaction relay by sdaftuar
  56. DrahtBot cross-referenced this on Jun 27, 2020 from issue test: Add test for wtxid transaction relay by fjahr
  57. DrahtBot added the label Needs rebase on Jul 1, 2020
  58. hebasto force-pushed on Jul 3, 2020
  59. hebasto commented at 9:12 AM on July 3, 2020: member

    Rebased ff3d969891b0687219906f18e66c5bb499915968 -> 511670449669116df5488cd4f807de620e55a7e3 (pr19306.04 -> pr19306.05) due to the conflict with #19331.

  60. DrahtBot removed the label Needs rebase on Jul 3, 2020
  61. DrahtBot cross-referenced this on Jul 9, 2020 from issue Accurately account for mempool index memory by sipa
  62. DrahtBot cross-referenced this on Jul 10, 2020 from issue Remove CTxMempool::mapLinks data structure member by JeremyRubin
  63. DrahtBot cross-referenced this on Jul 10, 2020 from issue doc: Use precise permission flags where possible by MarcoFalke
  64. DrahtBot added the label Needs rebase on Jul 11, 2020
  65. hebasto force-pushed on Jul 16, 2020
  66. hebasto commented at 6:57 PM on July 16, 2020: member

    Rebased 511670449669116df5488cd4f807de620e55a7e3 -> 0c03cea32d3ab30a58e62bbe42af6ebef016ede4 (pr19306.05 -> pr19306.06) due to the conflicts with #19174 and #19474.

  67. DrahtBot removed the label Needs rebase on Jul 16, 2020
  68. DrahtBot cross-referenced this on Jul 16, 2020 from issue Tidy up ProcessOrphanTx by jnewbery
  69. DrahtBot cross-referenced this on Jul 16, 2020 from issue Refactor mempool.dat to be extensible, and store missing info by luke-jr
  70. DrahtBot cross-referenced this on Jul 17, 2020 from issue Revert "refactor: replace CConnman pointers by references in net_processing.cpp" by laanwj
  71. DrahtBot cross-referenced this on Jul 18, 2020 from issue refactor: Add ParseBool to rpc/util by fjahr
  72. DrahtBot cross-referenced this on Jul 19, 2020 from issue validation: Warm coins cache during prevalidation to connect blocks faster by andrewtoth
  73. DrahtBot cross-referenced this on Jul 20, 2020 from issue Remove mempool global by MarcoFalke
  74. DrahtBot added the label Needs rebase on Jul 22, 2020
  75. hebasto force-pushed on Jul 29, 2020
  76. hebasto commented at 11:32 AM on July 29, 2020: member

    Rebased 0c03cea32d3ab30a58e62bbe42af6ebef016ede4 -> 656cba72f475603497e318ed3f01db4ab694b2af (pr19306.06 -> pr19306.07) due to the conflicts with #18044 and #18637.

  77. DrahtBot removed the label Needs rebase on Jul 29, 2020
  78. DrahtBot cross-referenced this on Jul 29, 2020 from issue Pass mempool pointer to UnloadBlockIndex/GetCoinsCacheSizeState by MarcoFalke
  79. DrahtBot cross-referenced this on Jul 29, 2020 from issue Deduplicate parent txid loop of requested transactions and missing parents of orphan transactions by sdaftuar
  80. DrahtBot cross-referenced this on Jul 29, 2020 from issue ZMQ: Create "sequence" notifier, enabling client-side mempool tracking by instagibbs
  81. DrahtBot cross-referenced this on Jul 29, 2020 from issue Enable fetching of orphan parents from wtxid peers by sipa
  82. DrahtBot cross-referenced this on Jul 29, 2020 from issue Erlay: bandwidth-efficient transaction relay protocol by naumenkogs
  83. DrahtBot cross-referenced this on Jul 29, 2020 from issue [RFC] Package-relay: sender-initiated by ariard
  84. DrahtBot cross-referenced this on Jul 30, 2020 from issue Disable fee estimation in blocksonly mode (by removing the fee estimates global) by darosior
  85. DrahtBot added the label Needs rebase on Jul 30, 2020
  86. hebasto force-pushed on Aug 2, 2020
  87. hebasto commented at 9:50 PM on August 2, 2020: member

    Rebased 656cba72f475603497e318ed3f01db4ab694b2af -> e23248c51c092269a33cde7ad0ff70a815876396 (pr19306.07 -> pr19306.08) due to the conflicts with #18011, #19569, and #19604.

  88. hebasto cross-referenced this on Aug 2, 2020 from issue Add thread safety annotations to CTxMemPool methods by hebasto
  89. hebasto commented at 10:47 PM on August 2, 2020: member

    Some commits split out into #19647. So please start reviewing from #19647.

  90. DrahtBot cross-referenced this on Aug 3, 2020 from issue Allow updating mempool-txn with cheaper witnesses by ariard
  91. DrahtBot cross-referenced this on Aug 3, 2020 from issue refactor: Keep mempool interface in validation by MarcoFalke
  92. hebasto cross-referenced this on Aug 3, 2020 from issue Avoid locking CTxMemPool::cs recursively in Mempool{Info}ToJSON() by hebasto
  93. DrahtBot removed the label Needs rebase on Aug 3, 2020
  94. DrahtBot cross-referenced this on Aug 7, 2020 from issue Run clang-tidy -*,performance-* by Warchant
  95. DrahtBot cross-referenced this on Aug 8, 2020 from issue p2p: refactor AlreadyHave(), CInv::type, INV/TX processing by jonatack
  96. DrahtBot added the label Needs rebase on Aug 10, 2020
  97. Wiphawee112 approved
  98. Wiphawee112 commented at 1:57 PM on August 19, 2020: none

  99. hebasto force-pushed on Aug 21, 2020
  100. hebasto commented at 6:56 AM on August 21, 2020: member

    Rebased e23248c51c092269a33cde7ad0ff70a815876396 -> 95b4daa91ea4eecd3f345a20f285fb0528f5070d (pr19306.08 -> pr19306.09) due to the conflict with #19569.

  101. DrahtBot removed the label Needs rebase on Aug 21, 2020
  102. DrahtBot cross-referenced this on Aug 21, 2020 from issue p2p: don't add AlreadyHave transactions to recentRejects by troygiorshev
  103. DrahtBot cross-referenced this on Aug 21, 2020 from issue Net processing: move ProcessMessage() to PeerLogicValidation by jnewbery
  104. DrahtBot added the label Needs rebase on Aug 24, 2020
  105. hebasto force-pushed on Aug 24, 2020
  106. hebasto commented at 7:14 PM on August 24, 2020: member

    Rebased 95b4daa91ea4eecd3f345a20f285fb0528f5070d -> 1faf43ac3b2cb2a116f501e56c2cd6fed903409c (pr19306.09 -> pr19306.10) due to the conflict with #19704.

  107. DrahtBot removed the label Needs rebase on Aug 24, 2020
  108. DrahtBot cross-referenced this on Aug 24, 2020 from issue [net processing] Move Misbehaving() to PeerManager by jnewbery
  109. DrahtBot cross-referenced this on Aug 26, 2020 from issue validation: UTXO snapshot activation by jamesob
  110. DrahtBot cross-referenced this on Aug 28, 2020 from issue Pass mempool reference to chainstate constructor by MarcoFalke
  111. DrahtBot added the label Needs rebase on Aug 31, 2020
  112. hebasto cross-referenced this on Sep 1, 2020 from issue Avoid locking CTxMemPool::cs recursively in simple cases by hebasto
  113. laanwj referenced this in commit 99a8eb6051 on Sep 4, 2020
  114. hebasto marked this as a draft on Sep 4, 2020
  115. hebasto cross-referenced this on Sep 4, 2020 from issue Avoid locking CTxMemPool::cs recursively in some cases by hebasto
  116. refactor: Prevent double lock in MempoolToJSON() 8f1767d115
  117. refactor: Add thread context to AcceptToMemoryPool() call sites c903bf7389
  118. refactor: Prevent double lock in AcceptToMemoryPool() 7878cfc4af
  119. refactor: Specify CChainState::FlushStateToDisk() by mempool mutex state b3d44b9dca
  120. refactor: Avoid excessive mempool locking in FlushStateToDiskHelper() afcf9161ce
  121. refactor: Remove excessive locking in CTxMemPool methods 52ec60d7bd
  122. refactor: Make CTxMemPool::ClearPrioritisation() private f2825fa7b5
  123. refactor: Add negative thread safety annotations to CTxMemPool ec98daf0ab
  124. refactor: Prevent double lock in CTxMemPool::check() 26da318c12
  125. refactor: Prevent double lock in CTxMemPool::clear() 87d624c46c
  126. refactor: Prevent double lock in CTxMemPool::isSpent() b9b8dfd957
  127. refactor: Prevent double lock in CTxMemPool::GetMinFee() 12922a0b65
  128. refactor: Prevent double lock in CTxMemPool::GetTransactionAncestry() a249d59dc7
  129. refactor: Prevent double lock in CTxMemPool::IsLoaded() 89dfadc589
  130. refactor: Prevent double lock in CTxMemPool::size() e1eab65372
  131. refactor: Prevent double lock in CTxMemPool::exists() ae48014680
  132. refactor: Prevent double lock in CTxMemPool::get() ce19fb90d4
  133. refactor: Prevent double lock in CTxMemPool::infoAll() 5eac81656b
  134. refactor: Prevent double lock in CTxMemPool::DynamicMemoryUsage() 1b68b74485
  135. refactor: Prevent double lock in CTxMemPool::RemoveUnbroadcastTx() a2eb36cb98
  136. refactor: Prevent double lock in CTxMemPool::GetUnbroadcastTxs() cbcdf19e49
  137. refactor: Prevent double lock in PartiallyDownloadedBlock::InitData() 5639ecbb7d
  138. refactor: Prevent double lock in BlockAssembler::CreateNewBlock(() c37916a81b
  139. refactor: Prevent double lock in CheckInputsFromMempoolAndCache() e6a0279063
  140. refactor: Replace RecursiveMutex with Mutex in CTxMemPool a887d73dcb
  141. hebasto force-pushed on Sep 6, 2020
  142. hebasto commented at 12:46 PM on September 6, 2020: member

    Rebased 1faf43ac3b2cb2a116f501e56c2cd6fed903409c -> a887d73dcb05d59067635aff91baf85e0c7c7396 (pr19306.10 -> pr19306.12) due to the merge conflicts.

  143. hebasto marked this as ready for review on Sep 6, 2020
  144. DrahtBot removed the label Needs rebase on Sep 6, 2020
  145. hebasto cross-referenced this on Sep 6, 2020 from issue Avoid locking CTxMemPool::cs recursively in CTxMemPool::DynamicMemoryUsage() by hebasto
  146. hebasto marked this as a draft on Sep 6, 2020
  147. sidhujag referenced this in commit 45b8840fd3 on Sep 9, 2020
  148. hebasto closed this on Aug 24, 2021

  149. bitcoin locked this on Aug 24, 2022

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-05-20 06:54 UTC