Skip to content

Commit c4fb418

Browse files
miss-islingtonkumaraditya303gpshead
authored
[3.10] GH-102397: Fix segfault from race condition in signal handling (GH-102399) (#102527)
GH-102397: Fix segfault from race condition in signal handling (GH-102399) (cherry picked from commit 1a84cc0) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent 5e6351c commit c4fb418

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
@@ -1349,6 +1349,21 @@ def handler(a, b):
13491349
signal.raise_signal(signal.SIGINT)
13501350
self.assertTrue(is_ok)
13511351

1352+
def test__thread_interrupt_main(self):
1353+
# See https://github.com/python/cpython/issues/102397
1354+
code = """if 1:
1355+
import _thread
1356+
class Foo():
1357+
def __del__(self):
1358+
_thread.interrupt_main()
1359+
1360+
x = Foo()
1361+
"""
1362+
1363+
rc, out, err = assert_python_ok('-c', code)
1364+
self.assertIn(b'OSError: Signal 2 ignored due to race condition', err)
1365+
1366+
13521367

13531368
class PidfdSignalTest(unittest.TestCase):
13541369

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
@@ -177,6 +177,10 @@ get_signal_state(PyObject *module)
177177
static inline int
178178
compare_handler(PyObject *func, PyObject *dfl_ign_handler)
179179
{
180+
// See https://github.com/python/cpython/pull/102399
181+
if (func == NULL || dfl_ign_handler == NULL) {
182+
return 0;
183+
}
180184
assert(PyLong_CheckExact(dfl_ign_handler));
181185
if (!PyLong_CheckExact(func)) {
182186
return 0;

0 commit comments

Comments
 (0)