[Mono-bugs] [Bug 74678][Nor] New - Writing to console should be safe from finalizer

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Wed, 20 Apr 2005 19:48:02 -0400 (EDT)

Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by michi@zeroc.com.


--- shadow/74678	2005-04-20 19:48:02.000000000 -0400
+++ shadow/74678.tmp.14873	2005-04-20 19:48:02.000000000 -0400
@@ -0,0 +1,67 @@
+Bug#: 74678
+Product: Mono: Runtime
+Version: 1.1
+OS: Red Hat 8.0
+OS Details: Dual-CPU
+Status: NEW   
+Priority: Normal
+Component: io-layer
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: michi@zeroc.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+Summary: Writing to console should be safe from finalizer
+Hi, I have an object with a destructor that writes to the console. No 
+problem, works fine. Except when my process calls System.Environment.Exit
+(), in which case it dies with a NullPointerException. This doesn't 
+happen deterministicalle -- on my dual-CPU machine, I get the error maybe 
+once in every ten runs.
+Obviously what is happening is that the GC occasionally destroys stdout 
+and stderr before the finalizer for my object runs, so things blow up 
+The interesting thing is that the .NET runtime does *not* do that. From 
+one of the .NET developers (Brian Grunkemeyer):
+"Back during the implementation of V1.0, I was debugging a problem where 
+a finalizer didnít seem to be called. To help figure out if the finalizer 
+was running at all, I added a call to Console.WriteLine inside it, but 
+then my app started blowing up with an unhandled ObjectDisposedException. 
+How could simply adding a call to Console.WriteLine to the finalizer 
+break the app?
+The problem turned out to be that the underlying console stream was being 
+finalized before my instance. The lesson I learned was to follow this 
+guideline: only use non-finalizable instance data from your own 
+finalizer. But I also happened to own the code for the Console class, so 
+I special cased Console.WriteLineónow we never close the handles for 
+stdout, stdin, or stderr. This is somewhat useful for printf-style 
+debugging and logging, and turned out later to be required to support 
+multiple appdomains within the same process (i.e. you donít want 
+arbitrary appdomains closing your process-wide handle for stdout). So 
+bottom line: using Console from your finalizer is actually a safe thing 
+to do, but watch out for everything else."
+This is taken from: http://www.bluebytesoftware.com/blog/PermaLink.aspx?
+The quoted comment appears about two-thirds down the page.
+Even though the spec does not guarantee that stdout and stderr should be 
+available in finalizers, the .NET runtime is implemented that way, 
+meaning that a program that was developed under .NET and ported to Mono 
+(as is the case for me) may randomly fail during process shutdown. That's 
+not a good thing. And being able to use the console in finalizers is 
+useful during debugging.
+So, could you change Mono to provide the same guarantee please?