Skip to content

gh-161235: Make the signal handler table partially safe thread safe with a lock - #162137

Open
johng wants to merge 5 commits into
python:mainfrom
johng:signal-handler-mutex
Open

johng wants to merge 5 commits into
python:mainfrom
johng:signal-handler-mutex

Conversation

@johng

@johng johng commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

This is an alternative proposal to leveraging lock free QSBR approach

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.
Comment thread Modules/signalmodule.c Outdated
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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Believe setting this flag will ensure that the lock is released before a stop the world before the fork, and this also helps on shutdown too ensure we wait for the lock to be released by any attaching threads.

@ngoldbaum

Copy link
Copy Markdown
Contributor

This looks like a reasonable incremental fix to me. The lock protects taking a reference, with callbacks and decrefs happening after unlocking. It also leaves room to fix PyErr_SetInterruptEx() separately by having it read atomic state to decide whether to queue a signal, without touching Python objects or taking this lock. Moving the Python handler table into interpreter state later also seems compatible with this approach.

Could we consider getsignal() from a subinterpreter now, though? Protecting table access there would require enabling the mutex on the GIL-enabled build too, since interpreters with separate GILs can access the table concurrently. That wouldn’t by itself resolve the problems with sharing the handler object across interpreters. We’d also need to revisit fork handling, since the current argument for not resetting the mutex relies on stopping all interpreters, which only happens on the free-threaded build.

This helps offer some partial protection
for sub interpretors
Comment thread Lib/test/test_signal.py

@support.requires_fork()
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_fork_with_handler_lock_held(self):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit on the fence about this test since it mutates the internal state setting a lock before fork. Believe this is the only approach to reliably test that we don't hold the mutex on fork

@johng
johng marked this pull request as ready for review November 17, 2026 21:25
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants