From 119c770c153a11bc358c7993dd20cdf75de9deda Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 10 Sep 2016 09:25:27 -0400 Subject: [PATCH] Address PR feedback --- .../src/System/IO/Win32FileStream.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs b/src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs index 83ad2640cf39..2fb4dca02255 100644 --- a/src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs +++ b/src/System.IO.FileSystem/src/System/IO/Win32FileStream.cs @@ -1764,22 +1764,25 @@ private async Task AsyncModeCopyToAsync(Stream destination, int bufferSize, Canc // whatever read operation may currently be in progress, if there is one. It's possible the cancellation // request could come in between operations, in which case we flag that with explicit calls to ThrowIfCancellationRequested // in the read/write copy loop. - cancellationReg = cancellationToken.Register(s => + if (cancellationToken.CanBeCanceled) { - var innerAwaitable = (AsyncCopyToAwaitable)s; - unsafe + cancellationReg = cancellationToken.Register(s => { - lock (innerAwaitable.CancellationLock) // synchronize with cleanup of the overlapped + var innerAwaitable = (AsyncCopyToAwaitable)s; + unsafe { - if (innerAwaitable._nativeOverlapped != null) - { - // Try to cancel the I/O. We ignore the return value, as cancellation is opportunistic and we - // don't want to fail the operation because we couldn't cancel it. - Interop.mincore.CancelIoEx(innerAwaitable._fileStream._handle, innerAwaitable._nativeOverlapped); + lock (innerAwaitable.CancellationLock) // synchronize with cleanup of the overlapped + { + if (innerAwaitable._nativeOverlapped != null) + { + // Try to cancel the I/O. We ignore the return value, as cancellation is opportunistic and we + // don't want to fail the operation because we couldn't cancel it. + Interop.mincore.CancelIoEx(innerAwaitable._fileStream._handle, innerAwaitable._nativeOverlapped); + } } } - } - }, readAwaitable); + }, readAwaitable); + } // Repeatedly read from this FileStream and write the results to the destination stream. while (true)