-
-
Notifications
You must be signed in to change notification settings - Fork 350
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
Document how Trio handles signals #673
Comments
By default, Trio does install a handler for SIGINT (control-C) when you entering
And, if you overwrite Trio's signal handler witha different one, then that's fine, nothing breaks. The uninstall code will even detect this case, and leave your signal handler in place rather than restoring the original signal handler. The only other time Trio installs any signal handlers is if you explicitly use So... I don't think you have anything to worry about. Trio's control-C handling acts exactly like the interpreter's default handling, and if whatever you're doing would work on the vanilla interpreter then it should work identically when trio gets involved. |
....ugh I just looked at the interpreter source code and I think i might be wrong here actually, basically because of an interpreter bug. What I wrote above is what the trio code tries to do. But, when it checks whether the current SIGINT handler is the default one python installed, it does that using the There is a gross workaround available: between starting the python interpreter and installing your C level handler, use The ideal real fix would be for the python signal module to stop assuming that all signal calls will be made through it. But that's a non-trivial change and can't happen before 3.8 at the earliest, so we should open an issue at bugs.python.org but also think if there's a better possible workaround. Ideally trio could detect automatically when this has happened. The Alternatively, yeah, we could provide some kwarg to |
Wow, thanks for checking this in the source as well! Installing a fake handler with the |
Looking more closely at |
It looks like the upstream bug here is bpo-13285, and here's my post about this there: https://bugs.python.org/issue13285#msg325700 |
Hello. Would the issue described below be related, or should I create a separate issue? I am trying to use trio in Pythonista, an iOS app running Python 3.6.1. I am able to use asyncio and aiohttp, but getting quite frustrated with the cancel semantics. Trying to run the following simple example:
I get this trace:
This is surely partially due to Pythonista implementation, but seems related tonthis issue. Are there any non-C workarounds? Thanks! |
@mikaelho How odd... what does That is probably a somewhat different issue, but there is a connection to this issue: that @Nikratio have you confirmed that setting a fake signal handler actually works? Now that I look at Trio's |
@njsmith, |
Let's move the discussion of Pythonista over to #684. |
@njsmith Nope, I did not check whether it works. In the meantime, increased trio prevalence throughout the code meant that the custom signal handlers was no longer required. |
@njsmith how about def is_main_thread():
try:
signal.set_wakeup_fd(-2)
except OSError:
return True
except ValueError:
return False
sys.exit("OH NO WE SET THE WAKEUP FD TO -2") |
@graingert Interesting idea! On Unix, file descriptors are signed On Windows, can -2 ever be a valid value? This gets obscure quickly. According to https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2, in theory a real socket handle could have the value -2:
(Socket handles are unsigned, so -1 is the same as INVALID_SOCKET, which is the same as the maximum possible socket value.) Could this ever happen in practice? On the vast majority of current systems, socket handles are kernel handles, and kernel handles are guaranteed to be a multiple of 4, so -2 can't happen. The only time socket handles aren't kernel handles are if you have some wacky "Layered Service Provider" installed. And even then I assume it's rare that a LSP will happen to pick -2 as a socket handle. (There's a lot more discussion of LSPs in #52.) So... in principle it's possible for I'm not quite sure what to do with that conclusion. All of the issues we're talking about here are very edge-case-y: the original code ran into a bug with old versions of gevent, which has since been fixed. The new code has a problem with C libraries that set their own SIGINT handlers, but we don't have any users actually complaining about this as a practical issue currently. The |
It would probably also be possible to remove all instances of Also in practice, this is currently having exactly zero effect on any users, so the best answer may be to defer any changes it until it causes an actual problem. |
I've come back to this issue because Python signal handling in general is a bit of a pain. Having read through all of the above, it seems to me that in order to use my own, C-level signal handlers the right thing to do is still:
Is that correct? If so, can we maybe add this to the Trio documentation and close this bug? I think a new (and very short) "Signal handling" section under "Trio's Core Functionality" would be the best place. (Hopefully this place could also describe what happens if no custom signal handling is done at all, and the user presses Ctrl-C. Will all tasks receive a KeyboardInterrupt? Or will they just stop executing? Or will they keep running?) |
The documentation and discussion in issue #550 leaves me with the impression that trio is installing some signal handlers. This concerns me, because I would like to use trio in an application that does its own Ctrl-C handling at the C level. Unfortunately I could not find any specifics about what trio is doing (and potential ways to disable it) in the documentation.
It would be great if someone could clarify :-).
The text was updated successfully, but these errors were encountered: