Skip to content

Commit 5d71eeb

Browse files
Swallow ObjectDisposedException when aborting QuicStream from CancellationAction (#75179)
Co-authored-by: Radek Zikmund <[email protected]>
1 parent f8b6128 commit 5d71eeb

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,18 @@ public sealed partial class QuicStream
7070
{
7171
CancellationAction = target =>
7272
{
73-
if (target is QuicStream stream)
73+
try
7474
{
75-
stream.Abort(QuicAbortDirection.Read, stream._defaultErrorCode);
75+
if (target is QuicStream stream)
76+
{
77+
stream.Abort(QuicAbortDirection.Read, stream._defaultErrorCode);
78+
}
79+
}
80+
catch (ObjectDisposedException)
81+
{
82+
// We collided with a Dispose in another thread. This can happen
83+
// when using CancellationTokenSource.CancelAfter.
84+
// Ignore the exception
7685
}
7786
}
7887
};
@@ -83,9 +92,18 @@ public sealed partial class QuicStream
8392
{
8493
CancellationAction = target =>
8594
{
86-
if (target is QuicStream stream)
95+
try
96+
{
97+
if (target is QuicStream stream)
98+
{
99+
stream.Abort(QuicAbortDirection.Write, stream._defaultErrorCode);
100+
}
101+
}
102+
catch (ObjectDisposedException)
87103
{
88-
stream.Abort(QuicAbortDirection.Write, stream._defaultErrorCode);
104+
// We collided with a Dispose in another thread. This can happen
105+
// when using CancellationTokenSource.CancelAfter.
106+
// Ignore the exception
89107
}
90108
}
91109
};
@@ -491,8 +509,8 @@ private unsafe int HandleEventStartComplete(ref START_COMPLETE data)
491509
private unsafe int HandleEventReceive(ref RECEIVE data)
492510
{
493511
ulong totalCopied = (ulong)_receiveBuffers.CopyFrom(
494-
new ReadOnlySpan<QUIC_BUFFER>(data.Buffers, (int) data.BufferCount),
495-
(int) data.TotalBufferLength,
512+
new ReadOnlySpan<QUIC_BUFFER>(data.Buffers, (int)data.BufferCount),
513+
(int)data.TotalBufferLength,
496514
data.Flags.HasFlag(QUIC_RECEIVE_FLAGS.FIN));
497515
if (totalCopied < data.TotalBufferLength)
498516
{

0 commit comments

Comments
 (0)