-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-135329: prevent infinite traceback loop on Ctrl-C for strace #138133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ede576f
fix: strace ./python will forever loop
yihong0618 9235522
fix: add news
yihong0618 5ff6b9b
fix: address comments
yihong0618 d37d318
fix: address comments and add tests
yihong0618 f33f4ad
fix: address comments
yihong0618 851476b
fix: add EIO erorr with strace test
yihong0618 9658e5f
Merge branch 'hy/fix_can_not_strace' of https://github.com/yihong0618…
yihong0618 d54f137
fix: address comments
yihong0618 f441dca
fix: raise EIO without strace
yihong0618 0fc6c4a
fix: import sort
yihong0618 ac14061
fix: use spawn_python
yihong0618 41e191e
Update Lib/_pyrepl/unix_console.py
yihong0618 e3ae9a2
fix: apply comments
yihong0618 f463849
fix: test on windows
yihong0618 1801806
Run the internal EIO test on any non-win32 plat
ambv d0f9659
Merge branch 'main' into pr-138133
ambv e4a9a67
Apply suggestions from code review
ambv 643f368
Treat ENXIO as success
ambv 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
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,94 @@ | ||
| import errno | ||
| import fcntl | ||
| import os | ||
| import pty | ||
| import signal | ||
| import sys | ||
| import termios | ||
|
|
||
|
|
||
| def handler(sig, f): | ||
| pass | ||
|
|
||
|
|
||
| def create_eio_condition(): | ||
| # SIGINT handler used to produce an EIO. | ||
| # See https://github.com/python/cpython/issues/135329. | ||
| try: | ||
| master_fd, slave_fd = pty.openpty() | ||
| child_pid = os.fork() | ||
| if child_pid == 0: | ||
| try: | ||
| os.setsid() | ||
| fcntl.ioctl(slave_fd, termios.TIOCSCTTY, 0) | ||
| child_process_group_id = os.getpgrp() | ||
| grandchild_pid = os.fork() | ||
| if grandchild_pid == 0: | ||
| os.setpgid(0, 0) # set process group for grandchild | ||
| os.dup2(slave_fd, 0) # redirect stdin | ||
| if slave_fd > 2: | ||
| os.close(slave_fd) | ||
| # Fork grandchild for terminal control manipulation | ||
| if os.fork() == 0: | ||
| sys.exit(0) # exit the child process that was just obtained | ||
| else: | ||
| try: | ||
| os.tcsetpgrp(0, child_process_group_id) | ||
| except OSError: | ||
| pass | ||
| sys.exit(0) | ||
| else: | ||
| # Back to child | ||
| try: | ||
| os.setpgid(grandchild_pid, grandchild_pid) | ||
| except ProcessLookupError: | ||
| pass | ||
| os.tcsetpgrp(slave_fd, grandchild_pid) | ||
| if slave_fd > 2: | ||
| os.close(slave_fd) | ||
| os.waitpid(grandchild_pid, 0) | ||
| # Manipulate terminal control to create EIO condition | ||
| os.tcsetpgrp(master_fd, child_process_group_id) | ||
| # Now try to read from master - this might cause EIO | ||
| try: | ||
| os.read(master_fd, 1) | ||
| except OSError as e: | ||
| if e.errno == errno.EIO: | ||
| print(f"Setup created EIO condition: {e}", file=sys.stderr) | ||
| sys.exit(0) | ||
| except Exception as setup_e: | ||
| print(f"Setup error: {setup_e}", file=sys.stderr) | ||
| sys.exit(1) | ||
| else: | ||
| # Parent process | ||
| os.close(slave_fd) | ||
| os.waitpid(child_pid, 0) | ||
| # Now replace stdin with master_fd and try to read | ||
| os.dup2(master_fd, 0) | ||
| os.close(master_fd) | ||
| # This should now trigger EIO | ||
| print(f"Unexpectedly got input: {input()!r}", file=sys.stderr) | ||
| sys.exit(0) | ||
| except OSError as e: | ||
| if e.errno == errno.EIO: | ||
| print(f"Got EIO: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
| elif e.errno == errno.ENXIO: | ||
| print(f"Got ENXIO (no such device): {e}", file=sys.stderr) | ||
| sys.exit(1) # Treat ENXIO as success too | ||
| else: | ||
| print(f"Got other OSError: errno={e.errno} {e}", file=sys.stderr) | ||
| sys.exit(2) | ||
| except EOFError as e: | ||
| print(f"Got EOFError: {e}", file=sys.stderr) | ||
| sys.exit(3) | ||
| except Exception as e: | ||
| print(f"Got unexpected error: {type(e).__name__}: {e}", file=sys.stderr) | ||
| sys.exit(4) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Set up signal handler for coordination | ||
| signal.signal(signal.SIGUSR1, lambda *a: create_eio_condition()) | ||
| print("READY", flush=True) | ||
| signal.pause() | ||
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-08-25-18-06-04.gh-issue-138133.Zh9rGo.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 @@ | ||
| Prevent infinite traceback loop when sending CTRL^C to Python through ``strace``. |
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.