Skip to content

Commit 6ecc0b9

Browse files
Copilotadamsitnik
andauthored
Flip WaitHandle order to prioritize stderr, add Raymond Chen blog links
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/7af21a01-1482-4765-91af-d73ea6f088cc Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
1 parent e8cbe82 commit 6ecc0b9

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Multiplexing.Windows.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ private static unsafe void ReadPipes(
3838
outputOverlapped = AllocateOverlapped(outputEvent);
3939
errorOverlapped = AllocateOverlapped(errorEvent);
4040

41-
WaitHandle[] waitHandles = [outputEvent, errorEvent];
41+
// Error output gets index 0 so WaitAny services it first when both are signaled.
42+
WaitHandle[] waitHandles = [errorEvent, outputEvent];
4243

4344
// Issue initial reads.
4445
bool outputDone = !QueueRead(outputHandle, outputPin.GetAddressOfArrayData(), outputBuffer.Length, outputOverlapped, outputEvent);
@@ -63,7 +64,7 @@ private static unsafe void ReadPipes(
6364
throw new TimeoutException();
6465
}
6566

66-
bool isError = waitResult == 1;
67+
bool isError = waitResult == 0;
6768
NativeOverlapped* currentOverlapped = isError ? errorOverlapped : outputOverlapped;
6869
SafeFileHandle currentHandle = isError ? errorHandle : outputHandle;
6970
ref int totalBytesRead = ref (isError ? ref errorBytesRead : ref outputBytesRead);
@@ -180,6 +181,13 @@ private static unsafe int GetOverlappedResultForPipe(SafeFileHandle handle, Nati
180181
return bytesRead;
181182
}
182183

184+
/// <summary>
185+
/// Cancels a pending overlapped I/O and waits for completion before returning.
186+
/// See Raymond Chen's series on safe cancellation:
187+
/// https://devblogs.microsoft.com/oldnewthing/20110202-00/?p=11613
188+
/// https://devblogs.microsoft.com/oldnewthing/20110203-00/?p=11603
189+
/// https://devblogs.microsoft.com/oldnewthing/20110204-00/?p=11583
190+
/// </summary>
183191
private static unsafe void CancelPendingIOIfNeeded(SafeFileHandle handle, bool done, NativeOverlapped* overlapped)
184192
{
185193
if (done)

0 commit comments

Comments
 (0)