]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-102397: Fix segfault from race condition in signal handling (#102399)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Wed, 8 Mar 2023 07:59:39 +0000 (13:29 +0530)
committerGitHub <noreply@github.com>
Wed, 8 Mar 2023 07:59:39 +0000 (13:29 +0530)
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Lib/test/test_signal.py
Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst [new file with mode: 0644]
Modules/signalmodule.c

index 2562a57ea421ffd4bd36e292e949caf0072b1541..25afd6aabe07519dec78f277c53cfd2576bff536 100644 (file)
@@ -1406,6 +1406,21 @@ class RaiseSignalTest(unittest.TestCase):
         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 (file)
index 0000000..db0b3f3
--- /dev/null
@@ -0,0 +1,2 @@
+Fix segfault from race condition in signal handling during garbage collection.
+Patch by Kumar Aditya.
index cd26eca351c0ed5162bc50cb4ba58dc46f7611d0..0e472e1ee4f9ddcbc78b117928ee9a616be5b029 100644 (file)
@@ -148,6 +148,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;