Skip to content

Conversation

@sergey-miryanov
Copy link
Contributor

@sergey-miryanov sergey-miryanov commented Nov 8, 2025

Trying to be a bit safer for non-fatal signals

@sergey-miryanov
Copy link
Contributor Author

@vstinner Could you please take a look?

@sergey-miryanov sergey-miryanov requested review from AA-Turner and efimov-mikhail and removed request for AA-Turner November 8, 2025 18:58
@sergey-miryanov
Copy link
Contributor Author

@AA-Turner sorry, misclick.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

We can't acquire the GIL or make a stop-the-world pause inside signal handlers, because they're not signal safe. For example, Python could receive the signal while in the middle of a stop-the-world pause, which would deadlock inside the signal handler.

@bedevere-app
Copy link

bedevere-app bot commented Nov 8, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be poked with soft cushions!

@sergey-miryanov
Copy link
Contributor Author

@ZeroIntensity If I get you right (feel free to correct me):

  1. _PyThreadState_Attach is not signal-safe. If we call _PyThreadState_Attach from within faulthandler_user then another signal can call faulthandler_user again and this led to undefined state. IIUC, we can handle this with the reentrant flag, as faulthandler_dump_traceback did.
  2. _PyEval_StopTheWorld is not signal-safe too. If we already in the stop-the-world state then calling _PyEval_StopTheWorld again will lead to deadlock on stoptheworld_mutex. And we can't handle this without changing the semantics of _PyEval_StopTheWorld. I don't intend to do this.

Therefore, if we want to safely handle non-fatal signals, then using the signal module seems like the only option. Or leave them as-is.

@ZeroIntensity
Copy link
Member

IIUC, we can handle this with the reentrant flag, as faulthandler_dump_traceback did.

Nope, because _PyThreadState_Attach will also try to acquire locks, leading to a deadlock if they're called under a signal.

I think it would be best to just document that faulthandler sometimes crashes if you mess with it.

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