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

Skip to content

Commit ace9cf5

Browse files
fix race in signal handling
1 parent 4e7c0cb commit ace9cf5

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Lib/test/test_signal.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _thread
12
import enum
23
import errno
34
import inspect
@@ -1406,6 +1407,20 @@ def handler(a, b):
14061407
signal.raise_signal(signal.SIGINT)
14071408
self.assertTrue(is_ok)
14081409

1410+
def test__thread_interrupt_main(self):
1411+
code = """if 1:
1412+
import _thread
1413+
class Foo():
1414+
def __del__(self):
1415+
_thread.interrupt_main()
1416+
1417+
x = Foo()
1418+
"""
1419+
1420+
rc, out, err = assert_python_ok('-c', code)
1421+
self.assertIn(b'OSError: Signal 2 ignored due to race condition', err)
1422+
1423+
14091424

14101425
class PidfdSignalTest(unittest.TestCase):
14111426

Modules/signalmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ get_signal_state(PyObject *module)
148148
static inline int
149149
compare_handler(PyObject *func, PyObject *dfl_ign_handler)
150150
{
151+
if (func == NULL || dfl_ign_handler == NULL) {
152+
return 0;
153+
}
151154
assert(PyLong_CheckExact(dfl_ign_handler));
152155
if (!PyLong_CheckExact(func)) {
153156
return 0;

0 commit comments

Comments
 (0)