Skip to content

Fix Ctrl-C handling in REPL #3306

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

geofft
Copy link

@geofft geofft commented Jul 27, 2025

Python's readline module is unable to handle signals when it's called not on the main thread, and it's blocking so we can't call it from the same thread as Trio. Get rid of the additional thread and handle input ourselves.

geofft and others added 2 commits July 27, 2025 02:10
Python's readline module is unable to handle signals when it's called
not on the main thread, and it's blocking so we can't call it from the
same thread as Trio. Get rid of the additional thread and handle input
ourselves.
@geofft
Copy link
Author

geofft commented Jul 27, 2025

This is extremely rough, probably misbehaves on Windows, and is probably incomplete in some interesting way. I think it also misbehaves with Python 3.14. All I can say is it mostly works on my machine (Ubuntu 24.04, system Python). I'm mostly posting here for thoughts on the general approach of calling readline via ctypes + inspiration if someone else wants to run with this and finish it.

Copy link

codecov bot commented Jul 27, 2025

Codecov Report

❌ Patch coverage is 77.10843% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.75714%. Comparing base (e590d39) to head (0e25830).

Files with missing lines Patch % Lines
src/trio/_repl.py 77.10843% 16 Missing and 3 partials ⚠️
Additional details and impacted files
@@                 Coverage Diff                  @@
##                 main       #3306         +/-   ##
====================================================
- Coverage   100.00000%   99.75714%   -0.24286%     
====================================================
  Files             127         127                 
  Lines           19274       19353         +79     
  Branches         1304        1314         +10     
====================================================
+ Hits            19274       19306         +32     
- Misses              0          41         +41     
- Partials            0           6          +6     
Files with missing lines Coverage Δ
src/trio/_repl.py 77.86885% <77.10843%> (-22.13116%) ⬇️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@geofft geofft mentioned this pull request Jul 27, 2025
Copy link
Contributor

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

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

IMO this is too much poking at readline internals (the original had too much poking into OS internals).

I think the much better solution is to use that pyrepl reimplemented readline (at least then we're not using ctypes!!), so we can support CPython 3.12+ and (at the very least) PyPy 3.10+. In fact we should probably just error on earlier versions for the REPL since supporting them is very hard... I'll leave this open until we have something working for that though.

self.rl.rl_callback_handler_install(prompt.encode(), callback)
while line == b"":
await trio.lowlevel.wait_readable(0)
self.rl.rl_callback_read_char()
Copy link
Contributor

@A5rocks A5rocks Aug 12, 2025

Choose a reason for hiding this comment

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

So just to understand I'm reading this correctly, this rl_callback_read_char would fail a ctrl+c check, except it's guaranteed to have a character as stdin is readable?

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

Successfully merging this pull request may close these issues.

2 participants