runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Process.WaitForExit() hangs forever even though the process has exited in some cases

Open madelson opened this issue 7 years ago • 39 comments

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.

madelson avatar Apr 11 '19 11:04 madelson

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?

tmds avatar Apr 12 '19 12:04 tmds

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 avatar Apr 12 '19 12:04 tmds

@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"

danmoseley avatar Apr 13 '19 01:04 danmoseley

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

tmds avatar Apr 13 '19 05:04 tmds

@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 = true of Process3
  • Close() StandardOutput&Error of Process2

tmds avatar Apr 13 '19 06:04 tmds

@tmds I do not know, and an not able to examine the code at the moment. Perhaps @krwq @stephentoub know.

danmoseley avatar Apr 13 '19 11:04 danmoseley

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 avatar Apr 13 '19 11:04 madelson

@madelson you can do the Close, that should work too.

tmds avatar Apr 13 '19 13:04 tmds

@madelson can you please confirm this fixes it?

danmoseley avatar Apr 15 '19 19:04 danmoseley

@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

madelson avatar Apr 15 '19 22:04 madelson

Perhaps @krwq @stephentoub know.

@krwq @stephentoub do you know why WaitForExit reads standard out and error until EOF?

tmds avatar Apr 17 '19 16:04 tmds

@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 avatar Apr 20 '19 02:04 krwq

@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 avatar Apr 20 '19 20:04 madelson

@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.

tmds avatar Apr 23 '19 06:04 tmds

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

tmds avatar Apr 25 '19 07:04 tmds

Yeah, the "eof" part is a bit of a misnomer. It's really waiting until all background processing completes.

stephentoub avatar Apr 25 '19 10:04 stephentoub

@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).

madelson avatar Apr 26 '19 23:04 madelson

Is there a workaround for this?

Peperud avatar Dec 11 '19 01:12 Peperud

@Peperud see above https://github.com/dotnet/corefx/issues/36783#issuecomment-482780224

danmoseley avatar Dec 11 '19 03:12 danmoseley

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.

madelson avatar Dec 11 '19 11:12 madelson