Skip to content
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

[S.N.Quic] Observe exceptions in ResettableValueTaskSource #114226

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

antonfirsov
Copy link
Member

@antonfirsov antonfirsov commented Apr 3, 2025

There are use cases of QuicStream.WritesClosed / QuicStream.ReadsClosed in .NET itself which only query the IsCompletedSuccessfully property:

The current implementation would require those call sites to await the task or do the _ = task.Exception trick to avoid leaking unobserved exceptions. IMO this behavior is hard to communicate and the fact that we "misused" our own API is telling. It seems more reasonable if the implementation ensured that exceptions from those tasks are never forwarded to TaskScheduler.UnobservedTaskException.

This PR implements this behavior in ResettableValueTaskSource. Beyond that, it includes a refactor of the TrySetException() method removing its final parameter since it was never called with final: false, which felt confusing when working with the code.

Fixes #114128.

Running #114225 against this PR locally reduced the number of unobserved exceptions to zero for me.

@Copilot Copilot bot review requested due to automatic review settings April 3, 2025 17:47
Copy link
Contributor

@Copilot Copilot AI left a 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 addresses a design concern with exception handling in QUIC streams by ensuring that exceptions are observed and do not leak, while simplifying the API of the ResettableValueTaskSource.

  • Removed the redundant "final" parameter from TrySetException calls in QuicStream.
  • Updated ResettableValueTaskSource to always treat exception completion as final.
  • Added explicit observation of exceptions in the final task to prevent unobserved exceptions.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs Removed the now-unnecessary "final" parameter in TrySetException calls, simplifying exception propagation.
src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/ResettableValueTaskSource.cs Refactored TrySetException to always complete in a final state and added explicit observation of exception tasks.

Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@ManickaP ManickaP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

/// <param name="exception">The exception to set as a result of the value task.</param>
/// <returns><c>true</c> if this is the first call that set the result; otherwise, <c>false</c>.</returns>
public bool TrySetException(Exception exception, bool final = false)
public bool TrySetException(Exception exception)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the explanation: the bool final = false was here to have the "same" signature as TrySetResult and in the past we did have non-final exceptions (from cancellation).

It could have also gone the other way by making final mandatory to all calls to TrySetResult and TrySetException.

Anyway, this is not a hill I want to die on, so keep the change as-is if you prefer it this way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't plan to do further refactors in this PR, just leaving my take: It would be better to make it mandatory on TrySetResult() so it's easier to see what the call sites are doing. We can add it back to TrySetException when/if it turns out to be needed again, till then I don't see the value.

@antonfirsov
Copy link
Member Author

/azp run runtime-libraries stress-http

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

antonfirsov and others added 2 commits April 8, 2025 03:24
…ettableValueTaskSource.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>

This comment was marked as outdated.

@antonfirsov
Copy link
Member Author

/azp run runtime-libraries-coreclr outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TaskScheduler.UnobservedTaskException catches quic exceptions with kestrel combined with grpc + http3
2 participants