diff mbox

libgo patch committed: Block signals when creating new thread

Message ID mcrk3qtuuo9.fsf@google.com
State New
Headers show

Commit Message

Ian Lance Taylor Jan. 31, 2013, 5:30 p.m. UTC
This patch to libgo disables signals while creating a new thread.
Otherwise if a signal comes in between the time the thread starts and
the time the thread initializes its m and g TLS variables, the program
will crash with an error "signal received on thread not created by Go."
Signals are already enabled by the thread after it has been initialized.
Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.

Ian
diff mbox

Patch

diff -r 4716407240c2 libgo/runtime/proc.c
--- a/libgo/runtime/proc.c	Thu Jan 31 08:39:01 2013 -0800
+++ b/libgo/runtime/proc.c	Thu Jan 31 09:24:12 2013 -0800
@@ -3,6 +3,7 @@ 
 // license that can be found in the LICENSE file.
 
 #include <limits.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <pthread.h>
 #include <unistd.h>
@@ -1217,6 +1218,9 @@ 
 	pthread_attr_t attr;
 	pthread_t tid;
 	size_t stacksize;
+	sigset_t clear;
+	sigset_t old;
+	int ret;
 
 #if 0
 	static const Type *mtype;  // The Go type M
@@ -1249,7 +1253,15 @@ 
 	if(pthread_attr_setstacksize(&attr, stacksize) != 0)
 		runtime_throw("pthread_attr_setstacksize");
 
-	if(pthread_create(&tid, &attr, runtime_mstart, mp) != 0)
+	// Block signals during pthread_create so that the new thread
+	// starts with signals disabled.  It will enable them in minit.
+	sigfillset(&clear);
+	sigemptyset(&old);
+	sigprocmask(SIG_BLOCK, &clear, &old);
+	ret = pthread_create(&tid, &attr, runtime_mstart, mp);
+	sigprocmask(SIG_SETMASK, &old, nil);
+
+	if (ret != 0)
 		runtime_throw("pthread_create");
 
 	return mp;