Skip to content

Conversation

theuni
Copy link
Member

@theuni theuni commented May 22, 2025

Now that #32467 is merged, the only remaining usage of our old CRITICAL_SECTION macros (other than tests) is in getblocktemplate() and it can safely be replaced with a REVERSE_LOCK.

This PR makes that replacement, replaces the old CRITICAL_SECTION macro usage in tests, then deletes the macros themselves.

While testing this a few weeks ago, I noticed that REVERSE_LOCK does not currently work properly with our thread-safety annotations as after the REVERSE_LOCK is acquired, clang still believes that the mutex is locked. #32465 fixes this problem. Without that fix, this PR would potentially allow a false-negative if code were added in the future to this chunk of getblocktemplate which required cs_main to be locked.

I added a test for the reverse lock here in the form of a compiler warning in reverselock_tests.cpp to simulate that possibility. This PR will therefore cause a new warning (and should fail a warnings-as-errors ci check) until #32465 is merged and this is rebased on top of it.

Edit: Rebased on top of #32465, so this should now pass tests.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 22, 2025

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32592.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK TheCharlatan, maflcko, fjahr, furszy
Concept ACK hebasto

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed.
Task MSan, depends: https://github.com/bitcoin/bitcoin/runs/42732951890
LLM reason (✨ experimental): The CI failure is due to a thread safety error in reverselock_tests.cpp.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@theuni theuni force-pushed the remove-critsect branch from 3503d51 to ee98ad2 Compare May 22, 2025 19:20
@theuni
Copy link
Member Author

theuni commented May 22, 2025

As mentioned in the description, failing tests are just demonstrating the borked REVERSE_LOCK fixed by #32465.

@hebasto
Copy link
Member

hebasto commented May 22, 2025

Concept ACK.

@fjahr
Copy link
Contributor

fjahr commented May 26, 2025

Concept ACK

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Code review ACK ee98ad2, will hop into #32465.

@DrahtBot DrahtBot requested review from hebasto and fjahr May 26, 2025 13:56
@theuni
Copy link
Member Author

theuni commented May 28, 2025

Pushed a small additional commit that removes a now-unnecessary template instantiation. Just a little janitorial work, but it will make some future changes more obviously correct.

@fanquake
Copy link
Member

https://cirrus-ci.com/task/6734127736553472?logs=ci#L3223

[12:12:21.305] /ci_container_base/src/test/reverselock_tests.cpp:23:9: error: cannot call function 'AssertLockNotHeldInline' while mutex 'mutex' is held [-Werror,-Wthread-safety-analysis]
[12:12:21.305]    23 |         AssertLockNotHeld(mutex);
[12:12:21.305]       |         ^
[12:12:21.305] /ci_container_base/src/sync.h:141:31: note: expanded from macro 'AssertLockNotHeld'
[12:12:21.305]   141 | #define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
[12:12:21.305]       |                               ^
[12:12:21.305] 1 error generated.

@theuni
Copy link
Member Author

theuni commented May 29, 2025

https://cirrus-ci.com/task/6734127736553472?logs=ci#L3223

[12:12:21.305] /ci_container_base/src/test/reverselock_tests.cpp:23:9: error: cannot call function 'AssertLockNotHeldInline' while mutex 'mutex' is held [-Werror,-Wthread-safety-analysis]
[12:12:21.305]    23 |         AssertLockNotHeld(mutex);
[12:12:21.305]       |         ^
[12:12:21.305] /ci_container_base/src/sync.h:141:31: note: expanded from macro 'AssertLockNotHeld'
[12:12:21.305]   141 | #define AssertLockNotHeld(cs) AssertLockNotHeldInline(#cs, __FILE__, __LINE__, &cs)
[12:12:21.305]       |                               ^
[12:12:21.305] 1 error generated.

As expected :) From description:

I added a test for the reverse lock here in the form of a compiler warning in reverselock_tests.cpp to simulate that possibility. This PR will therefore cause a new warning (and should fail a warnings-as-errors ci check) until #32465 is merged and this is rebased on top of it.

@fanquake fanquake marked this pull request as draft May 30, 2025 09:40
@fanquake
Copy link
Member

fanquake commented Jul 1, 2025

@theuni want to rebase this now that #32465 is in?

@fanquake fanquake added this to the 30.0 milestone Jul 15, 2025
@fanquake
Copy link
Member

Put this on 30.0.

@maflcko
Copy link
Member

maflcko commented Aug 7, 2025

This is tagged for 30.0, but feature freeze is in less than two weeks, and it still needs rebase, so it'll likely miss the milestone.

@fanquake
Copy link
Member

This is tagged for 30.0, but feature freeze is in less than two weeks, and it still needs rebase, so it'll likely miss the milestone.

Feature freeze doesn't seem important here, given this isn't a feature; it's part of a cleanup of which the prior PRs have already landed, so landing this any time before branch off seems fine (assuming it gets rebased, have pinged @theuni).

@theuni theuni marked this pull request as ready for review August 22, 2025 14:33
Copy link
Contributor

@TheCharlatan TheCharlatan left a comment

Choose a reason for hiding this comment

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

ACK 46ca771

@@ -704,7 +704,7 @@ static RPCHelpMan getblocktemplate()
NodeContext& node = EnsureAnyNodeContext(request.context);
ChainstateManager& chainman = EnsureChainman(node);
Mining& miner = EnsureMining(node);
LOCK(cs_main);
WAIT_LOCK(cs_main, csmain_lock);
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really related to this PR, but I really don't like how long the scope of the lock is here. We could reduce this a bit by doing something like

diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index b710c605bc..750ca72bb4 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -707,2 +706,0 @@ static RPCHelpMan getblocktemplate()
-    WAIT_LOCK(cs_main, csmain_lock);
-    uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
@@ -737,0 +736 @@ static RPCHelpMan getblocktemplate()
+            LOCK(cs_main);
@@ -775,0 +775,3 @@ static RPCHelpMan getblocktemplate()
+    WAIT_LOCK(cs_main, cs_main_lock);
+    uint256 tip{CHECK_NONFATAL(miner.getTip()).value().hash};
+
@@ -814 +816 @@ static RPCHelpMan getblocktemplate()
-            REVERSE_LOCK(csmain_lock, cs_main);
+            REVERSE_LOCK(cs_main_lock, cs_main);

but then going on to hold it through the entire marshalling block also does not seem ideal.

@DrahtBot DrahtBot requested a review from furszy August 22, 2025 21:22
@maflcko
Copy link
Member

maflcko commented Aug 23, 2025

review ACK 46ca771 📌

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: review ACK 46ca7712cb5fcf759cfc9f4f32d74215c8c83763 📌
V2n3Eh8Fc5sBB4pkAgZwhO+niN3JRgn02KN6u3xaWJyYem/L+0JNJeiBY543bvMUZO6moB6cpFgOcoXMz3FDDA==

@fjahr
Copy link
Contributor

fjahr commented Aug 23, 2025

Code review ACK 46ca771

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

ACK 46ca771

@fanquake fanquake merged commit 9703b7e into bitcoin:master Aug 23, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants