-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
Ctrl+C behavior problems at the new REPL #3007
Comments
Obvious disclaimer that I don't really know/remember trio internals + how KI is delivered. I assume there's issues running the trio loop off the main thread? (we could probably take a trio token from it and then use the same strategy as we do now, just whether a thread is main or not is reversed) |
Yes, running the trio loop in a non-main thread will break the ability to interrupt "normal" running code on all platforms, at least without some additional work. |
The additional work is not too bad: wrap each thing you submit to the Trio loop in a cancel scope, and on SIGINT, cancel it. |
Will that interrupt Edit: sorry, that sounds a bit snooty as I read it again. The system is complex enough that I am genuinely unsure if the trio signal handler will get called or not in that situation. |
Good point, it won't. The only way to interrupt that on a non-main thread is https://docs.python.org/3/c-api/init.html#c.PyThreadState_SetAsyncExc . It is not actually necessary to write one's own C extension; you can call it via
Solving the problems that appear when taking input on the non-main thread is probably easier than solving the problems that appear when running Trio code on the non-main thread. |
So the new asyncio REPL (or rather, porting |
Note to self, |
It seemed a little too good to be true that Ctrl+C "just works" in the new Trio REPL (#2972, #3002), it turns out there are a couple weird problems. Interrupting running sync and async code seems to go fine, but when you are just sitting at the input prompt, KI cannot reach that code in the "usual" fashion. I've noticed 3 issues so far:
Runner._ki_pending
flag survives returning to the promptConsider executing a simple uninterruptible operation:
>>> await trio.to_thread.run_sync(time.sleep, 5)
This should and does last 5 seconds even if you try to interrupt it. However, Trio is still trying to get rid of the KI hot potato, so the next checkpoint will fire off a KI:
This one seems to be easy enough to solve, either dig into the Runner and manually reset the flag every time we're done processing input, or run
trio.from_thread.run(trio.lowlevel.checkpoint_if_cancelled)
and it will safely propagate to the normal console KI code. However, after doing that exposed the next two issues.input
on Windows sees Ctrl+C even though there is a handler AND it's not in the main threadIf you hit Ctrl+C any time the console is waiting for input, the Trio run ends:
(Weirdly, this doesn't happen during PyCharm's terminal emulation!) This is very simply a result of
input
popping outEOFError
as if you'd done Ctrl+Z+Enter:I suppose in the main thread this is overridden by the
KeyboardInterrupt
at some point for the python REPL behavior to work right. This seems like a matter of transforming the Exception in our console subclass:This works fine as far as I can tell.
input
on Linux DOES NOT see Ctrl+C (because it's not in the main thread)This isn't so bad as it only means KI at the input prompt doesn't take effect until you finish entering a (possibly multiline) command, and even then the command runs to completion. This is weird, but maybe tolerable? I think the worst part is that you can't quickly discard/drop out of multiline edits. Furthermore, I have no idea how to fix this.
Summary
This is an issue instead of a PR because there might be a smarter way to interact with KI that would also work on linux, and I wanted other people to follow up on this after experimenting a bit if there's any other weird behavior that isn't covered by the KI above checks (such as the linux case).
The text was updated successfully, but these errors were encountered: