Bug 51133 - System.Diagnostics.Process.OutputDataReceived raised after WaitForExit
Summary: System.Diagnostics.Process.OutputDataReceived raised after WaitForExit
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-12-29 07:34 UTC by Ivan Batrak
Modified: 2017-10-06 23:21 UTC (History)
3 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Code sample to reproduse this behavior (2.46 KB, text/plain)
2016-12-29 07:34 UTC, Ivan Batrak

Description Ivan Batrak 2016-12-29 07:34:22 UTC
Created attachment 19020 [details]
Code sample to reproduse this behavior

Hi all.
I have a program that is used under linux SLES 11.3 with mono as a scheduled task.
It's being used about one year with mono 4.0.2. Couple of days ago i decided to update mono runtime to
I got source code from url http://download.mono-project.com/sources/mono/mono-
After update i found strange behavior of System.Diagnostics.Process standard output.
With 4.6.2 runtime OutputDataReceived events raised a few moments later the process finished its work and System.Diagnostics.Process instance disposed, so i have no idea if there is some output or not.
It occurs in 90% of cases.

Please take a look at test code. It's pretty simple.
Code sample runs ip route get command and parses its output to get ip address used for routing.
To compile the code run mcs Test.cs
To run *.exe run mono Test.exe.
Here is how test code output looks like under mono 4.0.2 

# /opt/mono-4.0.2/bin/mono Test.exe
Determining route to host
ip route get data via YYY.YYY.YYY.YYY dev eth1  src XXX.XXX.XXX.XXX
ip route get data     cache
ip route get data
ip route get data
Process exited
Found ip address from ip route get XXX.XXX.XXX.XXX
Test exit

Here is output from mono 4.6.2 at the same machine.

# /opt/mono-4.6.2/bin/mono Test.exe
Determining route to host
Process exited
ip route get data
ip route get data via YYY.YYY.YYY.YYY dev eth1  src XXX.XXX.XXX.XXX
ip route get text not recognized <no text>
Test exit
ip route get data     cache
Comment 1 Ludovic Henry 2017-10-06 23:21:24 UTC
The behaviour is expected and is the behaviour on .NET.

To guarantee that you wait for all StandardOutput and StandardError to be read, you have to call `Process.WaitForExit` without a timeout. The following change does fix your test:

> diff --git a/repro.cs b/repro.cs
> index dd8de77..53c73b0 100644
> --- a/repro.cs
> +++ b/repro.cs
> @@ -46,6 +46,8 @@ namespace ProcessAsyncOutputTest
>                  }
>                  else
>                      normalExit = true;
> +
> +                Process.WaitForExit();
>              }
>              if (normalExit)

Note You need to log in before you can comment on or make changes to this bug.