Skip to content

Commit 3998832

Browse files
authored
Observe exceptions from _connectionCloseTcs (#112190)
1 parent 7324dd1 commit 3998832

File tree

2 files changed

+15
-26
lines changed

2 files changed

+15
-26
lines changed

src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/ThrowHelper.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,26 +215,4 @@ public static void ValidateNotNull(string argumentName, string resourceName, obj
215215
throw new ArgumentNullException(argumentName, SR.Format(resourceName, propertyName));
216216
}
217217
}
218-
219-
public static void ObserveException(this Task task)
220-
{
221-
if (task.IsCompleted)
222-
{
223-
ObserveExceptionCore(task);
224-
}
225-
else
226-
{
227-
task.ContinueWith(static (t) => ObserveExceptionCore(t), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
228-
}
229-
230-
static void ObserveExceptionCore(Task task)
231-
{
232-
Debug.Assert(task.IsCompleted);
233-
if (task.IsFaulted)
234-
{
235-
// Access Exception to avoid TaskScheduler.UnobservedTaskException firing.
236-
Exception? e = task.Exception!.InnerException;
237-
}
238-
}
239-
}
240218
}

src/libraries/System.Net.Quic/src/System/Net/Quic/QuicConnection.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,22 @@ private unsafe int HandleEventShutdownInitiatedByTransport(ref SHUTDOWN_INITIATE
658658
{
659659
Exception exception = ExceptionDispatchInfo.SetCurrentStackTrace(ThrowHelper.GetExceptionForMsQuicStatus(data.Status, (long)data.ErrorCode));
660660
_connectedTcs.TrySetException(exception);
661-
_connectionCloseTcs.TrySetException(exception);
661+
if (_connectionCloseTcs.TrySetException(exception))
662+
{
663+
// Observe the exception as the task is used only for internal workings and might not be observed.
664+
_ = _connectionCloseTcs.Task.Exception;
665+
}
662666
_acceptQueue.Writer.TryComplete(exception);
663667
return QUIC_STATUS_SUCCESS;
664668
}
665669
private unsafe int HandleEventShutdownInitiatedByPeer(ref SHUTDOWN_INITIATED_BY_PEER_DATA data)
666670
{
667671
Exception exception = ExceptionDispatchInfo.SetCurrentStackTrace(ThrowHelper.GetConnectionAbortedException((long)data.ErrorCode));
668-
_connectionCloseTcs.TrySetException(exception);
672+
if (_connectionCloseTcs.TrySetException(exception))
673+
{
674+
// Observe the exception as the task is used only for internal workings and might not be observed.
675+
_ = _connectionCloseTcs.Task.Exception;
676+
}
669677
_acceptQueue.Writer.TryComplete(exception);
670678
return QUIC_STATUS_SUCCESS;
671679
}
@@ -675,7 +683,11 @@ private unsafe int HandleEventShutdownComplete()
675683
_tlsSecret?.WriteSecret();
676684

677685
Exception exception = ExceptionDispatchInfo.SetCurrentStackTrace(_disposed ? new ObjectDisposedException(GetType().FullName) : ThrowHelper.GetOperationAbortedException());
678-
_connectionCloseTcs.TrySetException(exception);
686+
if (_connectionCloseTcs.TrySetException(exception))
687+
{
688+
// Observe the exception as the task is used only for internal workings and might not be observed.
689+
_ = _connectionCloseTcs.Task.Exception;
690+
}
679691
_acceptQueue.Writer.TryComplete(exception);
680692
_connectedTcs.TrySetException(exception);
681693
_shutdownTokenSource.Cancel();
@@ -850,7 +862,6 @@ public async ValueTask DisposeAsync()
850862
Debug.Assert(_connectionCloseTcs.Task.IsCompleted);
851863
_handle.Dispose();
852864
_shutdownTokenSource.Dispose();
853-
_connectionCloseTcs.Task.ObserveException();
854865
_configuration?.Dispose();
855866

856867
// Dispose remote certificate only if it hasn't been accessed via getter, in which case the accessing code becomes the owner of the certificate lifetime.

0 commit comments

Comments
 (0)