fix(tm2/rpc): don't panic when txDispatcher subscription closes on shutdown#5561
Open
thehowl wants to merge 3 commits intognolang:masterfrom
Open
fix(tm2/rpc): don't panic when txDispatcher subscription closes on shutdown#5561thehowl wants to merge 3 commits intognolang:masterfrom
thehowl wants to merge 3 commits intognolang:masterfrom
Conversation
…utdown The txDispatcher's listenRoutine used to panic with "txDispatcher subscription unexpectedly closed" whenever its events subscription channel was closed. That channel is closed legitimately by events.SubscribeFilteredOn during event-switch shutdown: when the listener callback's unbuffered send would block and evsw.Quit() has already fired, the callback takes the <-evsw.Quit() branch and close(ch)es the subscription. This hit integration test CI as a flake (panic: txDispatcher subscription unexpectedly closed) because Node.OnStop stops the event switch without first stopping the package-level gTxDispatcher, and eventSwitch.FireEvent has no stop check — so a late EventTx fired during teardown can still invoke the listener callback and race evsw.Quit() to close the channel. Treat the closed subscription as a clean shutdown signal: stop the dispatcher and return. Additionally stop gTxDispatcher in Node.OnStop before evsw.Stop so the goroutine exits via its own Quit channel in the normal case, and guard SetEventSwitch against leaking a previous dispatcher when the package globals are re-initialized. Regression tests exercise both the directly-closed-subscription path and the full event-switch shutdown race.
Collaborator
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🟢 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
7 tasks
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.
Summary
Fixes a CI flake manifesting as
panic: txDispatcher subscription unexpectedly closed(e.g. this run).The
txDispatcher.listenRoutinepanicked any time itseventssubscription channel was closed. But that channel is closed legitimately byevents.SubscribeFilteredOnduring event-switch shutdown: when the listener callback's unbuffered send would block andevsw.Quit()has already fired, the callback takes the<-evsw.Quit()branch andclose(ch)es the subscription (seetm2/pkg/events/subscribe.go:42-73).The race surfaces during node teardown because:
Node.OnStopstops the event switch (n.evsw.Stop()) but never stops the package-levelgTxDispatcher.eventSwitch.FireEventhas no stop check — a reactor that hasn't finished draining can still fire anEventTxafterStop, invoking the dispatcher's callback whileevsw.Quit()is closed.Fix
tm2/pkg/bft/rpc/core/mempool.go): return fromlistenRoutineand asynchronouslyStop()the dispatcher instead of panicking. PendinggetTxResultwaiters time out normally.gTxDispatcherduring node teardown (tm2/pkg/bft/node/node.go,tm2/pkg/bft/rpc/core/pipe.go): addrpccore.Stop()and call it fromNode.OnStopbeforeevsw.Stop, so in the normal case the listenRoutine exits via its ownQuitchannel. Also stop any previous dispatcher whenSetEventSwitchis called again (prevents goroutine leaks across tests reusing the package globals).tm2/pkg/bft/rpc/core/mempool_test.go): one test closes the subscription directly; the other reproduces the full event-switch shutdown race. The second test reliably produces the original panic when reverted against the old code.Test plan
go test ./tm2/pkg/bft/rpc/core/... -short -count=30— new regression tests pass 30/30go test ./tm2/pkg/bft/rpc/... ./tm2/pkg/bft/node/...— no regressionsgo test ./gno.land/pkg/integration/... -run TestMalformedTypeURL— passeslistenRoutinechange makes the newTestTxDispatcher_EventSwitchShutdownfail with the original panic, confirming the test exercises the exact CI failure