@@ -73,11 +73,12 @@ class sigset_t_converter(CConverter):
73
73
/*
74
74
NOTES ON THE INTERACTION BETWEEN SIGNALS AND THREADS
75
75
76
- When threads are supported, we want the following semantics:
76
+ We want the following semantics:
77
77
78
78
- only the main thread can set a signal handler
79
+ - only the main thread runs the signal handler
80
+ - signals can be delivered to any thread
79
81
- any thread can get a signal handler
80
- - signals are only delivered to the main thread
81
82
82
83
I.e. we don't support "synchronous signals" like SIGFPE (catching
83
84
this doesn't make much sense in Python anyway) nor do we support
@@ -88,17 +89,17 @@ class sigset_t_converter(CConverter):
88
89
We still have the problem that in some implementations signals
89
90
generated by the keyboard (e.g. SIGINT) are delivered to all
90
91
threads (e.g. SGI), while in others (e.g. Solaris) such signals are
91
- delivered to one random thread (an intermediate possibility would
92
- be to deliver it to the main thread -- POSIX?). For now, we have
93
- a working implementation that works in all three cases -- the
94
- handler ignores signals if getpid() isn't the same as in the main
95
- thread. XXX This is a hack.
92
+ delivered to one random thread. On Linux, signals are delivered to
93
+ the main thread (unless the main thread is blocking the signal, for
94
+ example because it's already handling the same signal). Since we
95
+ allow signals to be delivered to any thread, this works fine. The
96
+ only oddity is that the thread executing the Python signal handler
97
+ may not be the thread that received the signal.
96
98
*/
97
99
98
100
#include <sys/types.h> /* For pid_t */
99
101
#include "pythread.h"
100
102
static unsigned long main_thread ;
101
- static pid_t main_pid ;
102
103
static PyInterpreterState * main_interp ;
103
104
104
105
static volatile struct {
@@ -326,11 +327,7 @@ signal_handler(int sig_num)
326
327
{
327
328
int save_errno = errno ;
328
329
329
- /* See NOTES section above */
330
- if (getpid () == main_pid )
331
- {
332
- trip_signal (sig_num );
333
- }
330
+ trip_signal (sig_num );
334
331
335
332
#ifndef HAVE_SIGACTION
336
333
#ifdef SIGCHLD
@@ -1328,7 +1325,6 @@ PyInit__signal(void)
1328
1325
int i ;
1329
1326
1330
1327
main_thread = PyThread_get_thread_ident ();
1331
- main_pid = getpid ();
1332
1328
main_interp = _PyInterpreterState_Get ();
1333
1329
1334
1330
/* Create the module and add the functions */
@@ -1739,7 +1735,6 @@ _PySignal_AfterFork(void)
1739
1735
* the interpreter had an opportunity to call the handlers. issue9535. */
1740
1736
_clear_pending_signals ();
1741
1737
main_thread = PyThread_get_thread_ident ();
1742
- main_pid = getpid ();
1743
1738
main_interp = _PyInterpreterState_Get ();
1744
1739
}
1745
1740
0 commit comments