diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs index c11f3029a2136..d886ce4288f2a 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs @@ -70,9 +70,18 @@ public sealed partial class QuicStream { CancellationAction = target => { - if (target is QuicStream stream) + try { - stream.Abort(QuicAbortDirection.Read, stream._defaultErrorCode); + if (target is QuicStream stream) + { + stream.Abort(QuicAbortDirection.Read, stream._defaultErrorCode); + } + } + catch (ObjectDisposedException) + { + // We collided with a Dispose in another thread. This can happen + // when using CancellationTokenSource.CancelAfter. + // Ignore the exception } } }; @@ -83,9 +92,18 @@ public sealed partial class QuicStream { CancellationAction = target => { - if (target is QuicStream stream) + try + { + if (target is QuicStream stream) + { + stream.Abort(QuicAbortDirection.Write, stream._defaultErrorCode); + } + } + catch (ObjectDisposedException) { - stream.Abort(QuicAbortDirection.Write, stream._defaultErrorCode); + // We collided with a Dispose in another thread. This can happen + // when using CancellationTokenSource.CancelAfter. + // Ignore the exception } } }; @@ -475,8 +493,8 @@ private unsafe int HandleEventStartComplete(ref START_COMPLETE data) private unsafe int HandleEventReceive(ref RECEIVE data) { ulong totalCopied = (ulong)_receiveBuffers.CopyFrom( - new ReadOnlySpan(data.Buffers, (int) data.BufferCount), - (int) data.TotalBufferLength, + new ReadOnlySpan(data.Buffers, (int)data.BufferCount), + (int)data.TotalBufferLength, data.Flags.HasFlag(QUIC_RECEIVE_FLAGS.FIN)); if (totalCopied < data.TotalBufferLength) {