Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8023099

Browse files
committed
Add SF patch #468347 -- mask signals for non-main pthreads, by Jason Lowe:
This patch updates Python/thread_pthread.h to mask all signals for any thread created. This will keep all signals masked for any thread that isn't the initial thread. For Solaris and Linux, the two platforms I was able to test it on, it solves bug #465673 (pthreads need signal protection) and probably will solve bug #219772 (Interactive InterPreter+ Thread -> core dump at exit). I'd be great if this could get some testing on other platforms, especially HP-UX pre 11.00 and post 11.00, as I had to make some guesses for the DCE thread case. AIX is also a concern as I saw some mention of using sigthreadmask() as a pthread_sigmask() equivalent, but this patch doesn't use sigthreadmask(). I don't have access to AIX.
1 parent 3a40f32 commit 8023099

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Python/thread_pthread.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66
#include <pthread.h>
7+
#include <signal.h>
78

89

910
/* try to determine what version of the Pthread Standard is installed.
@@ -69,6 +70,18 @@
6970
#endif
7071

7172

73+
/* On platforms that don't use standard POSIX threads pthread_sigmask()
74+
* isn't present. DEC threads uses sigprocmask() instead as do most
75+
* other UNIX International compliant systems that don't have the full
76+
* pthread implementation.
77+
*/
78+
#ifdef PY_PTHREAD_STD
79+
# define SET_THREAD_SIGMASK pthread_sigmask
80+
#else
81+
# define SET_THREAD_SIGMASK sigprocmask
82+
#endif
83+
84+
7285
/* A pthread mutex isn't sufficient to model the Python lock type
7386
* because, according to Draft 5 of the docs (P1003.4a/D5), both of the
7487
* following are undefined:
@@ -135,6 +148,7 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
135148
{
136149
pthread_t th;
137150
int success;
151+
sigset_t oldmask, newmask;
138152
#if defined(THREAD_STACK_SIZE) || defined(PTHREAD_SYSTEM_SCHED_SUPPORTED)
139153
pthread_attr_t attrs;
140154
#endif
@@ -151,6 +165,14 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
151165
#ifdef PTHREAD_SYSTEM_SCHED_SUPPORTED
152166
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
153167
#endif
168+
169+
/* Mask all signals in the current thread before creating the new
170+
* thread. This causes the new thread to start with all signals
171+
* blocked.
172+
*/
173+
sigfillset(&newmask);
174+
SET_THREAD_SIGMASK(SIG_BLOCK, &newmask, &oldmask);
175+
154176
success = pthread_create(&th,
155177
#if defined(PY_PTHREAD_D4)
156178
pthread_attr_default,
@@ -174,6 +196,10 @@ PyThread_start_new_thread(void (*func)(void *), void *arg)
174196
(void *)arg
175197
#endif
176198
);
199+
200+
/* Restore signal mask for original thread */
201+
SET_THREAD_SIGMASK(SIG_SETMASK, &oldmask, NULL);
202+
177203
#ifdef THREAD_STACK_SIZE
178204
pthread_attr_destroy(&attrs);
179205
#endif

0 commit comments

Comments
 (0)