Skip to content

natsbus: do not close the channel a NATS callback writes to - #153

Merged
boks1971 merged 1 commit into
mainfrom
raja/natsbus-close-panic
Oct 6, 2026
Merged

boks1971 merged 1 commit into
mainfrom
raja/natsbus-close-panic

Conversation

@boks1971

@boks1971 boks1971 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Closing a plain NATS subscription no longer panics.

Close unsubscribed and then closed msgChan (natsbus.go#L163-L168). Unsubscribe does not wait for a callback that is already running. That callback's write then sends on the closed channel and crashes the process. This can happen when an RPC client closes while responses are still arriving, for example during node shutdown.

Now Close does not close msgChan, and Read returns when the subscription's context is cancelled, as the Redis bus does.

A few messages can still be delivered after Close. A late write can add one to the buffer, and Read may return buffered messages before it reports the close. This also happened before the change: Read returned the buffered messages first, and the typed subscription can pass them on to its channel after its own Close.

TestSubscriptionWriteAfterClose writes after Close. On main it panics.

🤖 Generated with Claude Code

Unsubscribe does not wait for a callback that is already running, so
its write could send on the closed msgChan and crash the process.
Read now returns on context cancel, and Close leaves msgChan open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@boks1971
boks1971 merged commit 29b4c27 into main Oct 6, 2026
8 checks passed
@boks1971
boks1971 deleted the raja/natsbus-close-panic branch October 6, 2026 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants