Fix panic in handleSubscribeHTTP when client disconnects during publish#1598
Merged
Conversation
Replace wlock.TryLock() with a proper Lock() + closed flag to prevent writing to a response writer that has been cleaned up after the handler returns. The previous TryLock approach could not guarantee the response writer was still valid when a concurrent Publish goroutine called Flush.
Owner
Author
|
This is strongly related to this issue from 3 years ago: #338 (comment) I am verifying that this fix is accurate and correct. |
Owner
Author
|
I verified by THINKING that this is a good fix, and it actually does likely fix a goroutine leak as well. Cursor wrote a fantastic test I verified that the test fails without the fix. |
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.
(This PR is AI generated, but a human [me] has verified that it is accurate)
Summary
handleSubscribeHTTPcaused by a race between atopic.Publishgoroutine flushing the HTTP response writer and the handler returning after client disconnect.wlock.TryLock()hack with a properwlock.Lock()+closedflag pattern that correctly waits for in-flight writes to finish and prevents future writes to a cleaned-up response writer.Root cause
topic.Publish()copies the subscribers map and calls each subscriber in its own goroutine. When a client disconnects:handleSubscribeHTTPreturns, triggering deferred cleanupPublishgoroutine may still be in-flight, callingFlush()on the now-invalid writerThe old
TryLock()approach had two failure modes:subrunning): futuresub()calls deadlock onwlock.Lock()(goroutine leak, since the lock is never released)subis running): the handler returns immediately without waiting, and Go cleans up the writer whilesubis still flushing — exactly the panicFix
The new approach:
wlock.Lock()to wait for any in-flightsub()call to finish, then setsclosed = truesub()function checks theclosedflag after acquiring the lock — if the connection has been closed, it returns without touching the response writerThis guarantees the response writer is never accessed after the handler returns.
Stack trace this fixes
Test plan
go vet ./server/passesmake check