Handle WebSocket Closing Error#16
Merged
Merged
Conversation
dank
reviewed
Jun 24, 2026
Handle websocket read errors with connection state check.
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.
Fix false ERROR log on intentional WebSocket close
Problem
When
Close()is called explicitly on aPsyNetRPCinstance, the internalreadMessages()goroutine is blocked onwsConn.ReadMessage(). Closing the underlying TCP socket causesReadMessage()to return ause of closed network connectionerror, which is then logged asERROR:This is misleading — the connection was closed intentionally, not due to a real network failure. A common pattern that triggers this is opening a short-lived RPC connection per request:
Root cause
readMessages()does not distinguish between an expected closure (triggered byClose()) and an unexpected network error. Both are logged atERRORlevel.Fix
Check the
connectedflag — which is already set tofalsebyClose()before the socket is closed — to determine whether the error is expected:This way, intentional closures are logged at
DEBUGlevel, while real unexpected errors remain atERROR.Why not
websocket.IsUnexpectedCloseError?use of closed network connectionis a raw TCP-level error, not a WebSocket close frame.IsUnexpectedCloseErroronly handles proper WebSocket close codes and would not catch this case.