Repository navigation
Description
Summary
When an agent has a Stop hook that calls git push (which spawns ssh), the ssh process calls setsid() placing itself in a new process group. After Claude exits, the drain logic kills the PGID but ssh survives and holds the stdout pipe write-end for 10-30 seconds during network I/O. After 30s the drain force-closes the read-end, discarding buffered data including the final result JSON. The execution records 502 "no result message" even though the agent completed its work (can have 14-38 turns of real output).
Component
Agent Runtime — docker/base-image/agent_server/utils/subprocess_pgroup.py + docker/base-image/agent_server/services/claude_code.py
Priority
P1 — affects any agent with a git-push Stop hook; completed work is lost and reported as failure
Error
WARNING: [Subprocess] Reader thread(s) still busy after process exit (pid=XXXX, stuck_count=1) — killing process group, then waiting 30s for natural drain
ERROR: [Subprocess] Reader thread(s) still stuck after 30s post-kill grace — force-closing pipes; some buffered data may be lost
ERROR: [Headless Task] Error reading stdout: I/O operation on closed file.
ERROR: [Headless Task] Execution completed without a result message after 0 tool calls / 38 turns (raw_messages=62).
Location
- File:
docker/base-image/agent_server/utils/subprocess_pgroup.py—drain_reader_threads()/safe_close_pipes() - File:
docker/base-image/agent_server/services/claude_code.py—read_stdout()inexecute_headless_task
Root Cause
- Claude Code exits → agent-server starts drain with 5s grace period
- stdout reader thread still alive (pipe write-end still open)
killpg(pgid, SIGKILL)kills Claude process groupssh(spawned bygit pushinside Stop hook) has calledsetsid()— new session/PGID — survives the kill- 30s post-kill grace: ssh is doing network I/O (remote push), takes 10-30s
- After 30s:
safe_close_pipes()closes the read-end of the stdout pipe readline()in reader thread raisesValueError: I/O operation on closed file- Reader thread exits; OS pipe buffer containing result JSON is discarded
Why is the write-end still at the pipe? A well-written async hook redirects fd 1 before spawning external processes (exec >/dev/null 2>&1). But if that redirect fails silently (e.g., due to set +e in bash), subsequent git push → ssh inherit the pipe write-end. Since ssh calls setsid(), it escapes PGID-based cleanup.
Reproduction Steps
- Configure an agent with a Stop hook that runs
git pushto a remote repo - Run any execution that produces output after 10+ turns
- Observe drain warning/error sequence in agent-server logs
- Execution recorded as failed / no result despite all work completing normally
Suggested Fix
Option A (preferred) — drain buffer before force-closing: In safe_close_pipes(), signal the reader thread to flush remaining buffered data before closing. The in-flight result JSON should be processable before the pipe is torn down.
Option B — enumerate write-end holders before killing: Before calling killpg, check /proc/*/fd (Linux) to find every PID holding a write-end of the pipe FD. Kill those PIDs explicitly, catching setsid() refugees.
Option C — hook author guidance: Document that Stop hook scripts must protect their stdout redirect:
#!/bin/bash
# IMPORTANT: release inherited stdout pipe before any git/network operations
exec >/dev/null 2>&1 # do NOT put set +e before this line
set +eEnvironment
- Trinity version:
f199383 - Reproducible with: any agent Stop hook that spawns
ssh(e.g.,git push)
Related
- Companion bug: bug: process_stream_line() crashes with AttributeError when message.content is a string #585 (
process_stream_line()string content crash) — different failure mode, same 502 outcome start_new_session=Trueon Claude subprocess means Claude PGID = Claude PID; hooks inherit this PGID butsetsid()in child processes breaks the containment
Activity
vybe commentedon Apr 29, 2026
Investigation update (2026-04-29)
Confirmed real, partially mitigated.
What's already fixed
Commit
44c6802(#531/#532, merged before f199383) added the 30s post-kill natural-drain window indrain_reader_threads(). This is why most agents succeed — the pipe drains naturally once the process group is killed.Why cornelius-m fails on every execution
cornelius-m has a git-sync Stop hook that runs
git push. On Linux,ssh(spawned by git-push) callssetsid(), placing itself in a new session and process group.killpg(pgid, SIGKILL)does not reach it. The 30s grace period is then spent waiting forsshto finish its network I/O. If the push takes > 30s (slow remote, large delta), the force-close discards the result JSON from the kernel pipe buffer → 502 "no result message" on every execution.No other agent has a Stop hook with
git push. delivery-ops-agent shows 8 leaked-reader warnings in 24h but all 19 executions succeeded (fast natural drain, not the 30s timeout path).Recommended fix: fix the hook, not the platform
Option C (targeted, low risk): Update the git-sync Stop hook to release the inherited stdout pipe write-end before any network I/O:
This closes the pipe write-end in the hook process before
sshis spawned, sosshnever inherits it. PGID kill becomes sufficient again.Option B (platform hardening): Before
safe_close_pipes(), enumerate/proc/*/fdto find all PIDs holding a write-end of the stdout pipe FD and send them SIGKILL explicitly. This catchessetsid()refugees generically. More complex but makes the platform robust against any hook that spawns detached subprocesses.Near-term action: fix the git-sync hook in cornelius-m (Option C). Option B can be a follow-up hardening task.
test(harness): repro scaffolding + negative results for #640 — 6 hypo…
AndriiPasternak31 commentedon May 11, 2026
/claim
github-actions commentedon May 11, 2026
@AndriiPasternak31 This issue is already assigned to AndriiPasternak31. Use
/unclaimfirst if you want to take over.Merge branch 'dev' into AndriiPasternak31/issue-586-plan-v1
fix(scripts): slurp JSONL for per-container summary in 586-fleet-chec…
fix(scripts): use grep-based gate for residual #586 events in fleet-c…
Merge branch 'dev' into AndriiPasternak31/issue-586-plan-v1
feat(observability): emit [METRIC] drain_outcome on slow-path orphan-…
Merge remote-tracking branch 'origin/dev' into AndriiPasternak31/issu…
test(#586) + test-suite recovery + doc backfill + CSO 2026-05-17 (#875)
docs(feature-flows): document #970 headless drain decoupling
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
fix(headless): address review findings on the drain/finalize hardening (
fix(headless): address review findings on the drain/finalize hardening (
Release: v0.6.1 (#1171)
fix(headless): harden drain/finalize against leaked reader threads (#…
Release: v0.7.0 (#1319)