Repository navigation
fix(consensus): release cs.mtx before sending to statsMsgQueue (backport #5813) - #5999
Merged
Merged
Conversation
## Problem `handleMsg` sent to `cs.statsMsgQueue` while holding `cs.mtx`. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine. ## Fix Hoist the `statsMsgQueue` send outside the `switch`, release `cs.mtx` before the send and re-acquire after. This also removes the duplicated `added` checks inside each `case` block, replacing them with a single check at the bottom. ## Testing - `TestHandleMsgReleasesLockBeforeStatsMsgQueueSend`: verifies `GetRoundState()` is not blocked while `handleMsg` is blocked on the send. --- #### PR checklist - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit bd8bb65)
Contributor
|
PR author is not in the allowed authors list. |
|
🔒 WARNING: unsigned commits detected This pull request contains 1 commit(s) without a verified signature. How to fix:
Unsigned commits:
|
swift1337
approved these changes
Jul 28, 2026
swift1337
added this pull request to the merge queue
Jul 28, 2026
This was referenced Jul 28, 2026
JayT106
pushed a commit
to crypto-org-chain/cometbft
that referenced
this pull request
Jul 28, 2026
…ort cometbft#5813) (cometbft#5999) ## Problem `handleMsg` sent to `cs.statsMsgQueue` while holding `cs.mtx`. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine. ## Fix Hoist the `statsMsgQueue` send outside the `switch`, release `cs.mtx` before the send and re-acquire after. This also removes the duplicated `added` checks inside each `case` block, replacing them with a single check at the bottom. ## Testing - `TestHandleMsgReleasesLockBeforeStatsMsgQueueSend`: verifies `GetRoundState()` is not blocked while `handleMsg` is blocked on the send. --- #### PR checklist - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5813 done by [Mergify](https://mergify.com). Co-authored-by: songgaoye <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit 4f1535b)
JayT106
added a commit
to crypto-org-chain/cometbft
that referenced
this pull request
Jul 29, 2026
* fix(consensus): release cs.mtx before sending to statsMsgQueue (backport cometbft#5813) (cometbft#5999) ## Problem `handleMsg` sent to `cs.statsMsgQueue` while holding `cs.mtx`. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine. ## Fix Hoist the `statsMsgQueue` send outside the `switch`, release `cs.mtx` before the send and re-acquire after. This also removes the duplicated `added` checks inside each `case` block, replacing them with a single check at the bottom. ## Testing - `TestHandleMsgReleasesLockBeforeStatsMsgQueueSend`: verifies `GetRoundState()` is not blocked while `handleMsg` is blocked on the send. --- #### PR checklist - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5813 done by [Mergify](https://mergify.com). Co-authored-by: songgaoye <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit 4f1535b) * fix(consensus): correct loop condition in checkDoubleSigningRisk for height=1 (backport cometbft#5668) (cometbft#6000) Fixes: cometbft#5435 When `double_sign_check_height` is set to `1`, the loop condition `i := int64(1); i < 1` is never satisfied, resulting in zero iterations and no double sign checks being performed. This makes the setting functionally equivalent to `0` (disabled), silently giving validators false confidence in their double-sign protection. Changed the loop condition from `i < doubleSignCheckHeight` to `i <= doubleSignCheckHeight` so that a value of `1` correctly checks `height - 1`, matching the documented intent of the configuration parameter. The existing guard `if doubleSignCheckHeight > height` already prevents out-of-bounds lookups on short chains, so no additional bounds protection is needed. - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5668 done by [Mergify](https://mergify.com). --------- Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit 222949a) * fix(blocksync): tolerate late BlockResponse from honest peers after switching to consensus (backport cometbft#5959) (cometbft#6002) `FilterMsgBytes` returned an error for any `BlockResponse` once the block pool had stopped (`!r.pool.IsRunning()`). That error panicked inside `p2p/peer.go`'s `onReceive` and was recovered into `StopPeerForError` — a hard disconnect. Because requests for the last blocks are typically still in-flight when the catch-up ticker fires and `pool.Stop()` is called, both request targets (primary and secondary peer) were disconnected on every normal sync completion, exactly when the node needs them for consensus gossip. Root cause introduced by cometbft#5860 (`FilterMsgBytes`). Not covered by cometbft#5950, which handles the second-peer race while the pool is still running. Split the single `||` guard into two branches: - `!r.enabled.Load()` — node never ran blocksync; any `BlockResponse` is genuinely unsolicited → reject (unchanged behaviour) - `!r.pool.IsRunning()` — pool stopped for consensus switch; requests we sent before the transition are still in-flight; the peers are honest → allow, but still run `validateMaxVotes` on the response The `AddBlock` call that follows returns `"already committed block"` and logs it; no peer state is mutated and no disconnect occurs. `pool.sendError` is already a no-op once the pool is stopped (it guards on `!pool.IsRunning()`), so the pre-fix punishment for genuinely out-of-window responses in this window was already silently dropped the moment `poolEventsRoutine` exited. The post-stop branch skips the `HasPendingRequestFrom` peer-identity check (any peer, not just the one we solicited, may legitimately answer a stale request) but still enforces `validateMaxVotes` — once the pool stops, any connected peer can reach this path, and the commit-signature cap exists to prevent a heap-amplification DoS regardless of sender. Caught by review on this PR; added a dedicated regression test (`rejects oversized BlockResponse after pool stops for consensus switch`) so an oversized response is still rejected in this window. --- - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments <hr>This is an automatic backport of pull request cometbft#5959 done by [Mergify](https://mergify.com). --------- Co-authored-by: JayT106 <[email protected]> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit 6ed1d96) * ci: run e2e-test on ubuntu-latest, not unavailable Depot runner Fork has no Depot runner registered, so depot-ubuntu-24.04-4 jobs queue indefinitely and get cancelled at the 24h timeout. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: songgaoye <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: jay.tseng <[email protected]>
JayT106
added a commit
to crypto-org-chain/cometbft
that referenced
this pull request
Oct 30, 2026
* fix(consensus): release cs.mtx before sending to statsMsgQueue (backport cometbft#5813) (cometbft#5999) `handleMsg` sent to `cs.statsMsgQueue` while holding `cs.mtx`. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine. Hoist the `statsMsgQueue` send outside the `switch`, release `cs.mtx` before the send and re-acquire after. This also removes the duplicated `added` checks inside each `case` block, replacing them with a single check at the bottom. - `TestHandleMsgReleasesLockBeforeStatsMsgQueueSend`: verifies `GetRoundState()` is not blocked while `handleMsg` is blocked on the send. --- - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5813 done by [Mergify](https://mergify.com). Co-authored-by: songgaoye <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Dmitry S <[email protected]> On v0.40.0 only the e2e runner change applies; the upstream commits are already in v0.40.0. (cherry picked from commit 4f1535b) * fix(consensus): correct loop condition in checkDoubleSigningRisk for height=1 (backport cometbft#5668) (cometbft#6000) Fixes: cometbft#5435 When `double_sign_check_height` is set to `1`, the loop condition `i := int64(1); i < 1` is never satisfied, resulting in zero iterations and no double sign checks being performed. This makes the setting functionally equivalent to `0` (disabled), silently giving validators false confidence in their double-sign protection. Changed the loop condition from `i < doubleSignCheckHeight` to `i <= doubleSignCheckHeight` so that a value of `1` correctly checks `height - 1`, matching the documented intent of the configuration parameter. The existing guard `if doubleSignCheckHeight > height` already prevents out-of-bounds lookups on short chains, so no additional bounds protection is needed. - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5668 done by [Mergify](https://mergify.com). --------- Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> On v0.40.0 only the e2e runner change applies; the upstream commits are already in v0.40.0. (cherry picked from commit 222949a) * fix(blocksync): tolerate late BlockResponse from honest peers after switching to consensus (backport cometbft#5959) (cometbft#6002) `FilterMsgBytes` returned an error for any `BlockResponse` once the block pool had stopped (`!r.pool.IsRunning()`). That error panicked inside `p2p/peer.go`'s `onReceive` and was recovered into `StopPeerForError` — a hard disconnect. Because requests for the last blocks are typically still in-flight when the catch-up ticker fires and `pool.Stop()` is called, both request targets (primary and secondary peer) were disconnected on every normal sync completion, exactly when the node needs them for consensus gossip. Root cause introduced by cometbft#5860 (`FilterMsgBytes`). Not covered by cometbft#5950, which handles the second-peer race while the pool is still running. Split the single `||` guard into two branches: - `!r.enabled.Load()` — node never ran blocksync; any `BlockResponse` is genuinely unsolicited → reject (unchanged behaviour) - `!r.pool.IsRunning()` — pool stopped for consensus switch; requests we sent before the transition are still in-flight; the peers are honest → allow, but still run `validateMaxVotes` on the response The `AddBlock` call that follows returns `"already committed block"` and logs it; no peer state is mutated and no disconnect occurs. `pool.sendError` is already a no-op once the pool is stopped (it guards on `!pool.IsRunning()`), so the pre-fix punishment for genuinely out-of-window responses in this window was already silently dropped the moment `poolEventsRoutine` exited. The post-stop branch skips the `HasPendingRequestFrom` peer-identity check (any peer, not just the one we solicited, may legitimately answer a stale request) but still enforces `validateMaxVotes` — once the pool stops, any connected peer can reach this path, and the commit-signature cap exists to prevent a heap-amplification DoS regardless of sender. Caught by review on this PR; added a dedicated regression test (`rejects oversized BlockResponse after pool stops for consensus switch`) so an oversized response is still rejected in this window. --- - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments <hr>This is an automatic backport of pull request cometbft#5959 done by [Mergify](https://mergify.com). --------- Co-authored-by: JayT106 <[email protected]> Co-authored-by: Dmitry S <[email protected]> On v0.40.0 only the e2e runner change applies; the upstream commits are already in v0.40.0. (cherry picked from commit 6ed1d96) * ci: run e2e-test on ubuntu-latest, not unavailable Depot runner Fork has no Depot runner registered, so depot-ubuntu-24.04-4 jobs queue indefinitely and get cancelled at the 24h timeout. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: songgaoye <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: jay.tseng <[email protected]> On v0.40.0 only the e2e runner change applies; the upstream commits are already in v0.40.0. (cherry picked from commit 14b7b93)
JayT106
added a commit
to crypto-org-chain/cometbft
that referenced
this pull request
Nov 2, 2026
* fix(consensus): release cs.mtx before sending to statsMsgQueue (backport cometbft#5813) (cometbft#5999) ## Problem `handleMsg` sent to `cs.statsMsgQueue` while holding `cs.mtx`. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine. ## Fix Hoist the `statsMsgQueue` send outside the `switch`, release `cs.mtx` before the send and re-acquire after. This also removes the duplicated `added` checks inside each `case` block, replacing them with a single check at the bottom. ## Testing - `TestHandleMsgReleasesLockBeforeStatsMsgQueueSend`: verifies `GetRoundState()` is not blocked while `handleMsg` is blocked on the send. --- #### PR checklist - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5813 done by [Mergify](https://mergify.com). Co-authored-by: songgaoye <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit 4f1535b) * fix(consensus): correct loop condition in checkDoubleSigningRisk for height=1 (backport cometbft#5668) (cometbft#6000) Fixes: cometbft#5435 When `double_sign_check_height` is set to `1`, the loop condition `i := int64(1); i < 1` is never satisfied, resulting in zero iterations and no double sign checks being performed. This makes the setting functionally equivalent to `0` (disabled), silently giving validators false confidence in their double-sign protection. Changed the loop condition from `i < doubleSignCheckHeight` to `i <= doubleSignCheckHeight` so that a value of `1` correctly checks `height - 1`, matching the documented intent of the configuration parameter. The existing guard `if doubleSignCheckHeight > height` already prevents out-of-bounds lookups on short chains, so no additional bounds protection is needed. - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments<hr>This is an automatic backport of pull request cometbft#5668 done by [Mergify](https://mergify.com). --------- Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> (cherry picked from commit 222949a) * fix(blocksync): tolerate late BlockResponse from honest peers after switching to consensus (backport cometbft#5959) (cometbft#6002) `FilterMsgBytes` returned an error for any `BlockResponse` once the block pool had stopped (`!r.pool.IsRunning()`). That error panicked inside `p2p/peer.go`'s `onReceive` and was recovered into `StopPeerForError` — a hard disconnect. Because requests for the last blocks are typically still in-flight when the catch-up ticker fires and `pool.Stop()` is called, both request targets (primary and secondary peer) were disconnected on every normal sync completion, exactly when the node needs them for consensus gossip. Root cause introduced by cometbft#5860 (`FilterMsgBytes`). Not covered by cometbft#5950, which handles the second-peer race while the pool is still running. Split the single `||` guard into two branches: - `!r.enabled.Load()` — node never ran blocksync; any `BlockResponse` is genuinely unsolicited → reject (unchanged behaviour) - `!r.pool.IsRunning()` — pool stopped for consensus switch; requests we sent before the transition are still in-flight; the peers are honest → allow, but still run `validateMaxVotes` on the response The `AddBlock` call that follows returns `"already committed block"` and logs it; no peer state is mutated and no disconnect occurs. `pool.sendError` is already a no-op once the pool is stopped (it guards on `!pool.IsRunning()`), so the pre-fix punishment for genuinely out-of-window responses in this window was already silently dropped the moment `poolEventsRoutine` exited. The post-stop branch skips the `HasPendingRequestFrom` peer-identity check (any peer, not just the one we solicited, may legitimately answer a stale request) but still enforces `validateMaxVotes` — once the pool stops, any connected peer can reach this path, and the commit-signature cap exists to prevent a heap-amplification DoS regardless of sender. Caught by review on this PR; added a dedicated regression test (`rejects oversized BlockResponse after pool stops for consensus switch`) so an oversized response is still rejected in this window. --- - [x] Tests written/updated - [x] Changelog entry added in `CHANGELOG.md` - [ ] Updated relevant documentation (`docs/` or `spec/`) and code comments <hr>This is an automatic backport of pull request cometbft#5959 done by [Mergify](https://mergify.com). --------- Co-authored-by: JayT106 <[email protected]> Co-authored-by: Dmitry S <[email protected]> (cherry picked from commit 6ed1d96) * ci: run e2e-test on ubuntu-latest, not unavailable Depot runner Fork has no Depot runner registered, so depot-ubuntu-24.04-4 jobs queue indefinitely and get cancelled at the 24h timeout. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: songgaoye <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: Aditya Mishra <[email protected]> Co-authored-by: jay.tseng <[email protected]> Port note (v0.40.0): only the e2e runner change applies; the upstream commits are already in v0.40.0. Also pins buf to v1.72.0 (upstream cometbft#6057; buf@latest needs Go 1.26.7 and broke check-proto) and fixes the flaky TestPEXReactorSeedModeFlushStop. (cherry picked from commit 14b7b93)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
handleMsgsent tocs.statsMsgQueuewhile holdingcs.mtx. If the buffer fills, the send blocks indefinitely — stalling the entire consensus state machine.Fix
Hoist the
statsMsgQueuesend outside theswitch, releasecs.mtxbefore the send and re-acquire after.This also removes the duplicated
addedchecks inside eachcaseblock, replacing them with a single check at the bottom.Testing
TestHandleMsgReleasesLockBeforeStatsMsgQueueSend: verifiesGetRoundState()is not blocked whilehandleMsgis blocked on the send.PR checklist
CHANGELOG.mddocs/orspec/) and code commentsThis is an automatic backport of pull request fix(consensus): release cs.mtx before sending to statsMsgQueue #5813 done by Mergify.