From ca731545a58307870a0baebb0ee43eeea61f175f Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 9 Mar 2022 06:49:16 -0500 Subject: [PATCH] Fix cancellation race condition in PipeStream cancellation callback (#65909) * Fix cancellation race condition in PipeStream cancellation callback The public PipeStream.SafePipeHandle property throws an exception if the handle has already been closed. This code should have been using the internal InternalHandle property, which just gets the SafePipeHandle object if it exists. * Update src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs Co-authored-by: Adam Sitnik Co-authored-by: Adam Sitnik --- .../System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs index 9d1430a..2b247e0 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.ValueTaskSource.cs @@ -75,11 +75,11 @@ namespace System.IO.Pipes _cancellationRegistration = cancellationToken.UnsafeRegister(static (s, token) => { PipeValueTaskSource vts = (PipeValueTaskSource)s!; - if (!vts._pipeStream.SafePipeHandle.IsInvalid) + if (vts._pipeStream.InternalHandle is { IsClosed: false } handle) { try { - Interop.Kernel32.CancelIoEx(vts._pipeStream.SafePipeHandle, vts._overlapped); + Interop.Kernel32.CancelIoEx(handle, vts._overlapped); // Ignore all failures: no matter whether it succeeds or fails, completion is handled via the IOCallback. } catch (ObjectDisposedException) { } // in case the SafeHandle is (erroneously) closed concurrently -- 2.7.4