GH-102397: Fix segfault from race condition in signal handling (GH-102399)

(cherry picked from commit 1a84cc007e)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
Miss Islington (bot) 2023-03-08 00:26:20 -08:00 committed by GitHub
parent 8bf8e3d9a0
commit 7905ae7b1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View file

@ -1413,6 +1413,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):

View file

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

View file

@ -181,6 +181,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;