runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Mac OS X - process.WaitForExit() does not work.

Open 616E64726173 opened this issue 6 years ago • 7 comments

Version: DotNet Core 3.1 OS: MacOSX Catalina

Right now I am using "open -W" as a workaround but it's not ideal. Additionally, differences in how MacOS handles Applications means that even if it did work as intended, in most cases we actually want to monitor the spawned child process. Opening TextEdit will in fact invoke TextEdit, but will wait on the entire TextEdit application to exit and not just the opened file.

Correct me if I am wrong, but there is presently no way in .NET to obtain the child PID except by calling Process.GetProcesses() and hoping you select the right one.

Expected behavior: For it to work. Actual behavior: It does not work. Ideal behavior: Return the child process if there is one, likely not possible since the invoked application will be the only one aware of the PID.

            using (var process = Process.Start("chrome", "www.bing.com"))
            {
                process.WaitForExit();
            }

Some Googling reveals people encountering similar issues.

https://stackoverflow.com/questions/55639285/process-waitforexit-is-not-waiting-in-mac-os-x-asp-net-core-2-2

dotnet/corefx/Process.OSX.cs:48 shows that it is invoking open; so I am getting the PID of open and not even the spawned application, explaining why Exited immediately fires.

616E64726173 avatar Feb 17 '20 23:02 616E64726173

@wfurt

scalablecory avatar Feb 18 '20 02:02 scalablecory

TextEdit will in fact invoke TextEdit, but will wait on the entire TextEdit application to exit and not just the opened file.

That is how the Process API work. You can know when a process is finished but it does not give out information when a particular action is taken or completed. It seems like the difference is that Linux would start fresh process but on OSX it could reuse an existing app. So as there is a difference if the app exits when done processing a given task or not. It seems like you would need to find a way how to tell the app to exit.

If you feel there is an issue with WaitForExit, please post runnable repro.

wfurt avatar Feb 18 '20 03:02 wfurt

Yes, there is an issue with WaitForExit on OS X. It doesn't work at all and nowhere in the code does it append -W, which is the wait flag, that causes open to wait for it's child to be closed.

As I said, in dotnet/corefx/Process.OSX.cs:48 you can clearly see that it invokes /usr/bin/open to spin up a process.

dotnet/corefx/System.Diagnostics.Process/src/System/Diagnostics/Process.cs:1333 simply waits for the exit event to be raised, which happens almost immediately as soon as /usr/bin/open completes launching the child process because it's monitoring the PID and events for 'open', which is it's own process, and not the process it launches.

Additionally, because of how this is being done, dotnet is NOT the parent process, bin/open is.

616E64726173 avatar Mar 02 '20 18:03 616E64726173

This issue has been automatically marked as no recent activity because it has been marked as needing more info but has not had any activity for 14 days. It will be closed if no further activity occurs within 7 more days.

ghost avatar Dec 07 '20 02:12 ghost

I'm also interested in a fix for this! We were able to repro this using bool WaitForExit(int milliseconds), but when we switched to void WaitForExit() it seemed to work.

amsoedal avatar Apr 06 '21 11:04 amsoedal

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.