Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore signal handlers at the end of open_signal_receiver. #689

Open
1 task done
jonathanslenders opened this issue Feb 12, 2024 · 3 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@jonathanslenders
Copy link
Contributor

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

Feature description

Would it be possible to restore the original signal handlers at the end of open_signal_receiver instead of only removing them? I see that currently, we don't do that. Is there reason for this?

To restore signal handlers, they also have to be restored using ctypes after calling remove_signal_handler. See this code for an example on how to do that: https://github.com/prompt-toolkit/python-prompt-toolkit/blob/465ab02854763fafc0099a2e38a56328c1cb0625/src/prompt_toolkit/application/application.py#L1596

Use case

(Use case is obvious I think. We have an application for which a main signal handler is installed at the beginning, however, during a portion of the code, we want to use open_signal_receiver to override the signal handler for that portion.)

@jonathanslenders jonathanslenders added the enhancement New feature or request label Feb 12, 2024
@agronholm
Copy link
Owner

This is impossible to do with asyncio's API.

@jonathanslenders
Copy link
Contributor Author

Right, I see it's mentioned in the docs here: https://anyio.readthedocs.io/en/stable/api.html#anyio.open_signal_receiver

I'd think we could simply pop the current handler from get_running_loop()._signal_handlers. Why would that be a private API? Are you aware of any effort on the asyncio side to expose a public API for this?

@agronholm
Copy link
Owner

Why would that be a private API?

If it's named with a leading underscore, and not included in the documentation, then it's private API. That said, maybe signal handling on asyncio could be done using the signal module directly, while jumping through some hoops? That would be a cleaner solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants