-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix input thread busy-looping at 100% CPU when stdin reaches EOF #6690
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
Open
m-k-l-s
wants to merge
3
commits into
Textualize:main
Choose a base branch
from
m-k-l-s:eof-busy-loop-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−4
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,73 @@ | ||
| """Regression test for the input thread when stdin reaches end-of-file. | ||
|
|
||
| A selector reports a file descriptor at EOF as readable, so `select()` returns | ||
| immediately, forever. Unless the input thread notices the EOF and stops | ||
| selecting, it busy-loops and pegs a CPU core for the life of the app. | ||
|
|
||
| Asserting the thread returns is a deterministic stand-in for measuring CPU: a | ||
| thread that has returned cannot busy-loop. | ||
| """ | ||
|
|
||
| import os | ||
| import signal | ||
| import sys | ||
| import threading | ||
| from typing import Iterator | ||
|
|
||
| import pytest | ||
|
|
||
| from textual.app import App | ||
| from textual.driver import Driver | ||
|
|
||
| if sys.platform == "win32": | ||
| pytest.skip("LinuxDriver/LinuxInlineDriver are POSIX only", allow_module_level=True) | ||
|
|
||
| from textual.drivers.linux_driver import LinuxDriver | ||
| from textual.drivers.linux_inline_driver import LinuxInlineDriver | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def eof_fileno() -> Iterator[int]: | ||
| """A file descriptor permanently at EOF, like stdin whose terminal is gone.""" | ||
| read_fd, write_fd = os.pipe() | ||
| os.close(write_fd) | ||
| try: | ||
| yield read_fd | ||
| finally: | ||
| os.close(read_fd) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def preserve_signal_handlers() -> Iterator[None]: | ||
| """Restore the handlers that `LinuxDriver.__init__` installs.""" | ||
| saved = { | ||
| number: signal.getsignal(number) for number in (signal.SIGTSTP, signal.SIGCONT) | ||
| } | ||
| try: | ||
| yield | ||
| finally: | ||
| for number, handler in saved.items(): | ||
| if handler is not None: | ||
| signal.signal(number, handler) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("driver_class", [LinuxDriver, LinuxInlineDriver]) | ||
| async def test_input_thread_stops_on_stdin_eof( | ||
| driver_class: type[Driver], | ||
| eof_fileno: int, | ||
| preserve_signal_handlers: None, | ||
| ) -> None: | ||
| async with App().run_test() as pilot: | ||
| driver = driver_class(pilot.app) | ||
| driver.fileno = eof_fileno | ||
|
|
||
| thread = threading.Thread(target=driver.run_input_thread, daemon=True) | ||
| thread.start() | ||
| thread.join(timeout=5.0) | ||
| spinning = thread.is_alive() | ||
| if spinning: | ||
| # Stop the thread before the fixture closes the descriptor under it. | ||
| driver.exit_event.set() | ||
| thread.join(timeout=5.0) | ||
|
|
||
| assert not spinning, "input thread is busy-looping on a stdin that is at EOF" |
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.