Patchwork libgo patch committed: Avoid crash at thread exit

login
register
mail settings
Submitter Ian Taylor
Date Jan. 22, 2011, 3 a.m.
Message ID <mcrzkqtabhq.fsf@google.com>
Download mbox | patch
Permalink /patch/79975/
State New
Headers show

Comments

Ian Taylor - Jan. 22, 2011, 3 a.m.
If the garbage collector runs just as a thread is exiting, it can
collect the M structure for the exiting thread.  This can then crash
when locking or unlocking the heap for some final statistics gathering.
This patch fixes that problem.  Bootstrapped and ran Go testsuite on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian

Patch

diff -r f68f39af572b libgo/runtime/go-go.c
--- a/libgo/runtime/go-go.c	Fri Jan 21 17:58:59 2011 -0800
+++ b/libgo/runtime/go-go.c	Fri Jan 21 18:09:14 2011 -0800
@@ -297,6 +297,15 @@ 
 {
   struct M *pm = m;
 
+  if (__sync_bool_compare_and_swap (&pm->holds_finlock, 1, 1))
+    {
+      /* We can't interrupt the thread while it holds the finalizer
+	 lock.  Otherwise we can get into a deadlock when mark calls
+	 runtime_walkfintab.  */
+      __sync_bool_compare_and_swap (&pm->gcing_for_finlock, 0, 1);
+      return;
+    }
+
   if (__sync_bool_compare_and_swap (&pm->mallocing, 1, 1))
     {
       /* m->mallocing was already non-zero.  We can't interrupt the
@@ -315,15 +324,6 @@ 
       return;
     }
 
-  if (__sync_bool_compare_and_swap (&pm->holds_finlock, 1, 1))
-    {
-      /* Similarly, we can't interrupt the thread while it holds the
-	 finalizer lock.  Otherwise we can get into a deadlock when
-	 mark calls runtime_walkfintab.  */
-      __sync_bool_compare_and_swap (&pm->gcing_for_finlock, 0, 1);
-      return;
-    }
-
   stop_for_gc ();
 }