Skip to content

gh-161235: Make the signal handler table partially thread safe with QSBR - #161363

Draft
johng wants to merge 2 commits into
python:mainfrom
johng:signalmodule-hanlder-free-fix
Draft

johng wants to merge 2 commits into
python:mainfrom
johng:signalmodule-hanlder-free-fix

Conversation

@johng

@johng johng commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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.

Utilise atomic_exchange and `QSBR` to ensure that accessing an old
handler does not result in a potential UAF.
@nascheme

Copy link
Copy Markdown
Member

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.

@ngoldbaum

ngoldbaum commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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.

@nascheme

nascheme commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

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

@nascheme

Copy link
Copy Markdown
Member

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:

  1. Lock.
  2. Exchange the slot pointer (no decref, no Python code).
  3. Unlock.
  4. 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.

@johng

johng commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Apologies for my description, believe I was meaning to say decref is delayed rather than de-reference.
"QSBR doesn't protect detached threads" realised I didn't make this clear in the description.

For the QSBR approach one of the main benefits I saw is that it helps with races with PyErr_SetInterruptEx under attached threads however I can see an issue with half solving this race condition, and also not helping for sub-interptors too in signal.getsignal, although the suggestion to move handlers to per interpreter state would resolve that I believe.

So with the mutex believe we better handle signal.getsignal race right now to include sub interpreters and without an delayed decref mechanism but we don't solve PyErr_SetInterruptEx for any case.

Let me re-visit the mutex approach per your suggestion.

Thanks!

@johng johng changed the title gh-161235: Make the signal handler table safe thread safe without a lock gh-161235: Make the signal handler table partially thread safe with QSBR Nov 15, 2026
@johng

johng commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

I have done another attempt of the mutex approach with #162137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants