-
-
Notifications
You must be signed in to change notification settings - Fork 358
Improve REPL KI #3030
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
base: main
Are you sure you want to change the base?
Improve REPL KI #3030
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3030 +/- ##
====================================================
- Coverage 100.00000% 99.91749% -0.08251%
====================================================
Files 127 127
Lines 19265 19392 +127
Branches 1302 1324 +22
====================================================
+ Hits 19265 19376 +111
- Misses 0 15 +15
- Partials 0 1 +1
🚀 New features to boost your workflow:
|
Could we spawn the REPL as another process and send it a signal? (simulate ctrl+c) I think we could first send it some data over stdin to get into specific scenarios. |
The newline is sent to the terminal I think, rather than any specific process. Potentially we could avoid the ioctl weirdness by allowing a small amount of user discomfort in needing to press enter after Ctrl+C. |
Still no idea how to test the proposed fixes. I think it's relevant that asyncio isn't able to interrupt the input thread either, unless it's using the new python repl: Was there ever talk about the |
src/trio/_repl.py
Outdated
nonlocal interrupted | ||
interrupted = True | ||
# Fake up a newline char as if user had typed it at terminal | ||
fcntl.ioctl(sys.stdin, termios.TIOCSTI, b"\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can't write to sys.stdin from a handler safely, I'm not sure if you can icotl to the .fileno() or not either, I think you need:
@disable_ki_protection
def _newline():
# Fake up a newline char as if user had typed it at terminal
fcntl.ioctl(sys.stdin, termios.TIOCSTI, b"\n")
class TrioInteractiveConsole:
token = trio.lowlevel.current_trio_token()
def handler(...):
nonlocal interrupted
interrupted = True
token.run_sync_soon(_newline, idempotent=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think I understand why, but won't a keyboard interrupt in the newline function blow up trio and end the repl? If so, Would suppressing the exception be sufficient?
74760af
to
620a5c0
Compare
02ae29d
to
a5dcdbf
Compare
Uh turns out pyrepl is a separate package that cpython vendored: https://pypi.org/project/pyrepl/#history I have no intention of vendoring it or making a dependency for this pr but just FYI |
I was talking about just sending
Interesting, I thought it was copied over from PyPy. Anyways, the interrupt thing is https://github.com/python/cpython/blob/2fd09b011031f3c00c342b44e02e2817010e507c/Lib/asyncio/__main__.py#L135-L142 I think. And then later they have a loop where they try |
I would be happy to push this to the finish line. Here's a couple things I noticed:
import trio
import sys
import subprocess
import signal
from functools import partial
async def main():
async with trio.open_nursery() as nursery:
proc = await nursery.start(partial(
trio.run_process,
[sys.executable, "-m", "trio"],
# shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
))
async with proc.stdout, proc.stderr:
# setup
await anext(proc.stderr)
assert await anext(proc.stdout) == b'>>> '
# ensure things work
await proc.stdin.send_all(b'print("hello!")\n')
assert await anext(proc.stdout) == b'hello!\n>>> '
# ensure that ctrl+c on the REPL works
proc.send_signal(signal.SIGINT)
print("stdout:")
with trio.move_on_after(0.5):
async for c in proc.stdout:
print(c)
print("stderr:")
with trio.move_on_after(0.5):
async for c in proc.stderr:
print(c)
await proc.stdin.send_all(b'\n')
print("stdout:")
with trio.move_on_after(0.5):
async for c in proc.stdout:
print(c)
print("stderr:")
with trio.move_on_after(0.5):
async for c in proc.stderr:
print(c)
# kill the process
nursery.cancel_scope.cancel()
trio.run(main) output:
Generally this looks great though. |
Thanks for taking interest in completing this!
This is an effort/reward balance that wouldn't quite make the cut for me, but I won't try to stop you!
I thought the ioctl somehow uses a kernel api to send bytes through the process's attached terminal so i'm not surprised that it only works in some cases, but I also don't know exactly what those cases would be without trying. What happened to your |
Unfortunately that doesn't seem to help -- I've tried both |
Ok it seems this relies on having a PTY, which is something we can provide it. Maybe one day CPython will export pyrepl so we can do what asyncio does and take advantage of the fact they use a reimplemented readline... |
Turns out the reason my test code above wasn't working is somewhat trivial: So far I get:
The latter seems closer, but I'm not quite getting Linux's permission model 🤔 |
It's actually impossible to test this in CI... because it doesn't work in CI! Linux 6.2 introduced a You can check your own system with Please review this! EDIT: I really dislike how... not great the tests are. I think we should rely on vendored pyrepls given we support only CPython/PyPy, especially since the functionality we need is basically at a "rewrite readline" level. Though any changes should be on another PR. |
This means that we cannot test our usage of `TIOCSTI`. This ctrl+c support was dead on arrival!
I'll try to review this weekend, but just quickly wanted to ask if this means that eventually the ioctl will break on (essentially) all linux users? |
Only if the distro disables the ioctl, so maybe not all? But yeah we will need to brainstorm another solution... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the tests make sense to me overall, it's just unfortunate that there are still some holes in them. If it is just a best-effort holdover until a pyrepl based solution can be stitched together then I don't think that should be a blocker. It's still better than the present behavior for all users.
fcntl.ioctl(sys.stdin, termios.TIOCSTI, b"\n") | ||
# on a best-effort basis | ||
with contextlib.suppress(OSError): | ||
fcntl.ioctl(sys.stdin, termios.TIOCSTI, b"\n") # type: ignore[attr-defined, unused-ignore] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of simply suppressing OSError, maybe catch it and prompt the user to press enter to continue by printing text? We could also get the OSError code at that time which could be useful for understanding the situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully my change worked, I'm not on a Linux machine so I cannot check yet.
signal_sent = signal.CTRL_C_EVENT if sys.platform == "win32" else signal.SIGINT # type: ignore[attr-defined,unused-ignore] | ||
os.kill(proc.pid, signal_sent) | ||
if sys.platform == "win32": | ||
# we rely on EOFError which... doesn't happen with pipes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never dug in to why EOFError pops out, maybe that knowledge could help us test here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't dug into this yet unfortunately.
I want to do this but still haven't:
Sorry about the delay, I've been on vacation! |
This commit has a draft method to fix #3007. I have 0 ideas on how to test this thing: not only does it use the
raw_input
that gets patched for testing, but the way it injects a newline is basically impossible for a test runner to pass in. Also, it feels fragile somehow to dip in and out of the trio loop patching the signal handler and peeking forKeyboardInterrupt
.