From ace9cf5c167d1bf69ff37e1367f335888c6a0a2a Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:02:50 +0000 Subject: [PATCH 1/5] fix race in signal handling --- Lib/test/test_signal.py | 15 +++++++++++++++ Modules/signalmodule.c | 3 +++ 2 files changed, 18 insertions(+) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 2562a57ea421ff..0ee55fe5286152 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1,3 +1,4 @@ +import _thread import enum import errno import inspect @@ -1406,6 +1407,20 @@ def handler(a, b): signal.raise_signal(signal.SIGINT) self.assertTrue(is_ok) + def test__thread_interrupt_main(self): + 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/Modules/signalmodule.c b/Modules/signalmodule.c index cd26eca351c0ed..a19b724e0c4711 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -148,6 +148,9 @@ get_signal_state(PyObject *module) static inline int compare_handler(PyObject *func, PyObject *dfl_ign_handler) { + if (func == NULL || dfl_ign_handler == NULL) { + return 0; + } assert(PyLong_CheckExact(dfl_ign_handler)); if (!PyLong_CheckExact(func)) { return 0; From f52a04666dbc16640cc467ebec94687d7343e61c Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 3 Mar 2023 15:04:22 +0000 Subject: [PATCH 2/5] comment --- Lib/test/test_signal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 0ee55fe5286152..25afd6aabe0751 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -1,4 +1,3 @@ -import _thread import enum import errno import inspect @@ -1408,6 +1407,7 @@ def handler(a, b): 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(): From 1e14780337628074c787c6465fa3ab1d8ac98d64 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 4 Mar 2023 06:48:36 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst 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 00000000000000..394b1a33853152 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst @@ -0,0 +1 @@ +Fix segfault from race condition in signal handling. Patch by Kumar Aditya. From 7c01cdd51f6e68032f89565903b66e33e421afe2 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Tue, 7 Mar 2023 23:10:02 -0800 Subject: [PATCH 4/5] mention that it happens during garbage collection --- .../2023-03-04-06-48-34.gh-issue-102397.ACJaOf.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 394b1a33853152..db0b3f32c2ec0b 100644 --- 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 @@ -1 +1,2 @@ -Fix segfault from race condition in signal handling. Patch by Kumar Aditya. +Fix segfault from race condition in signal handling during garbage collection. +Patch by Kumar Aditya. From 50e870d1c6bc0d9ddf2c2a59a36b88bb88fd33f5 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 8 Mar 2023 13:00:38 +0530 Subject: [PATCH 5/5] add comment --- Modules/signalmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index a19b724e0c4711..0e472e1ee4f9dd 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -148,6 +148,7 @@ 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; }