Bug 4550 - Finalizer is never executed
Summary: Finalizer is never executed
Status: RESOLVED INVALID
Alias: None
Product: Runtime
Classification: Mono
Component: GC ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Rodrigo Kumpera
URL:
Depends on:
Blocks:
 
Reported: 2012-04-20 05:45 UTC by Marek Safar
Modified: 2012-04-20 19:25 UTC (History)
2 users (show)

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

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 INVALID

Description Marek Safar 2012-04-20 05:45:03 UTC
using System;
using System.Threading.Tasks;
using System.Threading;

class C
{
	public static void Main ()
	{
		var mre = new ManualResetEvent (false);
		TaskScheduler.UnobservedTaskException += (o, args) => {
			Console.WriteLine ("From finalizer");
			args.SetObserved ();
			mre.Set ();
		};
		
		while (true) {
			mre.Reset ();
			var inner = new ApplicationException ();
			CreateFaultedTaskCompletionSource (inner);
			GC.Collect ();
			Console.WriteLine ("WaitForPendingFinalizers");
			GC.WaitForPendingFinalizers ();
			Console.WriteLine ("Wait is over");
			
			if (!mre.WaitOne (5000))
				throw new ApplicationException ();
		}
	}

	static void CreateFaultedTaskCompletionSource (Exception inner)
	{
		var tcs = new TaskCompletionSource<int> ();
		tcs.SetException (inner);
		tcs = null;
	}	
}


Just run it, it works reliably on .NET. I have tried both GCs, various settings but it does not work for me.
Comment 1 Marek Safar 2012-04-20 05:45:59 UTC
My output

WaitForPendingFinalizers
From finalizer
Wait is over
WaitForPendingFinalizers
Wait is over

Unhandled Exception: System.ApplicationException: An application exception has occurred.

I am not certain it's GC bug but for now it looks like one.
Comment 2 Rodrigo Kumpera 2012-04-20 12:05:35 UTC
Why a GC bug and not a PTL bug?
Comment 3 Rodrigo Kumpera 2012-04-20 12:06:28 UTC
The exception is been raised because TaskScheduler.UnobservedTaskException is not been fired, the GC doesn't fire events.
Comment 4 Rodrigo Kumpera 2012-04-20 19:25:26 UTC
This is caused by mono's conservative scanning, no bug here.