You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
std.ChildProcess can deadlock if both stdout and stderr are long and interleaved #6343
breakingImplementing this issue could cause existing code to no longer compile or have different behavior.bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
This is a flaw in the API design because this example code above will be duplicated many times in users of this API. We need to eliminate this footgun.
conststd=@import("std");
pubfnmain() !void {
constallocator=std.heap.page_allocator;
constargs=trystd.process.argsAlloc(allocator);
if (args.len!=1) {
// Enough to saturate the pipe buffertrystd.io.getStdErr().writeAll("A"**65536);
trystd.io.getStdErr().writeAll("A"**65536);
return;
}
constchild=trystd.ChildProcess.exec(.{
.allocator=allocator,
// Re-exec this program and fill the stderr pipe
.argv= &[_][]constu8{ "foo", "-nonexistentflag" },
.max_output_bytes=1_000_000,
});
std.debug.print("O {}", .{child.stdout});
std.debug.print("E {}", .{child.stderr});
}
From my POV the API is great as-is, if the user asks for stdout/stderr to be a pipe they must be ready to handle both streams at the same time using async IO/threads or whatever they prefer. The problem here is that some consumers, such as the ones in the Zig codebase marked with todo, only need a way to execute some command and read all the output.
To accommodate such uses of ChildProcess I'd propose to:
Add the polling logic in exec. This is the blessed function that abstracts the complexity away from the user, reading stdin/stdout by hand is still allowed but you're on your own.
Add a way to combine the stdin and stdout into a single stream, as many times you just need the complete subprocess output.
Making the polling work on Windows is, as always, a bit challenging as select() doesn't work for pipes and neither do many other parts of the async IO API. A quick search shows that pretty much everyone is "emulating" select by polling (PeekNamedPipe) and sleeping... Not the most elegant solution but it seems to work.
I did a partial merge of @LemonBoy's #6750 which solves the problem for the exec use case. The partial merge solves the issue for POSIX but not Windows. I'm closing the PR because it's an old draft, but this comment is a reminder that there is working(?) tested(?) code for Windows sitting around that could be utilized to solve this use case.
breakingImplementing this issue could cause existing code to no longer compile or have different behavior.bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Activity
LemonBoy commentedon Oct 19, 2020
A simple test case to show the deadlock on Linux:
From my POV the API is great as-is, if the user asks for stdout/stderr to be a pipe they must be ready to handle both streams at the same time using async IO/threads or whatever they prefer. The problem here is that some consumers, such as the ones in the Zig codebase marked with
todo, only need a way to execute some command and read all the output.To accommodate such uses of
ChildProcessI'd propose to:exec. This is the blessed function that abstracts the complexity away from the user, reading stdin/stdout by hand is still allowed but you're on your own.Making the polling work on Windows is, as always, a bit challenging as
select()doesn't work for pipes and neither do many other parts of the async IO API. A quick search shows that pretty much everyone is "emulating" select by polling (PeekNamedPipe) and sleeping... Not the most elegant solution but it seems to work.prime31 commentedon Nov 21, 2020
I just ran into the deadlock on macos with a process that dumps a lot of data to stderr.
LemonBoy commentedon Nov 21, 2020
Does #6750 work for you?
andrewrk commentedon Dec 29, 2020
I did a partial merge of @LemonBoy's #6750 which solves the problem for the
execuse case. The partial merge solves the issue for POSIX but not Windows. I'm closing the PR because it's an old draft, but this comment is a reminder that there is working(?) tested(?) code for Windows sitting around that could be utilized to solve this use case.marler8997 commentedon Jun 17, 2021
I ran into this today. I put together this program to reproduce on Windows:
LemonBoy commentedon Jun 17, 2021
Try applying the windows code path contained in #6750.
marler8997 commentedon Jun 17, 2021
@LemonBoy, tested and it fixes the issue :)
andrewrk commentedon Sep 16, 2021
Fixed by @marler8997 and the fix landed in 0.8.1.