diff --git a/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs b/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs index 1ba3516c42a9f1..12a9fe280a6d78 100644 --- a/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.OverlappedValueTaskSource.Windows.cs @@ -195,27 +195,25 @@ private static void IOCallback(uint errorCode, uint numBytes, NativeOverlapped* internal void Complete(uint errorCode, uint numBytes) { - Debug.Assert(errorCode == Interop.Errors.ERROR_SUCCESS || numBytes == 0, $"Callback returned {errorCode} error and {numBytes} bytes"); - OSFileStreamStrategy? strategy = _strategy; ReleaseResources(); - if (strategy is not null && _bufferSize != numBytes) // true only for incomplete operations - { - strategy.OnIncompleteOperation(_bufferSize, (int)numBytes); - } - switch (errorCode) { case Interop.Errors.ERROR_SUCCESS: case Interop.Errors.ERROR_BROKEN_PIPE: case Interop.Errors.ERROR_NO_DATA: case Interop.Errors.ERROR_HANDLE_EOF: // logically success with 0 bytes read (read at end of file) + if (_bufferSize != numBytes) // true only for incomplete operations + { + strategy?.OnIncompleteOperation(_bufferSize, (int)numBytes); + } // Success _source.SetResult((int)numBytes); break; case Interop.Errors.ERROR_OPERATION_ABORTED: + strategy?.OnIncompleteOperation(_bufferSize, 0); // don't use numBytes here, as it can be != 0 for this errorCode (#57212) // Cancellation CancellationToken ct = _cancellationRegistration.Token; _source.SetException(ct.IsCancellationRequested ? new OperationCanceledException(ct) : new OperationCanceledException()); @@ -223,6 +221,7 @@ internal void Complete(uint errorCode, uint numBytes) default: // Failure + strategy?.OnIncompleteOperation(_bufferSize, 0); _source.SetException(Win32Marshal.GetExceptionForWin32Error((int)errorCode)); break; }