Downgrade expected WebSocket disconnect exceptions from Error to Debug#8
Merged
Merged
Conversation
When a WebSocket connection closes normally (server restart, network jitter, client close), exceptions such as ObjectDisposedException, SocketException (ConnectionReset/ConnectionAborted/OperationAborted) and WebSocketException (ConnectionClosedPrematurely / Aborted state) are expected and should not flood the log at ERROR level. Changes: - WsChannel.cs: Add ObjectDisposedException silent break, add IsNormalDisconnect helper, downgrade normal SocketException codes (ConnectionReset 10054, ConnectionAborted 10053, OperationAborted 995) to Debug log in DoPull receive loop. - WsChannelCore.cs: Same ObjectDisposedException and SocketException handling; additionally catch WebSocketException with ConnectionClosedPrematurely or Aborted socket state and downgrade to Debug, simplifying the redundant state check in the generic handler. Fixes NewLifeX/NewLife.Remoting#<issue>
Copilot
AI
changed the title
[WIP] Fix excessive error logs on WebSocket disconnection
Downgrade expected WebSocket disconnect exceptions from Error to Debug
Jul 20, 2026
nnhy
marked this pull request as ready for review
July 20, 2026 15:02
There was a problem hiding this comment.
Pull request overview
This PR reduces log noise in the WebSocket client receive loops by treating expected disconnect-related exceptions (e.g., disposal, connection resets, premature close) as normal termination conditions, logging them at Debug (or silently breaking) instead of Error.
Changes:
- Added explicit handling for
ObjectDisposedExceptionand “normal”SocketExceptiondisconnect codes inWsChannel.DoPull. - Added explicit handling for
ObjectDisposedException, “normal”SocketException, and selectedWebSocketExceptiondisconnect scenarios inWsChannelCore.DoPull. - Introduced a shared
IsNormalDisconnect(SocketError)helper to standardize what’s considered a normal disconnect.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| NewLife.Remoting/Clients/WsChannel.cs | Adds normal-disconnect detection (IsNormalDisconnect) and downgrades common disconnect exceptions to non-error handling in the receive loop. |
| NewLife.Remoting/Clients/WsChannelCore.cs | Adds normal-disconnect handling for WebSocketException/SocketException in the NETCOREAPP receive loop to avoid flooding Error logs on expected disconnects. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+146
to
+148
| catch (WebSocketException wex) when ( | ||
| wex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely || | ||
| socket.State == WebSocketState.Aborted) |
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.
Normal WebSocket disconnections (server restart, network jitter, client close) were generating
ObjectDisposedException,SocketException, andWebSocketExceptionat ERROR level in theDoPullreceive loops, flooding logs and masking real errors.Changes
WsChannel.cs(non-NETCOREAPP client)protected static IsNormalDisconnect(SocketError)helper — matchesConnectionReset(10054),ConnectionAborted(10053),OperationAborted(995)Exceptionhandler:ObjectDisposedException→ silent breakSocketExceptionmatching normal codes → Debug log + breakWsChannelCore.cs(NETCOREAPP client,System.Net.WebSockets)ObjectDisposedExceptionandSocketExceptionhandling (reusesIsNormalDisconnectfrom base class)WebSocketExceptionwhenWebSocketErrorCode == ConnectionClosedPrematurelyorsocket.State == Aborted→ Debug log + break, replacing the previous ad-hoc inline suppression