Repository navigation
Fix: timed-out agent runs crash instead of reporting status="timeout" on Windows - #8
Merged
vsn411 merged 1 commit intoSep 18, 2026
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this change, and why?
AgentAdapter.run()kills a hung agent's whole process tree on timeout viaos.killpg(os.getpgid(pid), signal.SIGKILL)-- deliberately not justproc.kill(), per the existing comment, because that only kills the direct child, not any grandchildren (needed to reliably contain Cursor's documentedagent -phang bug).os.killpg/os.getpgiddon't exist outside POSIX. On Windows, hitting this path doesn't raiseAttributeErrorin a vacuum -- it propagates straight out ofrun(), so a timed-out run never produces astatus="timeout"result at all; the wholeexecute_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_raiseintests/test_agents/test_cursor.py) already asserts a hang resolves tostatus="timeout"rather than raising -- it just can't catch this because CI only runs onubuntu-latest, whereos.killpgexists and the bug never fires.The fix
Moved the kill logic into a small
_kill_process_tree(pid)helper inagents/base.py:os.killpg(os.getpgid(pid), signal.SIGKILL).taskkill /F /T /PID <pid>--/Tkills the whole process tree, which is the actual Windows equivalent of what killpg was doing (not justproc.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_treedirectly (happy path + already-exited race, for POSIX and Windows each). They take an explicitposix: boolparameter for this rather than monkeypatchingos.name, since forcingos.nameto"posix"mid-test on a real Windows box crashed pytest's own traceback formatting (it also readsos.nameto decide whichpathlib.Pathflavor to instantiate).pytest testspasses locally, including the existingtest_timeout_status_does_not_raise(verified its POSIX-branch behavior is unchanged by manually forcingposix=Truethrough the fullAgentAdapter.run()path -- see PR discussion if useful, happy to fold that in as an explicit test if preferred).ruff checkandbandit -c .bandit -r src/secfoo/agents/base.pyare both clean on the changed file.