GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@johng johng commented Nov 4, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

  • Issue: signalmodule.c in free-threaded Python #161235

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@johng
Make the signal handler table safe thread safe without a lock
2123fdc
Utilise atomic_exchange and `QSBR` to ensure that accessing an old
handler does not result in a potential UAF.
@bedevere-app bedevere-app Bot mentioned this pull request Nov 4, 2026
signalmodule.c in free-threaded Python #161235
Open
@johng
Keep the async-signal-safety comment on the handler slot update
8a42e24
@nascheme

nascheme commented Nov 12, 2026

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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum

ngoldbaum commented Nov 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nascheme

nascheme commented Nov 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nascheme

nascheme commented Nov 12, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

3 participants

@johng @nascheme @ngoldbaum
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.