Skip to content

Commit 1a84cc0

Browse files
GH-102397: Fix segfault from race condition in signal handling (#102399)
Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 061325e commit 1a84cc0

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/test/test_signal.py

+15
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,21 @@ def handler(a, b):
14061406
signal.raise_signal(signal.SIGINT)
14071407
self.assertTrue(is_ok)
14081408

1409+
def test__thread_interrupt_main(self):
1410+
# See https://github.com/python/cpython/issues/102397
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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix segfault from race condition in signal handling during garbage collection.
2+
Patch by Kumar Aditya.

Modules/signalmodule.c

+4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ get_signal_state(PyObject *module)
148148
static inline int
149149
compare_handler(PyObject *func, PyObject *dfl_ign_handler)
150150
{
151+
// See https://github.com/python/cpython/pull/102399
152+
if (func == NULL || dfl_ign_handler == NULL) {
153+
return 0;
154+
}
151155
assert(PyLong_CheckExact(dfl_ign_handler));
152156
if (!PyLong_CheckExact(func)) {
153157
return 0;

0 commit comments

Comments
 (0)