Process.WaitForExit() hangs forever even though the process has exited in some cases
Setup
Launch a process with stream redirection where that process itself launches a long-running process:
// in the host process (process1.exe)
var process2 = new Process
{
StartInfo =
{
FileName = "process2.exe"
UseShellExecute = false,
RedirectStandardOutput = true
}
};
process2.OutputDataReceived += (o, e) => Console.WriteLine(e.Data);
process2.Start();
process2.BeginOutputReadLine();
process2.WaitForExit(); // never returns, even though process2 does exit fairly quickly
return; // returns from Main(), exiting
// in process2.exe
Console.WriteLine("Launching process3.exe!");
var process3 = new Process { StartInfo = { FileName = "process3.exe", UseShellExecute = false } };
process3.Start();
process3.Dispose();
// in process3.exe
while (true) // loop basically forever performing background tasks
{
...
}
Expected Behavior
Process1 receives the output of Process2. Once Process2 has exited, Process1 can move on from its WaitForExit() call. Meanwhile Process3 continues to run.
Actual Behavior
WaitForExit() never returns.
Notes
I believe the problem is that WaitForExit() waits for EOF to be returned from redirected streams that are being consumed asynchronously. However, I think that Process3 is somehow inheriting the stdio handles from Process2 which prevents EOF from being sent even after Process2 exits.
Note that unlike dotnet/runtime#26165 and dotnet/runtime#27115 , I am observing the behavior on Windows. I'm not sure if the same would happen on Linux.
I believe the problem is that WaitForExit() waits for EOF to be returned from redirected streams that are being consumed asynchronously. However, I think that Process3 is somehow inheriting the stdio handles from Process2 which prevents EOF from being sent even after Process2 exits.
Yes, Process2 and Process3 share the same standard out. What happens when you RedirectStandardOutput = true for Process3?
WaitForExit involves waiting for the output to reach EOF, which won't happen because Process3 is keeping it alive.
https://github.com/dotnet/corefx/blob/007571b90c3cc555cd30f3b467b87c22f9f665e0/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs#L210-L228
@tmds the code above is Unix, the report is Windows (you probably intended this but just pointing out in case)
Looks like the code has a typo "WaitUtilEOF"
For Windows, we do the WaitUtilEOF also:
https://github.com/dotnet/corefx/blob/007571b90c3cc555cd30f3b467b87c22f9f665e0/src/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L173-L183
@danmosemsft what is the rationale for the WaitUtilEOF? From the code I think it is not really needed on Unix, but it seems to serve a role on Windows: the handle is Disposed after the WaitUtilEOF.
@madelson these two options should make it do what you want:
RedirectStandardOutput&Error = trueof Process3Close()StandardOutput&Errorof Process2
@tmds I do not know, and an not able to examine the code at the moment. Perhaps @krwq @stephentoub know.
What happens when you RedirectStandardOutput = true for Process3?
@tmds I agree that this would probably fix this specific situation. The problem is that in this case I don't control the source of process dotnet/corefx#2; that's just what it does.
@madelson you can do the Close, that should work too.
@madelson can you please confirm this fixes it?
@danmosemsft I will confirm soon when I next get a chance to retry the code. Thinking about this more I’m not convinced it will fix
Perhaps @krwq @stephentoub know.
@krwq @stephentoub do you know why WaitForExit reads standard out and error until EOF?
@tmds got no context on that, perhaps some simplification to avoid races between events and exit. If the question is just about linux then I suspect the code was ported from Windows and it's there for parity - if you think it shouldn't be there I think it makes sense to try to remove it and try running tests for some time in a loop and perhaps add some more targeted to see if there are any issues.
@krwq I imagine that partly what it's trying to achieve is to give you some reasonable way of knowing when everything is done and no more data is coming. With WaitForExit() not returning until this happens, it allows you to set up data received events, wait for the process to finish, and then move on.
@madelson did the Close of process2 stdout/stderr work for you?
I think it makes sense to try to remove it and try running tests for some time in a loop and perhaps add some more targeted to see if there are any issues.
We should keep it in both, or remove it in both, to have the same behavior. I wonder if on Windows the Dispose of the handle would affect the behavior of standard out and standard error if they were not yet read until EOF. I'll make a PR that removes the readuntileof and see if some tests fail.
Without the WaitUntilEOF tests fail that depend on OutputdataReceived being called before WaitForExit returns:
https://github.com/dotnet/corefx/blob/4e1b64e1ab06202e9aa451a0b320b7fd4ed11519/src/System.Net.Ping/tests/FunctionalTests/UnixPingUtilityTests.cs#L74-L75
https://github.com/dotnet/corefx/blob/4e1b64e1ab06202e9aa451a0b320b7fd4ed11519/src/System.Diagnostics.Process/tests/ProcessStreamReadTests.cs#L88
https://github.com/dotnet/corefx/blob/4e1b64e1ab06202e9aa451a0b320b7fd4ed11519/src/System.Diagnostics.Process/tests/ProcessStreamReadTests.cs#L417
Yeah, the "eof" part is a bit of a misnomer. It's really waiting until all background processing completes.
@stephentoub perhaps a solution would be for the process streams to support cancellation / timeout. If they did, then something could be set up where the process exiting cancels any outstanding reads (although even in that case you might have to try one more read on the stream to be sure that there's nothing left in the pipe).
Is there a workaround for this?
@Peperud see above https://github.com/dotnet/corefx/issues/36783#issuecomment-482780224
Close() StandardOutput&Error of Process2
did the Close of process2 stdout/stderr work for you?
@tmds I don't think this is a workaround because I am trying to capture the standard output of process2. If I close stdout before waiting for exit, I won't get the output. If I wait for exit before closing, then the program hangs.