Bug 20503 - GC.WaitForPendingFinalizers() doesn't wait for pending finalizers
Summary: GC.WaitForPendingFinalizers() doesn't wait for pending finalizers
Alias: None
Product: Runtime
Classification: Mono
Component: GC ()
Version: 3.4.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Paolo Molaro
Depends on:
Reported: 2014-06-10 02:01 UTC by Jonathan Pryor
Modified: 2014-08-26 18:37 UTC (History)
4 users (show)

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:

Description Jonathan Pryor 2014-06-10 02:01:06 UTC
Consider the following program to "test" finalizer execution:

  using System;
  using System.Threading;

  class T {
    public static int C;

    public T ()
      Interlocked.Increment (ref C);

    ~T ()
      Console.WriteLine (" T.Finalize");
      Interlocked.Decrement (ref C);

  class M {
    static void Main ()
      var t = new Thread (() => {
          int i = 0;
          while (Test ()) {
            Console.WriteLine ("# iteration: {0}", i++);
      t.Start ();
      t.Join ();

    public static bool Test ()
      WeakReference r = null;
      var t = new Thread (() => {
          var x = new T ();
          r = new WeakReference (x);
      t.Start ();
      t.Join ();

      GC.Collect ();
      GC.WaitForPendingFinalizers ();

      try {
        int c = T.C;
        if (c != 0) {
          Console.WriteLine ("  SHOULD NOT HAPPEN: T.C={0}", c);
          return false;
        return true;
      } finally {
        Console.WriteLine ("  test thread complete.");


  $ mono --version
  Mono JIT compiler version 3.4.0 ((no/c3fc3ba Thu May  1 22:52:17 EDT 2014)
  $ mcs wfpf.cs && mono wfpf.exe 
    test thread complete.
  # iteration: 0
    test thread complete.
  # iteration: 1
    test thread complete.
  # iteration: 2
    test thread complete.
  # iteration: 3
    test thread complete.
  # iteration: 4
    test thread complete.

Expected results; an infinite loop; no program exit.

Background/discussion: It is frequently useful/necessary to test finalizers, such as T.Finalize(). The Kumpera-recommended approach for testing them is done in M.Test():

1. Create a new Thread()
2. Create a new instance of the desired type within that Thread
3. Store a reference to the instance within a WeakReference (for later querying/testing to ensure it was actually finalized), 
4. Start() then Join() the thread
5. Call GC.Collect() and GC.WaitForPendingFinalizers() to allow the finalizer to execute.

However, as seen in the above output the T.Finalize() method may be invoked _after_ GC.WaitForPendingFinalizers() returns! The above approach is not consistently safe, and fails "randomly" (in that it won't always die on the 5th iteration; it may be the first, or the 20th...).

Is the above M.Test() approach correct? Should GC.WaitForPendingFinalizers() actually block until the T instance has had it's finalizer run, as opposed to ~immediately _after_ GC.WaitForPendingFinalizers() has returned?

WORKAROUND: The only way to make M.Test() behave reliably is to repeat the GC.WaitForPendingFinalizers() call, e.g.

      GC.Collect ();
      GC.WaitForPendingFinalizers ();
      GC.WaitForPendingFinalizers ();

Is this intentional and/or necessary? Should it be?

Additional information: Removing the Thread from M.Main() makes things slightly more reliable, in that instead of failing after ~5 invocations it'll take several hundred. However, it still fails.
Comment 1 Zoltan Varga 2014-07-01 14:36:21 UTC
The following seems to happen:
- the finalizer thread starts running some finalizers
- the main thread calls GC.Collect ()
- the gc notifies the finalizer thread that there are pending finalizers
- the main thread calls WaitForPendingFinalizers ()
- the finalizer thread finishes the last round of finalizers and sets the pending event which WaitForPendingFinalizers () is waiting on.
- WaitForPendingFinalizers () returns even through the finalizers queued by the last gc haven't ran yet.
Comment 2 Zoltan Varga 2014-07-01 15:11:04 UTC
Fixed in mono master a57fb5b645e8989e7f33b66a6594c9d87cb52188.
Comment 3 Saurabh 2014-08-05 10:10:14 UTC
I have checked this issue with following builds and observed that execution gets stop after some time. this is the screencast for the same: http://www.screencast.com/t/XHaLPcikRt

=== Xamarin Studio ===

Version 5.3 (build 412)
Installation UUID: 2591d519-875d-4afe-a3d9-5fcf391bbd2d
	Mono 3.8.0 ((no/9ffca33)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000002

=== Xamarin.Android ===

Version: 4.16.0 (Business Edition)
Android SDK: /Users/nischal/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: /usr
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

=== Apple Developer Tools ===

Xcode 4.6.3 (2068)
Build 4H1503

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 606f31a
Build date: 2014-08-01 15:27:48-0400

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 503000412
Git revision: 77e042831804079a649ee83092ed36c7c10773a2
Build date: 2014-08-01 06:32:22-04
Xamarin addins: 57e379e6a9454a1c0d97aa3f770e7ae7cc16c522

=== Operating System ===

Mac OS X 10.7.4
Darwin nischals-Mac-mini.local 11.4.0 Darwin Kernel Version 11.4.0
    Mon Apr  9 19:32:15 PDT 2012
    root:xnu-1699.26.8~1/RELEASE_X86_64 x86_64
Comment 4 Zoltan Varga 2014-08-26 18:37:52 UTC
I can reproduce this with 3.8.0 and master. What seems to happen is that the object stays alive somehow, so its finalizer is not called. Since we are using a conservative gc, this cannot be eliminated in all cases.