Skip to content

fix(consensus): release cs.mtx before sending to statsMsgQueue (backport #5813) - #5999

Merged
swift1337 merged 1 commit into
v0.39.xfrom
mergify/bp/v0.39.x/pr-5813
Jul 28, 2026
Merged

swift1337 merged 1 commit into
v0.39.xfrom
mergify/bp/v0.39.x/pr-5813

Conversation

@mergify

@mergify mergify Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

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

## 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)
@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

@github-actions

Copy link
Copy Markdown

🔒 WARNING: unsigned commits detected

This pull request contains 1 commit(s) without a verified signature.

How to fix:

  1. Set up commit signing (GPG or SSH).
  2. Amend/rebase so every commit in this PR is verified-signed.
  3. Push the updated branch and open a new PR, or ask a maintainer to reopen once fixed.

Docs: https://docs.github.com/authentication/managing-commit-signature-verification

Unsigned commits:

@swift1337
swift1337 added this pull request to the merge queue Jul 28, 2026
Merged via the queue into v0.39.x with commit 4f1535b Jul 28, 2026
34 checks passed
@swift1337
swift1337 deleted the mergify/bp/v0.39.x/pr-5813 branch July 28, 2026 10:57
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)
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.

2 participants