Repository navigation
Description
Summary
After a Claude Code subprocess exits, _kill_orphan_pipe_writers occasionally blocks for ~15 minutes because its /proc scan stalls on a D-state (uninterruptible sleep) kernel process. During this window, agent health probes time out and the circuit breaker opens, causing all scheduled executions to fail until the CB is manually reset or times out. PRs #730 and #747 (the two #728 fixes) are deployed but do not eliminate the D-state blocking — they cap the CPU spin and make the scan D-state-safer, but the block still occurs.
Component
Agent Runtime — subprocess_pgroup / subprocess_lifecycle
Priority
P1 — scheduled executions fail for the duration of the CB open window (observed 4+ hour outages)
Error
WARNING:agent_server.utils.subprocess_pgroup:[Subprocess] Reader thread(s) still busy after process exit (pid=XXXX, stuck_count=1) — killing process group, then waiting 30s for natural drain
WARNING:agent_server.services.subprocess_lifecycle:[Subprocess] Drain budget (90s) exceeded — safe_close_pipes may have deadlocked with reader thread's TextIOWrapper lock; reader threads are leaked daemon threads. Issue #728.
WARNING:agent_server.utils.subprocess_pgroup:[Subprocess] _kill_orphan_pipe_writers still running after 10s (pid=XXXX) — /proc scan may be blocked on a D-state process
ERROR:agent_server.utils.subprocess_pgroup:[Subprocess] Reader thread(s) still stuck after 917.6s post-kill grace — force-closing pipes; some buffered data may be lost (pid=XXXX, stuck_count=1)
Location
- File:
src/agent_server/utils/subprocess_pgroup.py(_kill_orphan_pipe_writers) - File:
src/agent_server/services/subprocess_lifecycle.py(safe_close_pipes)
Root Cause
The sequence:
- Claude Code subprocess exits; reader thread is stuck holding the
TextIOWrapperlock on the stdout/stderr pipe safe_close_pipesdeadlocks waiting for the reader thread — drain budget (90s cap from PR fix(agent-server): cap drain executor thread at 90 s to fix CPU spin on auth failure (#728) #730) expires_kill_orphan_pipe_writersis invoked to kill processes holding the pipe open via/procscan- The
/procscan itself stalls because one of the scanned PIDs is in D-state (uninterruptible I/O sleep) — the os.readlink improvement from PR fix(subprocess): replace os.stat() with os.readlink() in orphan pipe scan (#728) #747 reduces the chance but does not eliminate it _kill_orphan_pipe_writershangs for ~15 minutes until the D-state process eventually unblocks or the grace timer force-closes- During steps 3–5, the agent's HTTP interface is degraded (event loop partially blocked) → health probes fail → circuit breaker opens
The 917-second stuck duration (observed in production) is the grace period before pipes are force-closed. The CB opens well before that and does not auto-recover once the CB goes dormant (~40 min of failed probes).
Reproduction Steps
- Deploy an agent with a scheduled task that spawns a subprocess (e.g. a Claude Code execution that runs a tool with a long-lived child process)
- Allow the scheduled execution to complete normally
- Observe
_kill_orphan_pipe_writerslog warning in agent container logs shortly after execution ends - Monitor circuit breaker state — CB opens within minutes of the warning
- CB remains open until manually reset or backend restart
Note: Trigger is non-deterministic; occurs when a subprocess cleanup coincides with a D-state kernel process in the container's PID namespace.
Suggested Fix
Two complementary approaches:
1. Time-box _kill_orphan_pipe_writers unconditionally — the 10s warning exists but the scan itself has no hard kill. Wrap the entire /proc iteration in a concurrent.futures.ThreadPoolExecutor with a strict timeout so a stalled scan cannot block the event loop indefinitely regardless of D-state.
2. Move pipe cleanup entirely off the event loop — run safe_close_pipes and _kill_orphan_pipe_writers in a dedicated daemon thread with a hard wall-clock deadline, so a stuck cleanup cannot degrade health probe responsiveness.
Environment
- Trinity version:
3a3e9a7(dev branch, 2026-05-11) - Agent runtime: Claude Code subprocess via
subprocess_pgroup - OS: Ubuntu 22.04 LTS / Linux kernel (container)
Related
- Issue bug: agent-server.py spins at 90% CPU on OAuth token auth failure, blocking CB recovery #728 (original tracking issue — partially fixed by PR fix(agent-server): cap drain executor thread at 90 s to fix CPU spin on auth failure (#728) #730 + PR fix(subprocess): replace os.stat() with os.readlink() in orphan pipe scan (#728) #747)
- PR fix(agent-server): cap drain executor thread at 90 s to fix CPU spin on auth failure (#728) #730:
cap drain executor thread at 90s— fixes CPU spin, not D-state blocking - PR fix(subprocess): replace os.stat() with os.readlink() in orphan pipe scan (#728) #747:
replace os.stat() with os.readlink() in orphan pipe scan— reduces D-state risk, does not eliminate - Circuit breaker dormant state (bug: circuit breaker flood causes SQLite DB locks and UI unresponsiveness #631) means CB does not self-heal after ~40 min
Activity
vybe commentedon May 12, 2026
/claim
github-actions commentedon May 12, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/808-<short-slug>Fixes #808docs(feature-flows): sync parallel-headless-execution flow for #808
fix(orphan-killer): SCHED_IDLE + scan deadline to prevent health-prob…
Merge branch 'dev' into AndriiPasternak31/issue-586-plan-v1
test: remove unit tests for cleanup symbols deleted in #817 follow-up
fix(agent-runtime): cgroup-walk sweep catches env-stripped orphans (#817
docs(feature-flows): document #970 headless drain decoupling
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).