[Python-Dev] Python threads end up blocking signals in subprocesses

Jeff Epler jepler at unpythonic.net
Sun Dec 21 18:07:04 EST 2003


On Sun, Dec 21, 2003 at 11:27:45AM +0100, Martin v. Löwis wrote:
> OTOH, we already have PyOS_AfterFork, which could be used instead of
> pthread_atfork. Jeff, would you like to add some code there, to set
> all signal handlers into default for which Handlers lists that the
> default handling should occur?

When using pthread_atfork, os.system never triggers my code.  However,
reimplementing os.system in terms of os.fork+os.execv, it does.  I don't
know if this is right or wrong according to pthread, but since it doesn't
work on my platform the question is academic for me.

Wouldn't the PyOS_AfterFork approach also require python to provide its
own versions of any POSIX APIs that would typically be implemented in
terms of fork (system(), popen(), and spawn*() come to mind)?

Jeff

Index: Python/thread_pthread.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/thread_pthread.h,v
retrieving revision 2.48
diff -u -r2.48 thread_pthread.h
--- Python/thread_pthread.h	19 Nov 2003 22:52:22 -0000	2.48
+++ Python/thread_pthread.h	21 Dec 2003 23:03:52 -0000
@@ -143,6 +143,17 @@
  * Initialization.
  */
 
+static void PyThread__fork_child(void) {
+	/* Mask all signals in the current thread before creating the new
+	 * thread.  This causes the new thread to start with all signals
+	 * blocked.
+	 */
+	sigset_t childmask;
+	sigfillset(&childmask);
+	SET_THREAD_SIGMASK(SIG_UNBLOCK, &childmask, NULL);
+	fprintf(stderr, "PyThread__fork_child()\n"); fflush(stderr);
+}
+
 #ifdef _HAVE_BSDI
 static
 void _noop(void)
@@ -157,6 +168,7 @@
 	pthread_t thread1;
 	pthread_create(&thread1, NULL, (void *) _noop, &dummy);
 	pthread_join(thread1, NULL);
+	pthread_atfork(NULL, NULL, PyThread__fork_child);
 }
 
 #else /* !_HAVE_BSDI */
@@ -167,6 +179,7 @@
 #if defined(_AIX) && defined(__GNUC__)
 	pthread_init();
 #endif
+	pthread_atfork(NULL, NULL, PyThread__fork_child);
 }
 
 #endif /* !_HAVE_BSDI */



More information about the Python-Dev mailing list