Skip to content

Commit b248f40

Browse files
committed
Avoid crashing .NET Core processes while canceling async I/O
Workaround for dotnet/runtime#39902
1 parent c0b15bb commit b248f40

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/Nerdbank.Streams/PipeExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,21 @@ private static PipeReader UsePipeReader(this Stream stream, int sizeHint = 0, Pi
469469
// we can return a decorated PipeReader that calls us from its Complete method directly.
470470
var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
471471
#pragma warning disable CS0618 // Type or member is obsolete
472-
pipe.Writer.OnReaderCompleted((ex, state) => ((CancellationTokenSource)state).Cancel(), combinedTokenSource);
472+
pipe.Writer.OnReaderCompleted(
473+
(ex, state) =>
474+
{
475+
try
476+
{
477+
((CancellationTokenSource)state).Cancel();
478+
}
479+
catch (AggregateException cancelException)
480+
{
481+
// .NET Core may throw this when canceling async I/O (https://github.com/dotnet/runtime/issues/39902).
482+
// Just swallow it. We've canceled what we intended to.
483+
cancelException.Handle(x => x is ObjectDisposedException);
484+
}
485+
},
486+
combinedTokenSource);
473487

474488
// When this argument is provided, it provides a means to ensure we don't hang while reading from an I/O pipe
475489
// that doesn't respect the CancellationToken. Disposing a Stream while reading is a means to terminate the ReadAsync operation.

0 commit comments

Comments
 (0)