Repository navigation
fix: Wait on channel before broadcast - #5800
Conversation
Greptile SummaryThis PR fixes a goroutine leak and premature broadcast bug in Confidence Score: 5/5Safe 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.
|
| 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
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
--- 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
--- 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
Closes #5790 (comment)
PR checklist
CHANGELOG.mddocs/orspec/) and code comments