commit 53d8a25dab5ddcea16ac70cdcdf28cb3e4944cbb (tree)
parent adc9a282d8b3cbe58e07c965fe40fb1dd8666bd7
Author: Jonathan Marler <johnnymarler@gmail.com>
Date: Sat, 5 Feb 2022 18:59:09 -0700
child_process: collectOutputWindows handle broken_pipe from ReadFile
This was found on a user's machine when calling "git" as a child process from msys. Instead of getting BROKEN_PIPE on GetOverlappedREsult, it would occur on ReadFile which would then cause the function to hang because the async operation was never started.
Diffstat:
1 file changed, 22 insertions(+), 6 deletions(-)
diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig
@@ -277,10 +277,19 @@ pub const ChildProcess = struct {
const new_capacity = std.math.min(outs[i].items.len + bump_amt, max_output_bytes);
try outs[i].ensureTotalCapacity(new_capacity);
const buf = outs[i].unusedCapacitySlice();
- _ = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]);
- wait_objects[wait_object_count] = handles[i];
- wait_object_count += 1;
+ const read_result = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]);
+ std.debug.assert(read_result == 0);
+ switch (windows.kernel32.GetLastError()) {
+ .IO_PENDING => {
+ wait_objects[wait_object_count] = handles[i];
+ wait_object_count += 1;
+ },
+ .BROKEN_PIPE => {}, // don't add to the wait_objects list
+ else => |err| return windows.unexpectedError(err),
+ }
}
+ if (wait_object_count == 0)
+ return;
while (true) {
const status = windows.kernel32.WaitForMultipleObjects(wait_object_count, &wait_objects, 0, windows.INFINITE);
@@ -320,9 +329,16 @@ pub const ChildProcess = struct {
try outs[i].ensureTotalCapacity(new_capacity);
const buf = outs[i].unusedCapacitySlice();
if (buf.len == 0) return if (i == 0) error.StdoutStreamTooLong else error.StderrStreamTooLong;
- _ = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]);
- wait_objects[wait_object_count] = handles[i];
- wait_object_count += 1;
+ const read_result = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]);
+ std.debug.assert(read_result == 0);
+ switch (windows.kernel32.GetLastError()) {
+ .IO_PENDING => {
+ wait_objects[wait_object_count] = handles[i];
+ wait_object_count += 1;
+ },
+ .BROKEN_PIPE => {}, // don't add to the wait_objects list
+ else => |err| return windows.unexpectedError(err),
+ }
}
}