Repository navigation
Description
Context
#728 bounded the safe_close_pipes TextIOWrapper-lock deadlock with a 90s _DRAIN_BUDGET_SECONDS (docker/base-image/agent_server/services/subprocess_lifecycle.py, _drain_bounded). On budget-exceeded it returns budget_exceeded and leaks the reader thread to unwedge the task/executor. That fixed the up-to-7200s hang.
Problem (residual of #728)
On budget_exceeded, the leaked reader thread (and/or an un-reaped claude grandchild still holding the pipe) then spins at ~100% CPU with no visible claude child process, and stays there until the agent container is stopped/restarted. The 90s cap recovers the task/server but not the CPU — a single agent slot burns a full core with no automatic recovery.
The #728 fix explicitly accepted leaking the thread ("Leaked reader threads are daemon threads and die with the container") — but "dies with the container" means the only recovery is a manual restart, and in the meantime the host CPU is pegged.
Repro (observed on the Client Portal, but trigger-agnostic)
Dispatch a turn whose claude subprocess emits a large/blocking stream (e.g. the agent reads an ~83 KB binary file through claude --print --output-format stream-json). Then:
- agent-server log:
WARNING [Subprocess] Drain budget (90s) exceeded — safe_close_pipes may have deadlocked with reader thread's TextIOWrapper lock; reader threads are leaked daemon threads (pid=N). Issue #728. docker stats→ the agent container at ~100% CPU- container
ps -eo pid,pcpu,comm→ onlyagent-server.py(noclaude/nodechild visible) POST /api/agents/{name}/executions/{id}/terminate→ 504 (agent-server can't be reached / is busy)- only
docker stop <agent>clears it
Proposed fix
On budget_exceeded, actively tear down instead of leaking:
- SIGKILL the claude process group (
utils/subprocess_pgroup.pypgid) so any grandchild holding the pipe dies and the pipe EOFs, letting the reader thread unblock/exit; - only then leak as a last resort.
This frees the core without a container restart. Distinct from #728, which only unblocks the executor thread.
Severity
P1 — a single agent can hold a full CPU core indefinitely with no automatic recovery; terminate and the cleanup/CB recovery paths can't reach the wedged agent-server.
Related
- bug: agent-server.py spins at 90% CPU on OAuth token auth failure, blocking CB recovery #728 (closed) — the drain-deadlock 90s-budget mitigation this is the residual of
- bug: drain_reader_threads t.join(30) deadlocks asyncio event loop permanently when reader thread is stuck #657 — the original drain-deadlock on the normal-exit path
- Research and implement orchestration invariant catalog + continuous canary harness #411 — R-01 canary (zombie-claude) guards a related class
- Trigger-side mitigation already shipped for the Client Portal (images sent as vision instead of read as text, so the portal no longer emits the large-binary stream that trips this) — trinity-enterprise#104.
Activity
fix(agent-server): kill the process group on a budget-exceeded drain (#…
fix(sanitizer): linear KEY=value redaction — the #1661 CPU spin is th…
fix(agent): record WHERE a wedged reader thread is, instead of guessi…