| ... | ... | @@ -252,6 +252,35 @@ pub const ChildProcess = struct { |
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | const WindowsAsyncReadResult = enum { |
| 256 | pending, |
| 257 | closed, |
| 258 | full, |
| 259 | }; |
| 260 | |
| 261 | fn windowsAsyncRead( |
| 262 | handle: windows.HANDLE, |
| 263 | overlapped: *windows.OVERLAPPED, |
| 264 | buf: *std.ArrayList(u8), |
| 265 | bump_amt: usize, |
| 266 | max_output_bytes: usize, |
| 267 | ) !WindowsAsyncReadResult { |
| 268 | while (true) { |
| 269 | const new_capacity = std.math.min(buf.items.len + bump_amt, max_output_bytes); |
| 270 | try buf.ensureTotalCapacity(new_capacity); |
| 271 | const next_buf = buf.unusedCapacitySlice(); |
| 272 | if (next_buf.len == 0) return .full; |
| 273 | var read_bytes: u32 = undefined; |
| 274 | const read_result = windows.kernel32.ReadFile(handle, next_buf.ptr, math.cast(u32, next_buf.len) catch maxInt(u32), &read_bytes, overlapped); |
| 275 | if (read_result == 0) return switch (windows.kernel32.GetLastError()) { |
| 276 | .IO_PENDING => .pending, |
| 277 | .BROKEN_PIPE => .closed, |
| 278 | else => |err| windows.unexpectedError(err), |
| 279 | }; |
| 280 | buf.items.len += read_bytes; |
| 281 | } |
| 282 | } |
| 283 | |
| 255 | 284 | fn collectOutputWindows(child: *const ChildProcess, outs: [2]*std.ArrayList(u8), max_output_bytes: usize) !void { |
| 256 | 285 | const bump_amt = 512; |
| 257 | 286 | const handles = [_]windows.HANDLE{ |
| ... | ... | @@ -274,15 +303,17 @@ pub const ChildProcess = struct { |
| 274 | 303 | |
| 275 | 304 | // Windows Async IO requires an initial call to ReadFile before waiting on the handle |
| 276 | 305 | for ([_]u1{ 0, 1 }) |i| { |
| 277 | | const new_capacity = std.math.min(outs[i].items.len + bump_amt, max_output_bytes); |
| 278 | | try outs[i].ensureTotalCapacity(new_capacity); |
| 279 | | const buf = outs[i].unusedCapacitySlice(); |
| 280 | | _ = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]); |
| 281 | | wait_objects[wait_object_count] = handles[i]; |
| 282 | | wait_object_count += 1; |
| 306 | switch (try windowsAsyncRead(handles[i], &overlapped[i], outs[i], bump_amt, max_output_bytes)) { |
| 307 | .pending => { |
| 308 | wait_objects[wait_object_count] = handles[i]; |
| 309 | wait_object_count += 1; |
| 310 | }, |
| 311 | .closed => {}, // don't add to the wait_objects list |
| 312 | .full => return if (i == 0) error.StdoutStreamTooLong else error.StderrStreamTooLong, |
| 313 | } |
| 283 | 314 | } |
| 284 | 315 | |
| 285 | | while (true) { |
| 316 | while (wait_object_count > 0) { |
| 286 | 317 | const status = windows.kernel32.WaitForMultipleObjects(wait_object_count, &wait_objects, 0, windows.INFINITE); |
| 287 | 318 | if (status == windows.WAIT_FAILED) { |
| 288 | 319 | switch (windows.kernel32.GetLastError()) { |
| ... | ... | @@ -306,23 +337,21 @@ pub const ChildProcess = struct { |
| 306 | 337 | var read_bytes: u32 = undefined; |
| 307 | 338 | if (windows.kernel32.GetOverlappedResult(handles[i], &overlapped[i], &read_bytes, 0) == 0) { |
| 308 | 339 | switch (windows.kernel32.GetLastError()) { |
| 309 | | .BROKEN_PIPE => { |
| 310 | | if (wait_object_count == 0) |
| 311 | | break; |
| 312 | | continue; |
| 313 | | }, |
| 340 | .BROKEN_PIPE => continue, |
| 314 | 341 | else => |err| return windows.unexpectedError(err), |
| 315 | 342 | } |
| 316 | 343 | } |
| 317 | 344 | |
| 318 | 345 | outs[i].items.len += read_bytes; |
| 319 | | const new_capacity = std.math.min(outs[i].items.len + bump_amt, max_output_bytes); |
| 320 | | try outs[i].ensureTotalCapacity(new_capacity); |
| 321 | | const buf = outs[i].unusedCapacitySlice(); |
| 322 | | if (buf.len == 0) return if (i == 0) error.StdoutStreamTooLong else error.StderrStreamTooLong; |
| 323 | | _ = windows.kernel32.ReadFile(handles[i], buf.ptr, math.cast(u32, buf.len) catch maxInt(u32), null, &overlapped[i]); |
| 324 | | wait_objects[wait_object_count] = handles[i]; |
| 325 | | wait_object_count += 1; |
| 346 | |
| 347 | switch (try windowsAsyncRead(handles[i], &overlapped[i], outs[i], bump_amt, max_output_bytes)) { |
| 348 | .pending => { |
| 349 | wait_objects[wait_object_count] = handles[i]; |
| 350 | wait_object_count += 1; |
| 351 | }, |
| 352 | .closed => {}, // don't add to the wait_objects list |
| 353 | .full => return if (i == 0) error.StdoutStreamTooLong else error.StderrStreamTooLong, |
| 354 | } |
| 326 | 355 | } |
| 327 | 356 | } |
| 328 | 357 | |