GUARDED_BY annotations for nTx, m_chain_tx_count #36876

Manually merged
Crypt-iQ merged 1 commit from 2026-11/cblockindex-guard into master 2026-12-01 10:27:09 +00:00 AGit
Crypt-iQ commented 2026-11-14 21:37:23 +00:00 (Migrated from github.com)

The cs_main requirement was never made explicit. The nTx field is modified when the block data is received and m_chain_tx_count is modified when all ancestors' block data has been received. Because of this, reading nTx from the getblockheader RPC may race as it does not acquire cs_main. Also, m_chain_tx_count may race in getchaintxstats if a pruned node redownloads a block via getblockfrompeer or if the node is running assumeutxo and tries to read m_chain_tx_count while it is being written to.

The cs_main requirement was never made explicit. The nTx field is modified when the block data is received and m_chain_tx_count is modified when all ancestors' block data has been received. Because of this, reading nTx from the getblockheader RPC may race as it does not acquire cs_main. Also, m_chain_tx_count may race in getchaintxstats if a pruned node redownloads a block via getblockfrompeer or if the node is running assumeutxo and tries to read m_chain_tx_count while it is being written to.
DrahtBot commented 2026-11-14 21:37:31 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK maflcko, fjahr
Stale ACK darosior

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36598 ([Demo] validation: remove AssumeUTXO by l0rinc)
  • #36543 (validation: check the most recent AssumeUTXO commitment during IBD by l0rinc)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36876 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36876.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36876#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [maflcko](https://github.com/bitcoin/bitcoin/pull/36876#issuecomment-7280760798), [fjahr](https://github.com/bitcoin/bitcoin/pull/36876#issuecomment-7291103199) | | Stale ACK | [darosior](https://github.com/bitcoin/bitcoin/pull/36876#pullrequestreview-5922185024) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36598](https://github.com/bitcoin/bitcoin/pull/36598) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc) * [#36543](https://github.com/bitcoin/bitcoin/pull/36543) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36543.svg"></sub> (validation: check the most recent AssumeUTXO commitment during IBD by l0rinc) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Header JSON serialization reacquires cs_main for every header

With REST enabled, JSON /headers requests gain up to 2,000 avoidable cs_main acquisitions, although their latency impact has not been measured.

Location: src/rpc/blockchain.cpp:217 (head).

Review coverage was incomplete.


