-
Notifications
You must be signed in to change notification settings - Fork 5k
WebSocket: observe exceptions in WaitForServerToCloseConnectionAsync #114689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebSocket: observe exceptions in WaitForServerToCloseConnectionAsync #114689
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors how exceptions are logged and observed in WebSocket connection close handling, aligning with HttpConnectionBase.LogExceptions for consistency.
- Refactored WaitForServerToCloseConnectionAsync to use LogExceptions instead of Observe.
- Removed duplicated Observe methods in favor of the new LogExceptions implementations.
- Added a regression test to ensure that exceptions during the close handshake are properly observed.
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs | Updated WaitForServerToCloseConnectionAsync to log exceptions and refactored logging helper methods. |
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.KeepAlive.cs | Replaced Observe calls with LogExceptions and removed redundant logging helpers. |
src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs | Added a regression test for unobserved exceptions during the close handshake. |
Files not reviewed (1)
- src/libraries/System.Net.WebSockets.Client/tests/System.Net.WebSockets.Client.Tests.csproj: Language not supported
Comments suppressed due to low confidence (1)
src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs:539
- The test registers an event handler for TaskScheduler.UnobservedTaskException but does not unsubscribe it, which could potentially affect other tests. Consider unregistering the event handler after the test completes to ensure isolation.
TaskScheduler.UnobservedTaskException += (obj, args) =>
/azp run runtime-libraries-coreclr outerloop |
Azure Pipelines successfully started running 1 pipeline(s). |
While this is good as a workaround, I cannot not mention that this is a problem of |
I think we don't want a solution to just swallow the exception with @stephentoub any thoughts? |
That already exists in the form of ContinueWith(..., TaskContinuationOptions.OnlyOnFaulted), which this PR is already using. |
There seems to be no warning in the API docs for However, I see a very nice note for (Though this note is a bit hard to notice)Because it's placed in the very bottom of remarks that in their turn are only after the example 😅
|
Sure: |
Since it's very easy to forget (like we've done) to observe exceptions from the underlying task, would you consider having |
That assumes a very specific use case, one where you don't care at all about unobserved exceptions from these tasks. As Anton highlighted, we probably want here not to ignore the exception but to log it in some way. And in general the only time unobserved exceptions are visible is when you subscribe to the unobserved exceptions event, in which case you may very well want to know about exceptions that occur after you stop waiting for it. |
Callers still need a lot of ceremonial code around that API. Can be handled by a documentation example instead of a new API I guess. |
GC.Collect(2); | ||
GC.WaitForPendingFinalizers(); | ||
clientCompleted.SetResult(); | ||
Assert.Null(unobserved); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was that enough to consistently get an unobserved task exception before the PR change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's failing without the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
CI failures are unrelated. |
Fixes #80116
Includes a refactor renaming and moving the methods responsible for logging and observing the exceptions. The renaming is for consistency with
HttpConnectionBase.LogExceptions
inSystem.Net.Http
.