-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-130168: pyrepl can messup and poll is not thread safe #138617
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
Closed
+50
−8
Closed
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0609120
fix: pyrepl can messup and poll is not thread safe
yihong0618 ffc77f2
fix: mypy type check
yihong0618 8fa9c19
fix: apply suggestions from code review
yihong0618 78d7416
fix: __svtermstate default None
yihong0618 ca27ae4
fix: drop cleanup part for new test
yihong0618 b194f8f
fix: forget to add clean up back
yihong0618 e43fc60
Merge branch 'main' into hy/close_new_repl_error_mess
yihong0618 5d83648
fix: tests failed by wrong conflict fix
yihong0618 b3a41eb
Merge branch 'main' into hy/close_new_repl_error_mess
yihong0618 17877b6
fix: merge regress
yihong0618 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import signal | ||
| import struct | ||
| import termios | ||
| import threading | ||
| import time | ||
| import types | ||
| import platform | ||
|
|
@@ -157,6 +158,8 @@ def __init__( | |
|
|
||
| self.pollob = poll() | ||
| self.pollob.register(self.input_fd, select.POLLIN) | ||
| self._poll_lock = threading.RLock() | ||
| self._polling_thread: threading.Thread | None = None | ||
| self.terminfo = terminfo.TermInfo(term or None) | ||
| self.term = term | ||
|
|
||
|
|
@@ -325,8 +328,11 @@ def prepare(self): | |
| """ | ||
| Prepare the console for input/output operations. | ||
| """ | ||
| self.__svtermstate = tcgetattr(self.input_fd) | ||
| raw = self.__svtermstate.copy() | ||
| # gh-130168: prevents signal handlers from overwriting the original state | ||
| if not hasattr(self, '_UnixConsole__svtermstate'): | ||
| self.__svtermstate = tcgetattr(self.input_fd) | ||
|
|
||
| raw = tcgetattr(self.input_fd).copy() | ||
|
yihong0618 marked this conversation as resolved.
|
||
| raw.iflag &= ~(termios.INPCK | termios.ISTRIP | termios.IXON) | ||
| raw.oflag &= ~(termios.OPOST) | ||
| raw.cflag &= ~(termios.CSIZE | termios.PARENB) | ||
|
|
@@ -368,7 +374,11 @@ def restore(self): | |
| self.__disable_bracketed_paste() | ||
| self.__maybe_write_code(self._rmkx) | ||
| self.flushoutput() | ||
| tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate) | ||
|
|
||
| if hasattr(self, '_UnixConsole__svtermstate'): | ||
| tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate) | ||
| # Clear the saved state so prepare() can save a fresh one next time | ||
| del self.__svtermstate | ||
|
yihong0618 marked this conversation as resolved.
Outdated
|
||
|
|
||
| if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal": | ||
| os.write(self.output_fd, b"\033[?7h") | ||
|
|
@@ -417,10 +427,25 @@ def wait(self, timeout: float | None = None) -> bool: | |
| """ | ||
| Wait for events on the console. | ||
| """ | ||
| return ( | ||
| not self.event_queue.empty() | ||
| or bool(self.pollob.poll(timeout)) | ||
| ) | ||
| if not self.event_queue.empty(): | ||
| return True | ||
|
|
||
| current_thread = threading.current_thread() | ||
|
|
||
| if self._polling_thread is current_thread: | ||
| # This is a re-entrant call from the same thread | ||
| # like old repl runtime error | ||
| raise RuntimeError("can't re-enter readline") | ||
|
Comment on lines
+456
to
+457
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, maybe we can change the message. I don't have a suggestion now but I'd like REPL maintainers to first check this PR. |
||
|
|
||
| if not self._poll_lock.acquire(blocking=False): | ||
| return False | ||
|
|
||
| try: | ||
| self._polling_thread = current_thread | ||
| return bool(self.pollob.poll(timeout)) | ||
| finally: | ||
| self._polling_thread = None | ||
| self._poll_lock.release() | ||
|
yihong0618 marked this conversation as resolved.
|
||
|
|
||
| def set_cursor_vis(self, visible): | ||
| """ | ||
|
|
@@ -786,7 +811,10 @@ def __tputs(self, fmt, prog=delayprog): | |
| # using .get() means that things will blow up | ||
| # only if the bps is actually needed (which I'm | ||
| # betting is pretty unlkely) | ||
| bps = ratedict.get(self.__svtermstate.ospeed) | ||
| if hasattr(self, '_UnixConsole__svtermstate'): | ||
|
yihong0618 marked this conversation as resolved.
Outdated
|
||
| bps = ratedict.get(self.__svtermstate.ospeed) | ||
| else: | ||
| bps = None | ||
| while True: | ||
| m = prog.search(fmt) | ||
| if not m: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-09-07-19-47-35.gh-issue-130168.qvtlOZ.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix: pyrepl messed up terminal if a signal handler expects stdin. | ||
|
yihong0618 marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.