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 4a5369c6d4a..e93ca2098d9 100644 --- a/src/google/adk/environment/_local_environment.py +++ b/src/google/adk/environment/_local_environment.py @@ -190,8 +190,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 = []