bus/natsbus: avoid closing the direct NATS callback channel - #141
Open
herefindalex wants to merge 2 commits into
Open
herefindalex wants to merge 2 commits into
herefindalex wants to merge 2 commits into
Conversation
Unsubscribe removes interest but does not join an asynchronous callback that NATS has already selected. Closing the direct subscription's msgChan can therefore race with a callback send and panic. Keep the callback channel open and let the raw reader observe cancellation, as the writer already does. Typed Close can still wait for its forwarder to exit. Routed children retain their existing mutex-protected shutdown. Add a deterministic regression that pauses a real NATS callback across Close and checks that its destination remains open before resuming it. Wait for NATS callback completion, cover canceled readers and blocked writers, and exercise typed shutdown and direct/queue subscription churn.
herefindalex
force-pushed
the
fix/nats-subscription-shutdown-race
branch
from
September 21, 2026 03:26
55600a6 to
0d0828e
Compare
Contributor
Author
|
@paulwe I rebased this onto Verification on the rebased commit:
Could you take a look when you have a chance? |
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
NATS
Unsubscriberemoves a subscription but does not wait for an asynchronous callback that has already been selected. Direct PSRPC subscriptions currently close their handoff channel immediately afterward, so a late callback can race with the close and panic withsend on closed channel.Keep the direct callback channel open and make the raw reader observe context cancellation, matching the existing writer behavior.
Routed subscriptions are unchanged because
router.mualready orders child writes, route removal, and channel closure.Root cause
The following sequence is possible:
subscription.Closecancels the context and callsUnsubscribe.Unsubscriberemoves the subscription and returns without joining the selected callback.CloseclosesmsgChan.msgChan, causingpanic: send on closed channel.Cancellation alone does not make closing the channel safe. If the canceled-context receive and a send to a closed channel are both selectable, Go may choose the send and panic.
Fix
Read()andwrite()terminate through context cancellation.The resulting ownership invariant is:
Typed
Subscription.Closestill waits for its forwarding goroutine to exit and closes the typed output channel before returning.Tests
Three focused tests cover the shutdown contracts:
TestNATSSubscriptionReadCancellation:synctestestablishes thatRead()is blocked before cancellation, then checks that it returnsfalse.TestNATSSubscriptionWriteCancellation:synctestestablishes that an unbuffered send is blocked, then checks that cancellation releases it.TestNATSSubscriptionCloseWithInflightCallback: a real NATS callback pauses beforewrite;Close()returns and removes the subscription; the callback's channel is checked to be open before resuming the callback and waiting for the NATS callback loop to exit.The channel-open assertion makes the ownership regression deterministic. Relying only on the resumed callback would allow Go to select cancellation instead of the invalid send to a closed channel. A single buffered callback channel exercises this boundary; buffer-state matrices and subscription churn add no distinct shutdown invariant.
Verification
go test -race ./pkg/bus/natsbus ./pkg/bus -count=1 -timeout=120s: 58 passed.mainimplementation deterministically fails the Read cancellation and channel ownership assertions; the existing write cancellation behavior passes.Behavior notes
This is cancellation-based shutdown, not a drain or callback join. A callback selected before
Unsubscribemay still enqueue a bounded buffered message if the send is ready. Owners must also continue callingClose; canceling the parent context alone does not unregister the NATS subscription.