Repository navigation
fix(agent-server): wall-clock timeout on chat-mode subprocess (#313) - #436
Merged
Merged
Conversation
`execute_claude_code` called `process.wait()` with no timeout and ran the subprocess executor without `asyncio.wait_for`. If the Claude CLI stalled on a billing error, infinite stream, or a hook grandchild wedge that `--max-turns` couldn't catch, chat sessions hung indefinitely until the container was killed externally. This is the GUARD-003 gap that was explicitly deferred from Phase 1. Fix mirrors the existing task-mode pattern in `execute_headless_task`: - Read `execution_timeout_sec` from the guardrails runtime config with a 30-minute default (`_DEFAULT_EXECUTION_TIMEOUT_SEC = 1800`). - Inner `process.wait(timeout=timeout_seconds)` bounds the executor thread — on `TimeoutExpired` we terminate the process group (graceful→SIGKILL), drain reader threads, and re-raise. - Outer `asyncio.wait_for(..., timeout=timeout_seconds + 60)` is the async safety net with a 60-s grace buffer for drain/cleanup. - Both timeout paths raise `HTTPException(504)`; `registry.unregister` still fires via the existing `finally` block. Tests: AST-level regression guards on the constant, guardrails read, inner/outer wait pattern, and 504 raise count, plus a behavioural smoke test that `subprocess.Popen.wait(timeout=X)` raises `TimeoutExpired` on this platform (guards against a primitive-level regression). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
vybe
approved these changes
Apr 21, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
Validated via /validate-pr: mirrors task-mode wall-clock timeout pattern. ✅
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.
Summary
Closes #313. GUARD-003 follow-up — chat mode now has the wall-clock timeout that task mode already had.
Root cause
execute_claude_codeindocker/base-image/agent_server/services/claude_code.pycalled:If the Claude CLI subprocess stalls — billing error, stuck stream, hook grandchild wedge that
--max-turnsdoesn't catch — the chat session hangs until the container is killed externally. Task mode (execute_headless_task) correctly usesasyncio.wait_for+process.kill(); chat mode silently lacked the parity.Fix
Mirror task mode's two-layer pattern:
_DEFAULT_EXECUTION_TIMEOUT_SEC = 1800(30 min).timeout_seconds = int(guardrails.get("execution_timeout_sec") or _DEFAULT_EXECUTION_TIMEOUT_SEC). Field already exists in the shared guardrails schema (agent_config.py:497, range 60–7200).process.wait(timeout=timeout_seconds). Onsubprocess.TimeoutExpired:_terminate_process_group(graceful_timeout=5)→ drain readers → re-raise.asyncio.wait_for(..., timeout=timeout_seconds + 60). Onasyncio.TimeoutError:_terminate_process_group(graceful_timeout=2)+_safe_close_pipes. Both timeout paths raiseHTTPException(504).registry.unregister(execution_id)still runs via the existingfinallyblock.Test plan
tests/unit/test_chat_wallclock_timeout.py— 6/6 pass in 0.32s.process.wait(timeout=…)+TimeoutExpiredhandling, outerasyncio.wait_for+asyncio.TimeoutErrorhandling, ≥2 HTTP 504 raises.subprocess.Popen.wait(timeout=0.3)raisesTimeoutExpiredon this platform (guards against a primitive-level regression).Scope
docker/base-image/agent_server/services/claude_code.py— 49+/5-.tests/unit/test_chat_wallclock_timeout.py— new, 159 lines.No router, DB schema, or external API change. No behaviour change when the subprocess completes before the timeout.
Notes
The unit tests are source-AST regression guards rather than runtime tests of the full
execute_claude_code. Reason: the module has relative imports (from ..models,from ..state) that require the fullagent_serverpackage to be importable. The helpers the fix composes (_terminate_process_group,_drain_reader_threads,_safe_close_pipes) are already behaviourally covered bytests/unit/test_subprocess_pgroup.py, and the pattern mirrors the production-proven task-mode code. Behavioural regression of the full chat-mode timeout path would require the agent-container integration harness.🤖 Generated with Claude Code