Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions openhands-sdk/openhands/sdk/hooks/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,16 +552,28 @@ def execute(
)

try:
# Execute the hook command synchronously
result = subprocess.run(
# Execute the hook command synchronously, in its own session so a
# timeout stops the commands the shell started, not just the shell.
with subprocess.Popen(
command,
shell=True,
cwd=self.working_dir,
env=hook_env,
input=event_json,
capture_output=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=hook.timeout,
start_new_session=os.name != "nt",
) as process:
try:
stdout, stderr = process.communicate(
event_json, timeout=hook.timeout
)
except subprocess.TimeoutExpired:
self.async_process_manager._terminate_process(process)
raise
result = subprocess.CompletedProcess(
command, process.returncode, stdout, stderr
)

# Parse the result
Expand Down
1 change: 0 additions & 1 deletion tests/sdk/conversation/test_local_conversation_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ def _failed_servers(caplog: pytest.LogCaptureFixture) -> list[str]:
"broken",
[
pytest.param(UNRESPONSIVE_SERVER, id="never-answers"),
pytest.param({}, id="invalid-entry"),
pytest.param({"command": "missing-mcp-server-command"}, id="missing-command"),
],
)
Expand Down
29 changes: 29 additions & 0 deletions tests/sdk/hooks/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Tests for hook executor."""

import contextlib
import json
import os
import signal
import subprocess
from unittest import mock
from unittest.mock import MagicMock, patch
Expand Down Expand Up @@ -193,6 +196,32 @@ def test_execute_captures_stderr(self, executor, sample_event):
assert result.blocked
assert "error message" in result.stderr

@pytest.mark.skipif(os.name == "nt", reason="POSIX process semantics")
def test_timed_out_hook_does_not_leave_its_child_running(
self, executor, sample_event, tmp_path
):
pid_file = tmp_path / "child.pid"
# The trailing ``; true`` stops the shell from exec-ing the child, so
# the child is a separate process the timeout has to reach.
hook = HookDefinition(
command=f"sh -c 'echo $$ > {pid_file}; exec sleep 30'; true",
timeout=1,
)

result = executor.execute(hook, sample_event)
assert "timed out" in (result.error or "")

child_pid = int(pid_file.read_text())
try:
os.kill(child_pid, 0)
child_alive = True
except ProcessLookupError:
child_alive = False
finally:
with contextlib.suppress(ProcessLookupError):
os.kill(child_pid, signal.SIGKILL)
assert not child_alive


class TestAsyncHookExecution:
"""Tests for async hook execution."""
Expand Down
Loading