$OpenBSD: patch-mono_metadata_gc_c,v 1.2 2013/01/24 13:52:09 kurt Exp $
--- mono/metadata/gc.c.orig	Mon Jan 30 13:01:23 2012
+++ mono/metadata/gc.c	Mon Jan 21 16:48:48 2013
@@ -1130,7 +1130,7 @@ mono_gc_init (void)
 	MONO_SEM_INIT (&finalizer_sem, 0);
 #endif
 
-	gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, 0);
+	gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, TRUE, 0);
 	ves_icall_System_Threading_Thread_SetName_internal (gc_thread, mono_string_new (mono_domain_get (), "Finalizer"));
 }
 
@@ -1175,6 +1175,21 @@ mono_gc_cleanup (void)
 					Sleep (100);
 				}
 
+			} else {
+				int ret;
+
+                                /* Wait for the thread to actually exit */
+                                ret = WaitForSingleObjectEx (gc_thread->handle, INFINITE, TRUE);
+                                g_assert (ret == WAIT_OBJECT_0);
+
+#ifndef HOST_WIN32
+                               /*
+                                * The above wait only waits for the exited event to be signalled, the thread might still be running. To fix this race, we
+                                * create the finalizer thread without calling pthread_detach () on it, so we can wait for it manually.
+                                */
+                               ret = pthread_join ((gpointer)(gsize)gc_thread->tid, NULL);
+                               g_assert (ret == 0);
+#endif
 			}
 		}
 		gc_thread = NULL;
