Skip to content

fix: Wait on channel before broadcast - #5800

Merged
Eric-Warehime merged 3 commits into
mainfrom
eric/wait-sync-app-mempool
Apr 20, 2026
Merged

Eric-Warehime merged 3 commits into
mainfrom
eric/wait-sync-app-mempool

Conversation

@Eric-Warehime

Copy link
Copy Markdown
Collaborator

Closes #5790 (comment)

PR checklist

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

@Eric-Warehime
Eric-Warehime requested a review from a team as a code owner April 20, 2026 14:53
@greptile-apps

greptile-apps Bot commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a goroutine leak and premature broadcast bug in AppReactor: when waitForSync=true, the broadcast goroutine now blocks on waitForSwitchingOnCh before calling broadcastTransactionsBatch, and OnStop() always calls cancelCtx() (removing the old early-return guard) so a stopped-but-never-enabled reactor can correctly unblock the waiting goroutine. The new test section (ACT #0 / ASSERT #0) deterministically verifies the gating behaviour end-to-end.

Confidence Score: 5/5

Safe to merge — the fix is correct and the only remaining finding is a minor documentation suggestion.

The logic change is small and well-reasoned: the goroutine wait is properly guarded against nil-channel panics by the existing atomic flag invariant, and the OnStop simplification correctly ensures cancelCtx is always reachable. The new test directly exercises the fixed code path. All remaining comments are P2 style suggestions.

No files require special attention.

Important Files Changed

Filename Overview
mempool/app_reactor.go Adds a pre-broadcast wait on waitForSwitchingOnCh inside the goroutine; removes the buggy early return in OnStop() that prevented cancelCtx() from unblocking a waiting goroutine when the reactor was stopped before being enabled.
mempool/app_reactor_test.go Adds ACT #0 / ASSERT #0 to verify the new gating behavior — inserts a tx into an unenabled node, asserts it doesn't propagate for 500ms, enables the node, then confirms propagation — covering the fix directly.
CHANGELOG.md Adds a BUG FIXES changelog entry for the mempool broadcast gating fix under the correct (unreleased) section.

Sequence Diagram

sequenceDiagram
    participant OS as OnStart goroutine
    participant EI as EnableInOutTxs
    participant BC as broadcastTransactionsBatch
    participant OT as OnStop

    OS->>OS: check switchedOn.Load()
    alt not yet switched on
        OS->>OS: select { waitForSwitchingOnCh / ctx.Done() }
        alt EnableInOutTxs called
            EI->>EI: CompareAndSwap(false→true)
            EI->>OS: close(waitForSwitchingOnCh)
            OS->>BC: start broadcasting
        else OnStop called first
            OT->>OS: cancelCtx() → ctx.Done()
            OS->>OS: return (goroutine exits cleanly)
        end
    else already switched on
        OS->>BC: start broadcasting immediately
    end
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: mempool/app_reactor.go
Line: 81-87

Comment:
**Race-free but worth documenting: nil channel safety**

When `waitForSync=false`, `waitForSwitchingOnCh` is `nil`. The new select block is guarded by `!r.switchedOn.Load()`, and since the constructor sets `switchedOn=true` in that path, the select is correctly skipped. `EnableInOutTxs` is also protected via `CompareAndSwap(false, true)` returning `false` early, preventing a `close(nil)` panic.

This is safe as written, but a brief comment on the invariant "if `waitForSwitchingOnCh == nil` then `switchedOn` is already `true`" would help future readers avoid accidentally breaking this contract.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (2): Last reviewed commit: "Always call cancelCtx" | Re-trigger Greptile

@Eric-Warehime
Eric-Warehime enabled auto-merge April 20, 2026 15:04
@Eric-Warehime
Eric-Warehime added this pull request to the merge queue Apr 20, 2026
Merged via the queue into main with commit 9b7c838 Apr 20, 2026
34 checks passed
@Eric-Warehime
Eric-Warehime deleted the eric/wait-sync-app-mempool branch April 20, 2026 15:22
mattac21 pushed a commit that referenced this pull request May 4, 2026
---

Closes
#5790 (comment)

#### PR checklist

- [X] Tests written/updated
- [X] Changelog entry added in `CHANGELOG.md`
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
kevindharmawan pushed a commit to SeeMoReLab/cometbft that referenced this pull request Jul 30, 2026
---

Closes
cometbft#5790 (comment)

#### PR checklist

- [X] Tests written/updated
- [X] Changelog entry added in `CHANGELOG.md`
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
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.

3 participants