@@ -99,6 +99,7 @@ class sigset_t_converter(CConverter):
99
99
#include "pythread.h"
100
100
static unsigned long main_thread ;
101
101
static pid_t main_pid ;
102
+ static PyInterpreterState * main_interp ;
102
103
103
104
static volatile struct {
104
105
_Py_atomic_int tripped ;
@@ -185,6 +186,13 @@ itimer_retval(struct itimerval *iv)
185
186
}
186
187
#endif
187
188
189
+ static int
190
+ is_main (void )
191
+ {
192
+ return PyThread_get_thread_ident () == main_thread &&
193
+ _PyInterpreterState_Get () == main_interp ;
194
+ }
195
+
188
196
static PyObject *
189
197
signal_default_int_handler (PyObject * self , PyObject * args )
190
198
{
@@ -464,7 +472,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
464
472
return NULL ;
465
473
}
466
474
#endif
467
- if (PyThread_get_thread_ident () != main_thread ) {
475
+ if (! is_main () ) {
468
476
PyErr_SetString (PyExc_ValueError ,
469
477
"signal only works in main thread" );
470
478
return NULL ;
@@ -486,7 +494,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler)
486
494
else
487
495
func = signal_handler ;
488
496
/* Check for pending signals before changing signal handler */
489
- if (PyErr_CheckSignals ()) {
497
+ if (_PyErr_CheckSignals ()) {
490
498
return NULL ;
491
499
}
492
500
if (PyOS_setsig (signalnum , func ) == SIG_ERR ) {
@@ -681,7 +689,7 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args, PyObject *kwds)
681
689
return NULL ;
682
690
#endif
683
691
684
- if (PyThread_get_thread_ident () != main_thread ) {
692
+ if (! is_main () ) {
685
693
PyErr_SetString (PyExc_ValueError ,
686
694
"set_wakeup_fd only works in main thread" );
687
695
return NULL ;
@@ -1314,6 +1322,7 @@ PyInit__signal(void)
1314
1322
1315
1323
main_thread = PyThread_get_thread_ident ();
1316
1324
main_pid = getpid ();
1325
+ main_interp = _PyInterpreterState_Get ();
1317
1326
1318
1327
/* Create the module and add the functions */
1319
1328
m = PyModule_Create (& signalmodule );
@@ -1606,16 +1615,25 @@ finisignal(void)
1606
1615
/* Declared in pyerrors.h */
1607
1616
int
1608
1617
PyErr_CheckSignals (void )
1618
+ {
1619
+ if (!is_main ()) {
1620
+ return 0 ;
1621
+ }
1622
+
1623
+ return _PyErr_CheckSignals ();
1624
+ }
1625
+
1626
+
1627
+ /* Declared in cpython/pyerrors.h */
1628
+ int
1629
+ _PyErr_CheckSignals (void )
1609
1630
{
1610
1631
int i ;
1611
1632
PyObject * f ;
1612
1633
1613
1634
if (!_Py_atomic_load (& is_tripped ))
1614
1635
return 0 ;
1615
1636
1616
- if (PyThread_get_thread_ident () != main_thread )
1617
- return 0 ;
1618
-
1619
1637
/*
1620
1638
* The is_tripped variable is meant to speed up the calls to
1621
1639
* PyErr_CheckSignals (both directly or via pending calls) when no
@@ -1687,8 +1705,9 @@ int
1687
1705
PyOS_InterruptOccurred (void )
1688
1706
{
1689
1707
if (_Py_atomic_load_relaxed (& Handlers [SIGINT ].tripped )) {
1690
- if (PyThread_get_thread_ident () != main_thread )
1708
+ if (! is_main ()) {
1691
1709
return 0 ;
1710
+ }
1692
1711
_Py_atomic_store_relaxed (& Handlers [SIGINT ].tripped , 0 );
1693
1712
return 1 ;
1694
1713
}
@@ -1716,12 +1735,13 @@ _PySignal_AfterFork(void)
1716
1735
_clear_pending_signals ();
1717
1736
main_thread = PyThread_get_thread_ident ();
1718
1737
main_pid = getpid ();
1738
+ main_interp = _PyInterpreterState_Get ();
1719
1739
}
1720
1740
1721
1741
int
1722
1742
_PyOS_IsMainThread (void )
1723
1743
{
1724
- return PyThread_get_thread_ident () == main_thread ;
1744
+ return is_main () ;
1725
1745
}
1726
1746
1727
1747
#ifdef MS_WINDOWS
0 commit comments