Bug 48484 - System.Diagnostics.Process.Exited raised more than once if Dispose is called in handler
Summary: System.Diagnostics.Process.Exited raised more than once if Dispose is called ...
Status: RESOLVED FEATURE
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 4.6.0 (C8)
Hardware: All Linux
: --- normal
Target Milestone: Untriaged
Assignee: Ludovic Henry
URL:
Depends on:
Blocks:
 
Reported: 2016-11-30 22:38 UTC by Brandon White
Modified: 2016-12-01 23:14 UTC (History)
3 users (show)

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


Attachments
Minimal reproducing test program. (4.86 KB, application/x-zip-compressed)
2016-11-30 22:38 UTC, Brandon White
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 FEATURE

Description Brandon White 2016-11-30 22:38:21 UTC
Created attachment 18725 [details]
Minimal reproducing test program.

If Process.Dispose is called within an Exited handler, then sometimes the handler gets invoked a second time.

For instance:

````
        private static void Process_Exited(object sender, EventArgs e)
        {
            ++exitCount;
            Console.WriteLine("Process_Exited");
            
            ((IDisposable)sender).Dispose();
        }

````

When SIGTERM is sent to the app, this appears to lead to the handler being invoked twice.  The weird thing is, it is not consistent.  Sometimes you get a single call as expected, and sometimes you get the spurious second call

This behavior seems to have changed somewhere between 4.2.x and 4.8.x.  I just started seeing this after updating from 4.2.x to 4.8.x.  In my prior 4.2.x runtime, these double calls never happened.

Attached is a minimal reproducing test program.

````
[root@ts4712-470053:~]# cat /etc/debian_version
7.8

[root@ts4712-470053:~]# mono -V
Mono JIT compiler version 4.8.0 (Stable 4.8.0.344/f5fbc32 Fri Nov 18 13:04:32 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       normal
        Notifications: epoll
        Architecture:  armel,vfp+fallback
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

````
Comment 1 Brandon White 2016-12-01 14:09:17 UTC
I have now also reproduced this on 4.6.2.7 Ubuntu PC, using the attached program.

$ mono -V
Mono JIT compiler version 4.6.2 (Stable 4.6.2.7/08fd525 Mon Nov 14 12:30:00 UTC 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen
Comment 2 Ludovic Henry 2016-12-01 23:05:23 UTC
Hi Brandon,

I can reproduce the same with .NET, so this is not a bug but the expected behavior.

This surfaced because starting with 4.6 we use the System.Diagnostics.Process implementation from referencesource.

Thank you for the bug report,
Ludovic
Comment 3 Brandon White 2016-12-01 23:14:53 UTC
Thanks Ludovic.

For the benefit of anyone coming across this later, I have found setting EnableRaisingEvents to false before calling Dispose prevents the extraneous Exited event.