Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
On a Process not started from mono, WaitForExit does not work and returns immediately
var proc = Process.GetProcessById(1)
WaitForExit(timeout) returns false immediately
WaitForExit returns immediately
using Process.Exited event also causes the event to be fired immediately, without the process having exited
HasExited works correctly
Worked correctly in 4.6.2 and older
Possibly related to this:
Update: it seems its also broken in 4.6.2
This is because there is no way to wait for a non-child process on Unix. We can only approximate if a PID is still alive by using `kill`, but if a PID is reused, then HasExited will still return true, even if the original process has exited. So WaitForExit returning immediately false is the expected behaviour.
I will still investigate the Exited event being fired.
HasExited will still return false *
I added a test case at https://github.com/ludovic-henry/mono/blob/4b1207020c536a91708f9d66033cc2de32ccd773/mcs/class/System/Test/System.Diagnostics/ProcessTest.cs#L1135-L1180 and cannot reproduce the behaviour you are describing, even on mono-4.8.0-branch or mono/master. Can you check if the test case reproduce on your machine, or provide a repro? Thank you.
I think Process.EnableRaisingEvents() needs to be called otherwise the event is never fired (after subscribing, calling it before event subscription races with the subscription because its fired (almost?) immediately)
You are right, EnableRaisingEvents is needed. But even with it, the bug doesn't reproduce with the above testcase on mono/mono-4.8.0-branch and mono/master.
Can you run the attached script on your machine, and possibly modify to reproduce your bug? I ran it with mono-4.8.0-branch/8aed84d and cannot reproduce. Thank you!
Created attachment 19855 [details]
I can't reproduce it with your code, but if you add a Thread.Sleep(10) after
EnableRaisingEvents I can repro it reliably (on a running non-child process)
The problem is that the assertions run too fast and after exiting the using() the event had no chance to fire
Also, it seems that Exceptions thrown inside the Exited handler are just ignored, I had to add a Console.WriteLine to test it
It seems that RegisterWaitForSingleObject() which is used by Process to call the Exited handler silently swallows all exceptions (in .net on Windows it reports an unhandled exception)
Created attachment 19896 [details]
I updated the attached test case to reflect your comment. I can now reproduce your bug on 4.8, but not on master. I cannot guarantee the fix is going to make it into 4.8, but to mitigate the bug, you can simply call `if (!process.HasExited) return;` at the beginning of the `Exited` callback.
You were right in both the race and the swallowed exception. I will investigate the behaviour on .NET for the excepted unhandled exception, and fix Mono accordingly. I will open up a bug for that so we can track this different issues separately.
The updated test on master: https://github.com/mono/mono/pull/4409
The fix for the unhandled exception: https://github.com/mono/mono/pull/4408