-
Notifications
You must be signed in to change notification settings - Fork 249
Fix Windows terminal Ctrl-C process cleanup #3171
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
+174
−3
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2082074
Fix Windows terminal Ctrl-C process cleanup
neubig ee62dc2
Remove unused Windows terminal flag initialization
openhands-agent 86c4990
test: clean up browser toolset reset executor
openhands-agent 0090c24
test: avoid browser toolset mock cleanup hang
openhands-agent 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Windows-specific terminal interrupt behavior tests.""" | ||
|
|
||
| import platform | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| from openhands.tools.terminal.definition import TerminalAction | ||
| from openhands.tools.terminal.terminal import create_terminal_session | ||
| from openhands.tools.terminal.terminal.terminal_session import TerminalCommandStatus | ||
|
|
||
|
|
||
| pytestmark = pytest.mark.skipif( | ||
| platform.system() != "Windows", | ||
| reason="Windows CTRL_BREAK/PowerShell process behavior only applies on Windows", | ||
| ) | ||
|
|
||
|
|
||
| def _powershell_process_exists(pid: int) -> bool: | ||
| result = subprocess.run( | ||
| [ | ||
| "powershell.exe", | ||
| "-NoLogo", | ||
| "-NoProfile", | ||
| "-Command", | ||
| ( | ||
| f"if (Get-Process -Id {pid} -ErrorAction SilentlyContinue) " | ||
| "{ exit 0 } else { exit 1 }" | ||
| ), | ||
| ], | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| check=False, | ||
| ) | ||
| return result.returncode == 0 | ||
|
|
||
|
|
||
| def _stop_powershell_process(pid: int) -> None: | ||
| subprocess.run( | ||
| [ | ||
| "powershell.exe", | ||
| "-NoLogo", | ||
| "-NoProfile", | ||
| "-Command", | ||
| f"Stop-Process -Id {pid} -Force -ErrorAction SilentlyContinue", | ||
| ], | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.DEVNULL, | ||
| check=False, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.timeout(20) | ||
| def test_windows_ctrl_c_interrupt_kills_child_process_tree(tmp_path) -> None: | ||
| """Ctrl-C after a timeout should stop the process that kept the command alive. | ||
|
|
||
| This captures the behavior promised by the timeout prompt. The current | ||
| PowerShell backend sends CTRL_BREAK to the persistent PowerShell process, but | ||
| does not ensure child processes launched by the command are terminated. | ||
| """ | ||
| pid_path = tmp_path / "child.pid" | ||
| script_path = tmp_path / "wait_on_child.ps1" | ||
| script_path.write_text( | ||
| "\n".join( | ||
| [ | ||
| f"$pidPath = '{pid_path.as_posix()}'", | ||
| "$child = Start-Process -FilePath powershell.exe " | ||
| "-ArgumentList '-NoLogo','-NoProfile','-Command'," | ||
| "'Start-Sleep -Seconds 120' -PassThru", | ||
| "Set-Content -LiteralPath $pidPath -Value $child.Id", | ||
| "Wait-Process -Id $child.Id", | ||
| ] | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| session = create_terminal_session( | ||
| work_dir=str(tmp_path), | ||
| terminal_type="powershell", | ||
| no_change_timeout_seconds=1, | ||
| ) | ||
| child_pid: int | None = None | ||
| child_was_still_running = False | ||
| try: | ||
| session.initialize() | ||
|
|
||
| obs = session.execute(TerminalAction(command=f"& '{script_path.as_posix()}'")) | ||
|
|
||
| assert obs.metadata.exit_code == -1 | ||
| assert session.prev_status == TerminalCommandStatus.NO_CHANGE_TIMEOUT | ||
| assert pid_path.exists() | ||
| child_pid = int(pid_path.read_text(encoding="utf-8").strip()) | ||
| assert _powershell_process_exists(child_pid) | ||
|
|
||
| session.execute(TerminalAction(command="C-c", is_input=True, timeout=3)) | ||
|
|
||
| child_was_still_running = _powershell_process_exists(child_pid) | ||
| finally: | ||
| if child_pid is not None: | ||
| _stop_powershell_process(child_pid) | ||
| session.close() | ||
|
|
||
| assert not child_was_still_running, ( | ||
| "Windows Ctrl-C reported through the terminal did not terminate the " | ||
| "child process that kept the timed-out command alive." | ||
| ) |
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.
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.
🟡 Suggestion:
Get-CimInstance Win32_Processenumerates all processes on the system. On machines with thousands of processes this could take 1-2+ seconds. Consider documenting expected latency in the method docstring, or adding a timeout check if this becomes a production issue.Not blocking - this only runs during interrupt/close, so the overhead is acceptable for fixing the cleanup bug.