fix: prevent deadlock in server Write and websocket Close/WriteManual#408
Open
rishabhvaish wants to merge 1 commit intolorenzodonini:masterfrom
Open
fix: prevent deadlock in server Write and websocket Close/WriteManual#408rishabhvaish wants to merge 1 commit intolorenzodonini:masterfrom
rishabhvaish wants to merge 1 commit intolorenzodonini:masterfrom
Conversation
The server's Write method held connMutex.RLock while calling webSocket.Write, which in turn held webSocket.mutex.RLock while sending on the outQueue channel. If the channel was full, the goroutine blocked while holding both locks. Meanwhile, cleanup() needed webSocket.mutex.Lock and handleDisconnect needed connMutex.Lock, creating a deadlock. The fix releases each lock immediately after the map/nil-check lookup, before any potentially-blocking channel send. A deferred recover guards against the race where cleanup closes the channel between the nil check and the send. Fixes lorenzodonini#291
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 #291
server.WriteheldconnMutex.RLockwhile callingwebSocket.WriteManual, which heldwebSocket.mutex.RLockwhile sending onoutQueue. If the channel buffer was full, the goroutine blocked holding both read locks. Concurrently,cleanup()needed both write locks — deadlock.The fix releases each lock immediately after the map lookup / nil check, before any channel send. A deferred
recoverhandles the race wherecleanup()closes the channel between the nil check and the send.Changes:
server.Write: releaseconnMutex.RLockright after map lookupwebSocket.WriteManual: releasemutex.RLockafter nil check, add recoverwebSocket.Close: same pattern as WriteManualTest plan
go test ./ws/... -v -racepasses🤖 Generated with Claude Code