Skip to content

Commit 7905ae7

Browse files
miss-islingtonkumaraditya303gpshead
authored
GH-102397: Fix segfault from race condition in signal handling (GH-102399)
(cherry picked from commit 1a84cc0) Co-authored-by: Kumar Aditya <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 8bf8e3d commit 7905ae7

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/test/test_signal.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,21 @@ def handler(a, b):
14131413
signal.raise_signal(signal.SIGINT)
14141414
self.assertTrue(is_ok)
14151415

1416+
def test__thread_interrupt_main(self):
1417+
# See https://github.com/python/cpython/issues/102397
1418+
code = """if 1:
1419+
import _thread
1420+
class Foo():
1421+
def __del__(self):
1422+
_thread.interrupt_main()
1423+
1424+
x = Foo()
1425+
"""
1426+
1427+
rc, out, err = assert_python_ok('-c', code)
1428+
self.assertIn(b'OSError: Signal 2 ignored due to race condition', err)
1429+
1430+
14161431

14171432
class PidfdSignalTest(unittest.TestCase):
14181433

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ get_signal_state(PyObject *module)
181181
static inline int
182182
compare_handler(PyObject *func, PyObject *dfl_ign_handler)
183183
{
184+
// See https://github.com/python/cpython/pull/102399
185+
if (func == NULL || dfl_ign_handler == NULL) {
186+
return 0;
187+
}
184188
assert(PyLong_CheckExact(dfl_ign_handler));
185189
if (!PyLong_CheckExact(func)) {
186190
return 0;

0 commit comments

Comments
 (0)