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

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


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


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED ANSWERED

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 4.6.2.7.
I got source code from url http://download.mono-project.com/sources/mono/mono-4.6.2.7.tar.bz2
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 8.8.8.8 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 8.8.8.8
ip route get data 8.8.8.8 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 8.8.8.8
Process exited
ip route get data
ip route get data 8.8.8.8 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)