[Python-checkins] [3.10] GH-102397: Fix segfault from race condition in signal handling (GH-102399) (#102527)

kumaraditya303 webhook-mailer at python.org
Wed Mar 8 03:21:20 EST 2023


https://github.com/python/cpython/commit/c4fb41816f6045dd608d4d97266f16feaf974b83
commit: c4fb41816f6045dd608d4d97266f16feaf974b83
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: kumaraditya303 <59607654+kumaraditya303 at users.noreply.github.com>
date: 2023-03-08T13:51:13+05:30
summary:

[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 1a84cc007e207f2dd61f86a7fc3d86632fdce72f)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg at krypto.org>

files:
A Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst
M Lib/test/test_signal.py
M Modules/signalmodule.c

diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index c2b5861fc3e9..294cf3888d11 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -1349,6 +1349,21 @@ def handler(a, b):
         signal.raise_signal(signal.SIGINT)
         self.assertTrue(is_ok)
 
+    def test__thread_interrupt_main(self):
+        # See https://github.com/python/cpython/issues/102397
+        code = """if 1:
+        import _thread
+        class Foo():
+            def __del__(self):
+                _thread.interrupt_main()
+
+        x = Foo()
+        """
+
+        rc, out, err = assert_python_ok('-c', code)
+        self.assertIn(b'OSError: Signal 2 ignored due to race condition', err)
+
+
 
 class PidfdSignalTest(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst b/Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst
new file mode 100644
index 000000000000..db0b3f32c2ec
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst	
@@ -0,0 +1,2 @@
+Fix segfault from race condition in signal handling during garbage collection.
+Patch by Kumar Aditya.
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 80fa4d50eef7..bcfd5537708d 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -177,6 +177,10 @@ get_signal_state(PyObject *module)
 static inline int
 compare_handler(PyObject *func, PyObject *dfl_ign_handler)
 {
+    // See https://github.com/python/cpython/pull/102399
+    if (func == NULL || dfl_ign_handler == NULL) {
+        return 0;
+    }
     assert(PyLong_CheckExact(dfl_ign_handler));
     if (!PyLong_CheckExact(func)) {
         return 0;



More information about the Python-checkins mailing list