GeezerVison is showing github.com — gh-161235: Make the signal handler table safe thread safe without a lock by johng · Pull Request #161363 · python/cpython
Conversation
johng
commented
Nov 4, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Utilise atomic_exchange and QSBR to ensure that accessing an old handler does not result in a potential UAF. This fixes 2 out of 3 race conditions identified in the issue.
Swapping signal handlers can result in a UAF if another thread accesses the old handler slot that could have been freed. On swapping the old reference is given to the caller, however other accessors such as in the signal handler function do not maintain a strong reference. Taking a strong reference there could equally race too.
The proposed solution in the attached issue is to deference only at a quiescent point, this enables lock free access yet callers under most circumstances can be sure the handler won't be freed.
Happy to discuss this further on the parent issue if wider design discussion should be preferred at this stage, this PR is to demonstrate the first proposed solution from the issue.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Utilise atomic_exchange and `QSBR` to ensure that accessing an old
handler does not result in a potential UAF.
|
This looks like the correct fix to me. A few comments:
- description on the PR doesn't match the implementation
- in the news file "now stays alive until it returns" also applies to GIL-enabled build, suggest rewording to be clearer
- related bug, can be a separate issue: PyErr_SetInterruptEx is documented to not need attached threadstate but
compare_handler() looks at func. QSBR doesn't protect detached threads.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
ngoldbaum
commented
Nov 12, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
One other case is getsignal() from a subinterpreter: it currently returns the main interpreter’s handler object, but the delayed decref only waits for readers in the main interpreter.
Would it make sense to move the Python handler table into per-interpreter state? Python signal handlers can currently only be installed and run in the main interpreter, so we’d need to decide what getsignal() should do in a subinterpreter. Not sure if the current racy behavior is intended -- probably not! The C signal state would still need to be process-wide, but avoiding shared Python handler objects would make their lifetime easier to manage.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
nascheme
commented
Nov 12, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Another approach would be to use a mutex, rather than QSBR. In general it's good to avoid putting more things in the QSBR queue. Also, since the hander is size zero, it doesn't advance the QSBR sequence. This is not on a hot path and so the extra cost of a mutex (which is quite small anyhow) should not be significant. One complication with a mutex, we need to reset it after forking, like the import lock.
A third option would be to use tryincref. That gives prompt de-allocation and nothing to do after fork. Neither the mutex or tryincref fixes the PyErr_SetInterruptEx problem, unfortunately.
Edit: comparison of different options:
| --- |
PyMutex |
TryIncref |
QSBR |
| Old handler freed |
immediately |
immediately |
at next GC / queue flush |
| Readers |
lock around load + incref |
lock-free retry loop |
lock-free |
| Extra requirements |
reinit in _PySignal_AfterFork |
_PyObject_SetMaybeWeakref on every stored handler; object must live in a mimalloc object heap |
none |
| Subtlety |
low |
high |
medium |
Fixes PyErr_SetInterruptEx |
no |
no |
no |
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Actually, we don't need the reset after fork, just need to be careful not to be holding the mutex when fork happens. The decref of the old handler must be outside the critical section since a destructor could call fork(). Order would be for writer:
- Lock.
- Exchange the slot pointer (no decref, no Python code).
- Unlock.
- Decref the old handler; any destructor, including one that forks, runs here with no lock held
For reader: lock, load, incref, unlock, then call the handler. An incref never runs arbitrary code, so nothing inside either locked region can fork, detach or re-enter. I think I prefer the mutex approach.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|