Skip to content

Fix: timed-out agent runs crash instead of reporting status="timeout" on Windows - #8

Merged
vsn411 merged 1 commit into
secfoo-com:mainfrom
karthickmk2001:fix/agent-timeout-kill-windows
Sep 18, 2026
Merged

vsn411 merged 1 commit into
secfoo-com:mainfrom
karthickmk2001:fix/agent-timeout-kill-windows

Conversation

@karthickmk2001

Copy link
Copy Markdown
Collaborator

What does this change, and why?

AgentAdapter.run() kills a hung agent's whole process tree on timeout via os.killpg(os.getpgid(pid), signal.SIGKILL) -- deliberately not just proc.kill(), per the existing comment, because that only kills the direct child, not any grandchildren (needed to reliably contain Cursor's documented agent -p hang bug).

os.killpg/os.getpgid don't exist outside POSIX. On Windows, hitting this path doesn't raise AttributeError in a vacuum -- it propagates straight out of run(), so a timed-out run never produces a status="timeout" result at all; the whole execute_runs() call blows up instead. Found this while testing secfoo locally on Windows: the existing regression test for this exact behavior (test_timeout_status_does_not_raise in tests/test_agents/test_cursor.py) already asserts a hang resolves to status="timeout" rather than raising -- it just can't catch this because CI only runs on ubuntu-latest, where os.killpg exists and the bug never fires.

The fix

Moved the kill logic into a small _kill_process_tree(pid) helper in agents/base.py:

  • POSIX: unchanged, os.killpg(os.getpgid(pid), signal.SIGKILL).
  • Everywhere else: taskkill /F /T /PID <pid> -- /T kills the whole process tree, which is the actual Windows equivalent of what killpg was doing (not just proc.kill(), which would reintroduce the direct-child-only limitation the original code was written to avoid).

Both branches still swallow "already gone" races (ProcessLookupError / OSError) exactly as before.

Tests

Added 4 unit tests covering both branches of _kill_process_tree directly (happy path + already-exited race, for POSIX and Windows each). They take an explicit posix: bool parameter for this rather than monkeypatching os.name, since forcing os.name to "posix" mid-test on a real Windows box crashed pytest's own traceback formatting (it also reads os.name to decide which pathlib.Path flavor to instantiate).

pytest tests passes locally, including the existing test_timeout_status_does_not_raise (verified its POSIX-branch behavior is unchanged by manually forcing posix=True through the full AgentAdapter.run() path -- see PR discussion if useful, happy to fold that in as an explicit test if preferred).

ruff check and bandit -c .bandit -r src/secfoo/agents/base.py are both clean on the changed file.

AgentAdapter.run() kills a hung agent's whole process tree via
os.killpg(os.getpgid(pid), ...) when a run times out -- needed because
subprocess.run's own timeout handling only kills the direct child, not
any grandchildren (e.g. Cursor's documented `agent -p` hang bug).

os.killpg/os.getpgid don't exist outside POSIX, so on Windows this
raised AttributeError instead of killing anything: a timed-out run
never produced a "timeout" result, it just blew up.

Moved the kill logic into _kill_process_tree(), which uses
`taskkill /F /T /PID` (kill process tree) on non-POSIX platforms
instead, and added unit tests for both branches.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@karthickmk2001 karthickmk2001 changed the title fix: don't crash killing a timed-out agent process outside POSIX Fix: timed-out agent runs crash instead of reporting status="timeout" on Windows Sep 17, 2026
@vsn411
vsn411 merged commit 75e6d75 into secfoo-com:main Sep 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants