From a955cc1e02ba0045d30e70f60e25ba3377c5c8f0 Mon Sep 17 00:00:00 2001 From: jg Date: Mon, 16 Nov 2026 19:59:11 +0000 Subject: [PATCH 1/5] Make the signal handler table partially safe thread safe with a lock Add a mutex which protects access to the handler map under normal access. We delebrately do not have a lock in PyErr_SetInterruptEx due to deadlock concerns. --- Include/internal/pycore_signal.h | 4 +++ Lib/test/test_signal.py | 37 +++++++++++++++++++++++ Modules/signalmodule.c | 50 ++++++++++++++++++++------------ 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 47213a34ab77b5..499fad074150ab 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -42,6 +42,10 @@ struct _signals_runtime_state { int tripped; PyObject* func; } handlers[Py_NSIG]; +#ifdef Py_GIL_DISABLED + // Serializes taking a reference to handlers[i].func with replacing it. + PyMutex handlers_mutex; +#endif volatile struct { #ifdef MS_WINDOWS diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 2cad18a69ff7c0..abf1ada6044e13 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -12,6 +12,7 @@ import threading import time import unittest +import weakref from test import support from test.support import ( force_not_colorized, is_apple, is_apple_mobile, os_helper, threading_helper @@ -1441,6 +1442,42 @@ def handler(a, b): signal.raise_signal(signal.SIGINT) self.assertTrue(is_ok) + def test_handler_replacing_itself_stays_alive(self): + events = [] + def handler(signum, frame): + events.append("enter") + signal.signal(signum, signal.SIG_DFL) + support.gc_collect() + events.append("exit") + handler = functools.partial(handler) + wr = weakref.ref(handler, lambda ref: events.append("freed")) + old_signal = signal.signal(signal.SIGINT, handler) + self.addCleanup(signal.signal, signal.SIGINT, old_signal) + del handler + + signal.raise_signal(signal.SIGINT) + support.gc_collect() + self.assertEqual(events, ["enter", "exit", "freed"]) + + def test_handler_replacing_itself_and_raising_is_freed(self): + events = [] + def handler(signum, frame): + events.append("enter") + signal.signal(signum, signal.SIG_DFL) + support.gc_collect() + events.append("exit") + raise ZeroDivisionError + handler = functools.partial(handler) + wr = weakref.ref(handler, lambda ref: events.append("freed")) + old_signal = signal.signal(signal.SIGINT, handler) + self.addCleanup(signal.signal, signal.SIGINT, old_signal) + del handler + + with self.assertRaises(ZeroDivisionError): + signal.raise_signal(signal.SIGINT) + support.gc_collect() + self.assertEqual(events, ["enter", "exit", "freed"]) + def test__thread_interrupt_main(self): # See https://github.com/python/cpython/issues/102397 code = """if 1: diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index bc5aef55648e07..3196f51f3fc3c8 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -9,7 +9,9 @@ #include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH #include "pycore_interpframe.h" // _PyThreadState_GetFrame() +#include "pycore_lock.h" // _Py_LOCK_DONT_DETACH #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_pyatomic_ft_wrappers.h" // FT_MUTEX_LOCK_FLAGS() #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_signal.h" // _Py_RestoreSignals() @@ -135,12 +137,27 @@ get_handler(int i) return (PyObject *)_Py_atomic_load_ptr(&Handlers[i].func); } -Py_LOCAL_INLINE(void) -set_handler(int i, PyObject* func) +Py_LOCAL_INLINE(PyObject *) +get_handler_ref(int i) { - /* Store func with atomic operation to ensure - that PyErr_SetInterrupt is async-signal-safe. */ - _Py_atomic_store_ptr(&Handlers[i].func, func); + FT_MUTEX_LOCK_FLAGS(&_PyRuntime.signals.handlers_mutex, + _Py_LOCK_DONT_DETACH); + PyObject *func = Py_XNewRef(get_handler(i)); + FT_MUTEX_UNLOCK(&_PyRuntime.signals.handlers_mutex); + return func; +} + +/* Store a new reference to func as the Python handler of signal i and return + the reference to the previous handler, which the caller must release. */ +Py_LOCAL_INLINE(PyObject *) +swap_handler(int i, PyObject *func) +{ + FT_MUTEX_LOCK_FLAGS(&_PyRuntime.signals.handlers_mutex, + _Py_LOCK_DONT_DETACH); + PyObject *old = _Py_atomic_exchange_ptr(&Handlers[i].func, + Py_XNewRef(func)); + FT_MUTEX_UNLOCK(&_PyRuntime.signals.handlers_mutex); + return old; } @@ -532,9 +549,7 @@ signal_signal_impl(PyObject *module, int signalnum, PyObject *handler) return NULL; } - old_handler = get_handler(signalnum); - set_handler(signalnum, Py_NewRef(handler)); - + old_handler = swap_handler(signalnum, handler); if (old_handler != NULL) { return old_handler; } @@ -569,9 +584,9 @@ signal_getsignal_impl(PyObject *module, int signalnum) "signal number out of range"); return NULL; } - old_handler = get_handler(signalnum); + old_handler = get_handler_ref(signalnum); if (old_handler != NULL) { - return Py_NewRef(old_handler); + return old_handler; } else { Py_RETURN_NONE; @@ -1595,9 +1610,7 @@ signal_get_set_handlers(signal_state_t *state, PyObject *mod_dict) } // If signal_module_exec() is called more than one, we must // clear the strong reference to the previous function. - PyObject* old_func = get_handler(signum); - set_handler(signum, Py_NewRef(func)); - Py_XDECREF(old_func); + Py_XDECREF(swap_handler(signum, func)); } // Install Python SIGINT handler which raises KeyboardInterrupt @@ -1609,8 +1622,8 @@ signal_get_set_handlers(signal_state_t *state, PyObject *mod_dict) return -1; } - set_handler(SIGINT, int_handler); - Py_DECREF(sigint_func); + Py_XDECREF(swap_handler(SIGINT, int_handler)); + Py_DECREF(int_handler); PyOS_setsig(SIGINT, signal_handler); } return 0; @@ -1744,9 +1757,8 @@ _PySignal_Fini(void) // Restore default signals and clear handlers for (int signum = 1; signum < Py_NSIG; signum++) { - PyObject *func = get_handler(signum); _Py_atomic_store_int_relaxed(&Handlers[signum].tripped, 0); - set_handler(signum, NULL); + PyObject *func = swap_handler(signum, NULL); if (func != NULL && func != Py_None && !compare_handler(func, state->default_handler) @@ -1845,10 +1857,11 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate) * signal handler for it by the time PyErr_CheckSignals() is called * (see bpo-43406). */ - PyObject *func = get_handler(i); + PyObject *func = get_handler_ref(i); if (func == NULL || func == Py_None || compare_handler(func, state->ignore_handler) || compare_handler(func, state->default_handler)) { + Py_XDECREF(func); /* No Python signal handler due to aforementioned race condition. * We can't call raise() as it would break the assumption * that PyErr_SetInterrupt() only *simulates* an incoming @@ -1882,6 +1895,7 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate) else { result = NULL; } + Py_DECREF(func); if (!result) { /* On error, re-schedule a call to _PyErr_CheckSignalsTstate() */ _Py_atomic_store_int(&is_tripped, 1); From 62e816627a7af8069b0ef845354a773ada1a8541 Mon Sep 17 00:00:00 2001 From: jg Date: Mon, 16 Nov 2026 20:10:53 +0000 Subject: [PATCH 2/5] Add NEWS entry --- .../Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst b/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst new file mode 100644 index 00000000000000..aca0de93760e06 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst @@ -0,0 +1,3 @@ +Fix a crash in :func:`signal.getsignal` on the :term:`free-threaded build` +when another thread replaces the handler. A signal handler that replaces +itself now stays alive until it returns. From 2ea6fc3705ab5d6097ed51295006980d6ffb9acc Mon Sep 17 00:00:00 2001 From: jg Date: Tue, 17 Nov 2026 21:24:37 +0000 Subject: [PATCH 3/5] Enable mutex on GIL build too This helps offer some partial protection for sub interpretors --- Include/internal/pycore_signal.h | 2 -- Lib/test/test_signal.py | 28 ++++++++++++++++++++++++++++ Modules/_testinternalcapi.c | 16 ++++++++++++++++ Modules/signalmodule.c | 13 ++++++------- Python/pystate.c | 1 + 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Include/internal/pycore_signal.h b/Include/internal/pycore_signal.h index 499fad074150ab..8a3ccf00ae6905 100644 --- a/Include/internal/pycore_signal.h +++ b/Include/internal/pycore_signal.h @@ -42,10 +42,8 @@ struct _signals_runtime_state { int tripped; PyObject* func; } handlers[Py_NSIG]; -#ifdef Py_GIL_DISABLED // Serializes taking a reference to handlers[i].func with replacing it. PyMutex handlers_mutex; -#endif volatile struct { #ifdef MS_WINDOWS diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index abf1ada6044e13..4eacdf24cbe76e 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -22,6 +22,10 @@ import _testcapi except ImportError: _testcapi = None +try: + import _testinternalcapi +except ImportError: + _testinternalcapi = None class GenericTests(unittest.TestCase): @@ -180,6 +184,30 @@ def test_keyboard_interrupt_exit_code(self): # Writing an automation friendly test of an interactive shell # to confirm that our process died via a SIGINT proved too complex. + @support.requires_fork() + @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") + def test_fork_with_handler_lock_held(self): + # A thread holding the handler table lock when another thread forks, + # such as one in a subinterpreter with its own GIL, does not exist in + # the child, so the child must not inherit the lock held. + _testinternalcapi.lock_signal_handlers() + try: + pid = os.fork() + # pid returned of 0 if child process + if pid > 0: + return + + try: + signal.getsignal(signal.SIGINT) + except BaseException: + os._exit(1) + os._exit(0) + finally: + _testinternalcapi.unlock_signal_handlers() + + # Failure case of lock being held is exposed via timeout + support.wait_process(pid, exitcode=0) + @unittest.skipUnless(sys.platform == "win32", "Windows specific") class WindowsSignalTests(unittest.TestCase): diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 16be593763bf66..af484e8a46c2b8 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -226,6 +226,20 @@ test_stop_the_world(PyObject *self, PyObject *Py_UNUSED(args)) Py_RETURN_NONE; } +static PyObject * +lock_signal_handlers(PyObject *self, PyObject *Py_UNUSED(args)) +{ + PyMutex_Lock(&_PyRuntime.signals.handlers_mutex); + Py_RETURN_NONE; +} + +static PyObject * +unlock_signal_handlers(PyObject *self, PyObject *Py_UNUSED(args)) +{ + PyMutex_Unlock(&_PyRuntime.signals.handlers_mutex); + Py_RETURN_NONE; +} + #ifdef MS_WINDOWS static const char * classify_address(uintptr_t addr, int jit_enabled, PyInterpreterState *interp) @@ -3349,6 +3363,8 @@ static PyMethodDef module_functions[] = { {"get_stack_pointer", get_stack_pointer, METH_NOARGS}, {"get_stack_margin", get_stack_margin, METH_NOARGS}, {"test_stop_the_world", test_stop_the_world, METH_NOARGS}, + {"lock_signal_handlers", lock_signal_handlers, METH_NOARGS}, + {"unlock_signal_handlers", unlock_signal_handlers, METH_NOARGS}, {"classify_stack_addresses", classify_stack_addresses, METH_VARARGS}, {"get_jit_code_ranges", get_jit_code_ranges, METH_NOARGS}, {"get_jit_backend", get_jit_backend, METH_NOARGS}, diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 3196f51f3fc3c8..d25ef8c74e3c8b 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -11,7 +11,6 @@ #include "pycore_interpframe.h" // _PyThreadState_GetFrame() #include "pycore_lock.h" // _Py_LOCK_DONT_DETACH #include "pycore_moduleobject.h" // _PyModule_GetState() -#include "pycore_pyatomic_ft_wrappers.h" // FT_MUTEX_LOCK_FLAGS() #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_signal.h" // _Py_RestoreSignals() @@ -140,10 +139,10 @@ get_handler(int i) Py_LOCAL_INLINE(PyObject *) get_handler_ref(int i) { - FT_MUTEX_LOCK_FLAGS(&_PyRuntime.signals.handlers_mutex, - _Py_LOCK_DONT_DETACH); + PyMutex_LockFlags(&_PyRuntime.signals.handlers_mutex, + _Py_LOCK_DONT_DETACH); PyObject *func = Py_XNewRef(get_handler(i)); - FT_MUTEX_UNLOCK(&_PyRuntime.signals.handlers_mutex); + PyMutex_Unlock(&_PyRuntime.signals.handlers_mutex); return func; } @@ -152,11 +151,11 @@ get_handler_ref(int i) Py_LOCAL_INLINE(PyObject *) swap_handler(int i, PyObject *func) { - FT_MUTEX_LOCK_FLAGS(&_PyRuntime.signals.handlers_mutex, - _Py_LOCK_DONT_DETACH); + PyMutex_LockFlags(&_PyRuntime.signals.handlers_mutex, + _Py_LOCK_DONT_DETACH); PyObject *old = _Py_atomic_exchange_ptr(&Handlers[i].func, Py_XNewRef(func)); - FT_MUTEX_UNLOCK(&_PyRuntime.signals.handlers_mutex); + PyMutex_Unlock(&_PyRuntime.signals.handlers_mutex); return old; } diff --git a/Python/pystate.c b/Python/pystate.c index 81360f4999849d..2f620e93e759bc 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -322,6 +322,7 @@ _Py_COMP_DIAG_POP &(runtime)->_main_interpreter.types.mutex, \ &(runtime)->_main_interpreter.code_state.mutex, \ &(runtime)->_main_interpreter.dict_state.watcher_mutex, \ + &(runtime)->signals.handlers_mutex, \ } static void From 1245746cee406b2f321b390e3a70c474bb2ed137 Mon Sep 17 00:00:00 2001 From: jg Date: Tue, 17 Nov 2026 21:38:47 +0000 Subject: [PATCH 4/5] Fixes --- Lib/test/test_signal.py | 18 ++++++++---------- ...6-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst | 7 ++++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 4eacdf24cbe76e..ba146cee2f948d 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -193,19 +193,17 @@ def test_fork_with_handler_lock_held(self): _testinternalcapi.lock_signal_handlers() try: pid = os.fork() - # pid returned of 0 if child process - if pid > 0: - return - - try: - signal.getsignal(signal.SIGINT) - except BaseException: - os._exit(1) - os._exit(0) + if pid == 0: + # In the child: hangs here if the lock was inherited held. + try: + signal.getsignal(signal.SIGINT) + except BaseException: + os._exit(1) + os._exit(0) finally: _testinternalcapi.unlock_signal_handlers() - # Failure case of lock being held is exposed via timeout + # A child stuck on the inherited lock never exits, so this times out. support.wait_process(pid, exitcode=0) diff --git a/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst b/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst index aca0de93760e06..bb286fb4e9479f 100644 --- a/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst +++ b/Misc/NEWS.d/next/Library/2026-11-02-22-45-00.gh-issue-161235.sGt4Hd.rst @@ -1,3 +1,4 @@ -Fix a crash in :func:`signal.getsignal` on the :term:`free-threaded build` -when another thread replaces the handler. A signal handler that replaces -itself now stays alive until it returns. +Fix use-after-free crashes in :mod:`signal`. On all builds, a signal +handler that replaces itself is now kept alive until it returns. On the +:term:`free-threaded build`, :func:`signal.getsignal` no longer crashes +when another thread replaces the handler. From da0da5244b1add2c1d58b3d29e7c585a1aeb86e7 Mon Sep 17 00:00:00 2001 From: jg Date: Tue, 17 Nov 2026 22:02:03 +0000 Subject: [PATCH 5/5] Ignore the fork-with-threads warning in the fork test regrtest's per-test timeout runs a faulthandler watchdog thread, so os.fork() warns, and CI's -W error turns the warning into an error. --- Lib/test/test_signal.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index ba146cee2f948d..d02f7f27459827 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -15,7 +15,8 @@ import weakref from test import support from test.support import ( - force_not_colorized, is_apple, is_apple_mobile, os_helper, threading_helper + force_not_colorized, is_apple, is_apple_mobile, os_helper, threading_helper, + warnings_helper, ) from test.support.script_helper import assert_python_ok, spawn_python try: @@ -186,6 +187,7 @@ def test_keyboard_interrupt_exit_code(self): @support.requires_fork() @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") + @warnings_helper.ignore_fork_in_thread_deprecation_warnings() def test_fork_with_handler_lock_held(self): # A thread holding the handler table lock when another thread forks, # such as one in a subinterpreter with its own GIL, does not exist in