Reviewed: 2f7a0750ef2b · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:312ddb2095238aa2ca395a9cc39b05ab5a38a7177afbdb0d69098df9c3e0bef9:9a7f127b15c6116c267953d38288a9f17ec7c7bcd3a975fb71a2db8bfd974dbf --> <!-- ralph-base:483ed3be8d13b50996bf0f81d4f55487b6b938dd --> <!-- ralph-head:2f7a0750ef2bc38c38adfd90e7afda2728c6bf76 --> <!-- ralph-case:d4037f0b22fbd38d62b68e7f232d4b70355773b4f63e08aed1b6f2aa190118ea --> **🔵 Suggestion · Header JSON serialization reacquires `cs_main` for every header** With REST enabled, JSON `/headers` requests gain up to 2,000 avoidable `cs_main` acquisitions, although their latency impact has not been measured. Location: [`src/rpc/blockchain.cpp:217`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2f7a0750ef2bc38c38adfd90e7afda2728c6bf76/src/rpc/blockchain.cpp#L217>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`2f7a0750ef2b`](<https://git.fish.foo/bitcoin/bitcoin/commit/2f7a0750ef2bc38c38adfd90e7afda2728c6bf76>) · [Review report](<https://review.fish.foo/report/36876-2f7a0750ef2bc38c38adfd90e7afda2728c6bf76-3351-4.html>) · [Previous reports](<https://review.fish.foo/traces/36876/>) [permalink](<https://review.fish.foo/report/36876-2f7a0750ef2bc38c38adfd90e7afda2728c6bf76-3351-4.html>)
fanquake commented 2026-11-15 02:20:27 +00:00 (Migrated from github.com)

cc @instagibbs

cc @instagibbs
darosior (Migrated from github.com) approved these changes 2026-11-16 15:03:57 +00:00
darosior (Migrated from github.com) left a comment

utACK 8775bebbde

utACK 8775bebbde2eac226a44a7ce88f9c0e7db9cd279
instagibbs (Migrated from github.com) reviewed 2026-11-16 15:39:44 +00:00
instagibbs (Migrated from github.com) commented 2026-11-16 15:39:44 +00:00
micro nit: https://github.com/bitcoin/bitcoin/pull/36876/changes#diff-decae4be02fb8a47ab4557fe74a9cb853bdfa3ec0fa1b515c0a1e5de91f4ad0bR197 above says it's not held, but now it is. Seems to mostly impact REST, didn't checkmark it.
Crypt-iQ (Migrated from github.com) reviewed 2026-11-16 15:44:13 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-11-16 15:44:13 +00:00

Yes, also the comment seems to imply that the function won't lock which is false. Should I remove the comment and the AssertLockNotHeld?

Yes, also the comment seems to imply that the function won't lock which is false. Should I remove the comment and the `AssertLockNotHeld`?
instagibbs (Migrated from github.com) reviewed 2026-11-16 15:56:45 +00:00
instagibbs (Migrated from github.com) commented 2026-11-16 15:56:45 +00:00

seems clearer yes

seems clearer yes
Crypt-iQ (Migrated from github.com) reviewed 2026-11-16 17:43:31 +00:00
Crypt-iQ (Migrated from github.com) commented 2026-11-16 17:43:31 +00:00

Done in ad6d75b196, which also removed the LOCKS_EXCLUDED macros

Done in ad6d75b196d301f1bfe430b8d05a23c70b5ea64a, which also removed the `LOCKS_EXCLUDED` macros
Crypt-iQ commented 2026-11-16 17:45:22 +00:00 (Migrated from github.com)

8775beb -> ad6d75b196

Removes the AssertLockNotHeld in blockheaderToJSON and also removes the LOCKS_EXCLUDED macros for blockheaderToJSON and blockToJSON. The intent was to show that these functions don't acquire the lock, which is no longer true.

8775beb -> ad6d75b196d301f1bfe430b8d05a23c70b5ea64a Removes the `AssertLockNotHeld` in `blockheaderToJSON` and also removes the `LOCKS_EXCLUDED` macros for `blockheaderToJSON` and `blockToJSON`. The intent was to show that these functions don't acquire the lock, which is no longer true.
fjahr (Migrated from github.com) reviewed 2026-11-20 09:09:06 +00:00
fjahr (Migrated from github.com) left a comment

Concept ACK

Concept ACK
Lines 45-48
fjahr (Migrated from github.com) commented 2026-11-20 09:08:50 +00:00

Hm, I'm a bit confused by this. blockToJSON already was grabbing cs_main inside of it, but still it had the annotation here. So was this a mistake? Otherwise it would seem fine to just leave them for both since just adding the lock grabbing to blockheaderToJSON doesn't seem like the right argument to remove it. We seem to have an example for this in the developer notes even for the PreciousBlock(): https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md?plain=1#L1056 And there are a couple of other functions that do this too. While this is not as critical here as elsewhere, the performance argument from the deleted comment probably still holds?

Hm, I'm a bit confused by this. `blockToJSON` already was grabbing `cs_main` inside of it, but still it had the annotation here. So was this a mistake? Otherwise it would seem fine to just leave them for both since just adding the lock grabbing to `blockheaderToJSON` doesn't seem like the right argument to remove it. We seem to have an example for this in the developer notes even for the `PreciousBlock()`: https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md?plain=1#L1056 And there are a couple of other functions that do this too. While this is not as critical here as elsewhere, the performance argument from the deleted comment probably still holds?
maflcko (Migrated from github.com) reviewed 2026-11-20 10:12:54 +00:00
Lines 45-48
maflcko (Migrated from github.com) commented 2026-11-20 10:12:54 +00:00

This was added in faea56400d. The annotation just says that the caller must not hold the given capabilities. It doesn't say anything about the function itself.

Anything is fine here. It can be dropped, or it can be kept.

This was added in faea56400d5578023133cf4d1c761cdeb0c3e3da. The annotation just says that the *caller* must not hold the given capabilities. It doesn't say anything about the function itself. Anything is fine here. It can be dropped, or it can be kept.
maflcko commented 2026-11-20 10:14:16 +00:00 (Migrated from github.com)

reproduced locally:

2026-11-20T10:11:15Z UpdateTip: new best=7afecf9bd6680a2a56596b5ee28dc5bbd208781986b175fe479db4866e2c9072 height=67 version=0x20000000 log2_work=7.087463 tx=68 date='2026-11-20T10:11:15Z' progress=1.000000 cache=0.3MiB(67txo)
==================
WARNING: ThreadSanitizer: data race (pid=560863)
  Write of size 4 at 0x7234000b4000 by thread T4 (mutexes: write M0):
    #0 ChainstateManager::ReceivedBlockTransactions(CBlock const&, CBlockIndex*, FlatFilePos const&) src/validation.cpp:3849
...

vs:

  Previous read of size 4 at 0x7234000b4000 by thread T7:
    #1 blockheaderToJSON(CBlockIndex const&, CBlockIndex const&, uint256) rpc/blockchain.cpp:215
reproduced locally: ``` 2026-11-20T10:11:15Z UpdateTip: new best=7afecf9bd6680a2a56596b5ee28dc5bbd208781986b175fe479db4866e2c9072 height=67 version=0x20000000 log2_work=7.087463 tx=68 date='2026-11-20T10:11:15Z' progress=1.000000 cache=0.3MiB(67txo) ================== WARNING: ThreadSanitizer: data race (pid=560863) Write of size 4 at 0x7234000b4000 by thread T4 (mutexes: write M0): #0 ChainstateManager::ReceivedBlockTransactions(CBlock const&, CBlockIndex*, FlatFilePos const&) src/validation.cpp:3849 ... vs: Previous read of size 4 at 0x7234000b4000 by thread T7: #1 blockheaderToJSON(CBlockIndex const&, CBlockIndex const&, uint256) rpc/blockchain.cpp:215
maflcko commented 2026-11-20 10:15:20 +00:00 (Migrated from github.com)

review ACK ad6d75b196 🏵

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 ad6d75b196d301f1bfe430b8d05a23c70b5ea64a 🏵
d5+/dbHwOHGHOMPFVsyugPq0JCuvMrqu0+4K+s5aGGySvgSe5VSUfLJKW0HW94mV6jeljBWECZx/2t0GRc48Dg==
review ACK ad6d75b196d301f1bfe430b8d05a23c70b5ea64a 🏵 <details><summary>Show signature</summary> 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 ad6d75b196d301f1bfe430b8d05a23c70b5ea64a 🏵 d5+/dbHwOHGHOMPFVsyugPq0JCuvMrqu0+4K+s5aGGySvgSe5VSUfLJKW0HW94mV6jeljBWECZx/2t0GRc48Dg== ``` </details>
Crypt-iQ (Migrated from github.com) reviewed 2026-11-20 12:59:32 +00:00
Lines 45-48
Crypt-iQ (Migrated from github.com) commented 2026-11-20 12:59:32 +00:00

Thanks, this was poor justification, I was confused by the comment. I've reverted it in the latest push.

Thanks, this was poor justification, I was confused by the comment. I've reverted it in the latest push.
fjahr commented 2026-11-20 13:09:03 +00:00 (Migrated from github.com)

Code review ACK 32429d7b6b

The latest push cleared up the only question I had. The CI failures look like the unrelated failures we currently see a lot.

Code review ACK 32429d7b6b92046b17e8fa1ee880a482d03055c5 The latest push cleared up the only question I had. The CI failures look like the unrelated failures we currently see a lot.
maflcko commented 2026-11-20 15:58:03 +00:00 (Migrated from github.com)

the commit looks identical to 8775bebbde, so I think you could have pushed that, to avoid re-review churn, but up to you. I'll ack both:

(1/2)

review ACK 8775bebbde 💿

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 8775bebbde2eac226a44a7ce88f9c0e7db9cd279 💿
76LvMIrFUa/yDrDYIam5pUzLXzVz6Bc3wEXF7ulziDvIesSNsn94MWpA6tTuzm1hbKyhySm9Tl9E+im1Gw1yCA==
the commit looks identical to 8775bebbde2eac226a44a7ce88f9c0e7db9cd279, so I think you could have pushed that, to avoid re-review churn, but up to you. I'll ack both: (1/2) review ACK 8775bebbde2eac226a44a7ce88f9c0e7db9cd279 💿 <details><summary>Show signature</summary> 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 8775bebbde2eac226a44a7ce88f9c0e7db9cd279 💿 76LvMIrFUa/yDrDYIam5pUzLXzVz6Bc3wEXF7ulziDvIesSNsn94MWpA6tTuzm1hbKyhySm9Tl9E+im1Gw1yCA== ``` </details>
maflcko commented 2026-11-20 15:59:17 +00:00 (Migrated from github.com)

(2/2)

review ACK 32429d7b6b 🎻

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 32429d7b6b92046b17e8fa1ee880a482d03055c5 🎻
1bFaYlCzgtdV3wChhlJvHCIO8WQDIfQ+72+jzJWgb4EJnk5ExuLJ7qzdSOx4P+QGEuL3BPgdmervm6IZSBE0CA==
(2/2) review ACK 32429d7b6b92046b17e8fa1ee880a482d03055c5 🎻 <details><summary>Show signature</summary> 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 32429d7b6b92046b17e8fa1ee880a482d03055c5 🎻 1bFaYlCzgtdV3wChhlJvHCIO8WQDIfQ+72+jzJWgb4EJnk5ExuLJ7qzdSOx4P+QGEuL3BPgdmervm6IZSBE0CA== ``` </details>
Crypt-iQ commented 2026-11-20 16:04:11 +00:00 (Migrated from github.com)

so I think you could have pushed that, to avoid re-review churn, but up to you.

Ah, I did not realize they were identical. I'll keep it at github.com/bitcoin/bitcoin@32429d7b6b since at this point I will invalidate one ACK either way

> so I think you could have pushed that, to avoid re-review churn, but up to you. Ah, I did not realize they were identical. I'll keep it at https://github.com/bitcoin/bitcoin/commit/32429d7b6b92046b17e8fa1ee880a482d03055c5 since at this point I will invalidate one ACK either way
DrahtBot commented 2026-11-20 22:33:03 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
maflcko commented 2026-11-30 14:03:12 +00:00 (Migrated from github.com)

review ACK 2f7a0750ef 🚟

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 2f7a0750ef2bc38c38adfd90e7afda2728c6bf76 🚟
mygyPVbrOk5manRxhFZzU0u9V66LkEawdfA3rcG8QF8Euw7Re1BLFUEncluILpDdW23AbQa1Zn7i20x19ihBBA==
review ACK 2f7a0750ef2bc38c38adfd90e7afda2728c6bf76 🚟 <details><summary>Show signature</summary> 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 2f7a0750ef2bc38c38adfd90e7afda2728c6bf76 🚟 mygyPVbrOk5manRxhFZzU0u9V66LkEawdfA3rcG8QF8Euw7Re1BLFUEncluILpDdW23AbQa1Zn7i20x19ihBBA== ``` </details>
fjahr commented 2026-11-30 20:33:06 +00:00 (Migrated from github.com)
ACK 2f7a0750ef2bc38c38adfd90e7afda2728c6bf76
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36876
No description provided.