Repository navigation
fix(sdk): kill a timed-out sync hook's whole process group - #6614
Conversation
subprocess.run's timeout only kills the shell it started, so commands the hook launched kept running after "Hook timed out". Run the hook in its own session and, on timeout, stop the group with the same _terminate_process the async hooks already use.
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
This comment has been minimized.
This comment has been minimized.
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope: In-repo change to openhands-sdk/openhands/sdk/hooks/executor.py (SDK core) fixing the pinned item in #6348 ("a timed-out sync hook only kills the shell; its child processes keep running"). The linked issue is open and labeled ready-for-dev; no maintainer decision is needed.
Mechanism review: On POSIX, sync hooks now run under Popen(start_new_session=True), so on TimeoutExpired the existing AsyncProcessManager._terminate_process reaches the whole process group (killpg SIGTERM → SIGKILL) instead of only the shell. The Windows path keeps start_new_session=False and uses the same taskkill /F /T branch the async path already uses. The exception is re-raised so the existing TimeoutExpired branch still returns the identical "Hook timed out after N seconds" result, and the CompletedProcess wrapper preserves the stdout/stderr/JSON parsing contract. The with block closes the pipes and reaps the process on all paths, so there is no fd or zombie leak. Running sync hooks in their own session mirrors the established async-hook behavior and the documented Python idiom for timeout-killable subprocesses; no public API, serialization, or persisted-shape change is involved.
Verification in this workspace (head b16dcb48):
uv run pytest tests/sdk/hooks→ 153 passed, including the newtest_timed_out_hook_does_not_leave_its_child_running.- The pinned test run against
main's executor in a throwaway worktree fails (assert not child_alive— the hook'ssleep 30child survives the timeout), confirming the original symptom on the base revision and that this head fixes it through the real executor path, not mocked state. - The failing
sdk-testscheck on this head istest_failing_mcp_server_keeps_the_other_servers_tools[invalid-entry](pydanticValidationErrorin MCP config parsing). I reproduced that exact failure identically onmainlocally; it is pre-existing and unrelated to this PR, which touches no MCP or conversation code.windows-tests,cross-tests, and the Windows/macOS binary builds passed on this head.
No material correctness, security, compatibility, or acceptance-criterion defects found.
✅ APPROVED
HUMAN:
just fixing a bug
AGENT:
Why
Command hooks run through
shell=True. For a synchronous hook,subprocess.run(..., timeout=...)only kills the shell process when the timeout fires, so anything the hook started (a script,sleep, a server) keeps running in the background after the hook reports "timed out". Async hooks don't have this problem: they start in a new session andAsyncProcessManager._terminate_processkills the whole process group (or usestaskkill /Ton Windows).Summary
Popen(..., start_new_session=True)(POSIX) andcommunicate(timeout=...). On timeout, stop them with the existing_terminate_process, then re-raise so the existingTimeoutExpiredbranch returns the same "Hook timed out" result.CompletedProcess, so stdout/stderr/JSON parsing reads the same fields as before.xfailmarker.Issue Number
Part of #6348 (item: "a timed-out sync hook only kills the shell; its child processes keep running (the async path kills the process group)").
How to Test
test_timed_out_hook_does_not_leave_its_child_runningfails onmain(the hook'ssleep 30child is still alive after the timeout) and passes here;tests/sdk/hookspasses (153).Type
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:6ba0544-pythonRun
All tags pushed for this build
About Multi-Architecture Support
6ba0544-python) is a multi-arch manifest supporting both amd64 and arm646ba0544-python-amd64) are also available if needed