From f41eacc102cf365359fb607a8f8eff9e1e51e2cd Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:22:36 +0800 Subject: [PATCH] fix: keep timeouts from crashing on Windows when SIGKILL is missing The timeout escalation in `UnsafeLocalCodeExecutor._kill_execution` and `LocalEnvironment._kill_command` looked up `signal.SIGKILL` unconditionally. Windows has no SIGKILL, so a timed-out code execution or command raised `AttributeError: module 'signal' has no attribute 'SIGKILL'` instead of being killed and reported: the runaway process kept running and the agent saw the crash rather than the timeout result. Resolve the escalation signal with `getattr(signal, 'SIGKILL', signal.SIGTERM)`, the same fallback `dev_server.py` already uses; `_signal_group` no-ops on platforms without process groups, so the fallback value is never sent by it, and on POSIX nothing changes. Mark `test_kill_execution_signals_group_before_killing_it` as needing POSIX process groups: it asserts the group-signal order and references `signal.SIGKILL` itself, so it cannot run on Windows. CI only runs Linux, which is why the crash stayed invisible there. --- src/google/adk/code_executors/unsafe_local_code_executor.py | 5 +++-- src/google/adk/environment/_local_environment.py | 6 ++++-- .../code_executors/test_unsafe_local_code_executor.py | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/google/adk/code_executors/unsafe_local_code_executor.py b/src/google/adk/code_executors/unsafe_local_code_executor.py index c84c0fbe585..f3ecc0576a2 100644 --- a/src/google/adk/code_executors/unsafe_local_code_executor.py +++ b/src/google/adk/code_executors/unsafe_local_code_executor.py @@ -108,8 +108,9 @@ def _kill_execution(process: subprocess.Popen[str]) -> tuple[str, str]: # Escalate unconditionally: the execution process exiting says nothing about # a child of it that is ignoring SIGTERM, and such a child holds the output - # pipes open besides. - _signal_group(group, signal.SIGKILL) + # pipes open besides. `_signal_group` no-ops on the platforms that have no + # process groups, so the fallback signal is never sent by it. + _signal_group(group, getattr(signal, 'SIGKILL', signal.SIGTERM)) process.kill() try: return process.communicate(timeout=_TERMINATE_GRACE_SECONDS) diff --git a/src/google/adk/environment/_local_environment.py b/src/google/adk/environment/_local_environment.py index 827e301d51a..bd04c0e3a73 100644 --- a/src/google/adk/environment/_local_environment.py +++ b/src/google/adk/environment/_local_environment.py @@ -192,8 +192,10 @@ async def _kill_command( if not done: # Escalate unconditionally: the command exiting says nothing about a # child of it that is ignoring SIGTERM, and such a child holds the - # output pipes open besides. - _signal_group(group, signal.SIGKILL) + # output pipes open besides. `_signal_group` no-ops on the platforms + # that have no process groups, so the fallback signal is never sent by + # it. + _signal_group(group, getattr(signal, 'SIGKILL', signal.SIGTERM)) _kill(proc) done, _ = await asyncio.wait([drain], timeout=_TERMINATE_GRACE_SECONDS) diff --git a/tests/unittests/code_executors/test_unsafe_local_code_executor.py b/tests/unittests/code_executors/test_unsafe_local_code_executor.py index 891034fa954..d0e6cbf92fe 100644 --- a/tests/unittests/code_executors/test_unsafe_local_code_executor.py +++ b/tests/unittests/code_executors/test_unsafe_local_code_executor.py @@ -427,6 +427,9 @@ def test_execute_code_imports_resolve_from_the_agent_path( assert result.stdout == "imported\n" assert result.stderr == "" + @pytest.mark.skipif( + not hasattr(os, "killpg"), reason="needs POSIX process groups" + ) def test_kill_execution_signals_group_before_killing_it(self, monkeypatch): """The group gets SIGTERM and its grace period before SIGKILL.""" signalled = []