Skip to content

fix(blocksync): removeTimedoutPeers deadlock found via Byzantine prevote gossip race - #5839

Merged
swift1337 merged 3 commits into
cometbft:mainfrom
songgaoye:song/fix_TestByzantinePrevoteEquivocation
May 11, 2026
Merged

swift1337 merged 3 commits into
cometbft:mainfrom
songgaoye:song/fix_TestByzantinePrevoteEquivocation

Conversation

@songgaoye

@songgaoye songgaoye commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Fix the CI failure at:
https://github.com/cometbft/cometbft/actions/runs/25498058929/job/74823025038

That run revealed two issues:

  1. removeTimedoutPeers and onTimeout held pool.mtx while calling
    sendError, which blocks on errorsCh. The reactor loop (the only
    consumer) may itself wait for pool.mtx via AddBlock, forming a
    circular deadlock.

  2. The split-delivery of conflicting prevotes required gossip to
    propagate before height 2 committed, which raced under -race.
    Fix: send both conflicting prevotes to every peer directly so each
    validator detects the equivocation without relying on gossip timing.

Follows up on #5814

PR checklist

  • Tests written/updated
  • Changelog entry added in CHANGELOG.md
  • Updated relevant documentation (docs/ or spec/) and code comments

@songgaoye
songgaoye requested a review from a team as a code owner May 7, 2026 13:14
@songgaoye
songgaoye force-pushed the song/fix_TestByzantinePrevoteEquivocation branch from f0776de to e57476e Compare May 7, 2026 15:04
@songgaoye songgaoye changed the title test(consensus): fix TestByzantinePrevoteEquivocation gossip race fix(blocksync): removeTimedoutPeers deadlock found via Byzantine prevote gossip race May 7, 2026
@swift1337

Copy link
Copy Markdown
Member

@greptile review

@greptile-apps

greptile-apps Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a circular deadlock in blocksync/pool.go where removeTimedoutPeers and onTimeout both held pool.mtx while calling sendError, which blocks on errorsCh; the reactor's poolRoutine (the only consumer of errorsCh) could simultaneously be blocked waiting for pool.mtx in AddBlock, completing the deadlock cycle. A secondary fix eliminates a gossip-timing race in the Byzantine equivocation test.

  • Deadlock fix (blocksync/pool.go): timed-out peer IDs are now collected under the lock and sendError is called after the mutex is explicitly released, both in removeTimedoutPeers and onTimeout.
  • Test race fix (consensus/byzantine_test.go): both conflicting prevotes are now sent to every peer directly, removing dependence on gossip propagation racing against height advancement.
  • Changelog: entry added in CHANGELOG.md under BUG FIXES despite the checklist item being unchecked.

Confidence Score: 4/5

Safe to merge; the deadlock fix is correct and the test change removes a known race, with one style concern about panic-safety of explicit mutex unlocks.

Both code paths in removeTimedoutPeers and onTimeout now release the mutex before the blocking sendError call, correctly breaking the circular wait. The only open concern is that the explicit pool.mtx.Unlock() call (replacing defer) leaves the lock permanently held if a panic fires in the critical section; a goroutine closure with a deferred unlock would be safer. The Byzantine test change is straightforward and makes the test deterministic.

blocksync/pool.go — the explicit mutex unlock pattern in removeTimedoutPeers and onTimeout warrants a second look for panic-safety.

Important Files Changed

Filename Overview
blocksync/pool.go Fixes two deadlock paths in removeTimedoutPeers and onTimeout by releasing pool.mtx before calling sendError; the approach is correct but introduces a subtle window where onTimeout may call sendError for an already-removed peer.
consensus/byzantine_test.go Sends both conflicting prevotes to all peers instead of splitting them, eliminating gossip-timing race in the Byzantine equivocation test.
CHANGELOG.md Adds a BUG FIXES entry for the blocksync removeTimedoutPeers deadlock fix; entry is present but the PR checklist marks it unchecked.

Sequence Diagram

sequenceDiagram
    participant RT as removeTimedoutPeers / onTimeout goroutine
    participant MTX as pool.mtx
    participant CH as errorsCh (buffered 1000)
    participant RL as Reactor poolRoutine

    Note over RT,RL: Old (deadlock path)
    RT->>MTX: Lock()
    RL->>MTX: Lock() (in AddBlock) — BLOCKS
    RT->>CH: sendError() — BLOCKS if reactor not reading
    Note over RT,RL: Circular deadlock

    Note over RT,RL: New (fixed)
    RT->>MTX: Lock()
    RT->>MTX: Unlock() (explicit, after sortPeers)
    RT->>CH: sendError() — reactor free to read
    RL->>MTX: Lock() (in AddBlock) — succeeds
Loading

Reviews (1): Last reviewed commit: "add changelog" | Re-trigger Greptile

Comment thread blocksync/pool.go Resolved

@swift1337 swift1337 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@swift1337
swift1337 added this pull request to the merge queue May 11, 2026
Merged via the queue into cometbft:main with commit 55ea853 May 11, 2026
34 checks passed
@songgaoye
songgaoye deleted the song/fix_TestByzantinePrevoteEquivocation branch May 12, 2026 00:27
kevindharmawan pushed a commit to SeeMoReLab/cometbft that referenced this pull request Jul 30, 2026
…ote gossip race (cometbft#5839)

---
Fix the CI failure at:

https://github.com/cometbft/cometbft/actions/runs/25498058929/job/74823025038

That run revealed two issues:

1. removeTimedoutPeers and onTimeout held pool.mtx while calling
   sendError, which blocks on errorsCh. The reactor loop (the only
   consumer) may itself wait for pool.mtx via AddBlock, forming a
   circular deadlock.

2. The split-delivery of conflicting prevotes required gossip to
   propagate before height 2 committed, which raced under -race.
   Fix: send both conflicting prevotes to every peer directly so each
   validator detects the equivocation without relying on gossip timing.

Follows up on cometbft#5814
#### PR checklist

- [x] Tests written/updated
- [ ] Changelog entry added in `CHANGELOG.md`
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
Unique-Divine added a commit to vetclippy/nibiru that referenced this pull request Nov 5, 2026
The byzantine node split its two conflicting prevotes between peers, and
the honest validators (3 of 4 equal-power votes) could commit height 2
before gossip carried the other vote to any of them. Late votes are dropped
as a height mismatch, so no evidence was ever produced and the test timed
out after 20s.

Send both votes to every peer and give the byzantine validator 2/5 of the
power, so the honest nodes cannot finish height 2 without its prevote and
each one sees the conflict. Also stop the block subscribers on Cancelled.
Ports upstream cometbft/cometbft#5839, #6041, and #5814.
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