fix: prevent panic on send to closed channel in WebSocket.Disconnect()#43
Open
kramer65 wants to merge 1 commit into
Open
fix: prevent panic on send to closed channel in WebSocket.Disconnect()#43kramer65 wants to merge 1 commit into
kramer65 wants to merge 1 commit into
Conversation
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.
Problem
Disconnect()contains a race condition that causes a panic(
send on closed channel) in production under reconnect load.The sequence that triggers it:
Disconnect()creates an unbuffereddonechannel and registers aRecurringcallback onOnDisconnectedthat sendstruetodone.defer close(done)is registered, sodoneis closed whenDisconnect()returns.OnDisconnectedevent can fire from theread()goroutineconcurrently with (or after) the
defer close(done)runs.done <- trueon an already-closed channel:panic: send on closed channel.
This panic is not recoverable and crashes the entire process, taking
down any other goroutines or connections that were running concurrently.
Root cause
Two mistakes compound to cause the panic:
closes it. Any timing that lets the sender run after the closer
is fatal.
defer close(done). Closing a channel you are still potentiallywriting to violates Go's rule that only the sender should close a
channel.
Fix
Three small changes together make
Disconnect()race-free:Buffered channel (capacity 1): the callback can always complete
its send without blocking, regardless of when it fires relative to
the function returning.
Remove
defer close(done): the channel only needs to be closedif something is ranging over it; here we only receive one value.
Removing the close eliminates the hazardous write-after-close window.
Non-blocking send in the callback (
select/default): defensiveguard in case the
OnDisconnectedevent fires more than once (e.g.on a rapid reconnect cycle); the first send wins, subsequent calls
are silently dropped instead of blocking.
How to reproduce (before fix)
Run the connector under any workload that triggers repeated reconnects
(network blip, exchange-side disconnect, explicit
Close()during anactive connection). With high enough reconnect frequency the goroutine
scheduler will expose the window and the panic will occur within minutes.