Repository navigation
Description
Summary
When an agent has stdio-based MCP servers configured (e.g. npx-launched packages), those processes inherit Claude's stdout pipe. When Claude exits after completing its work, the MCP subprocesses remain alive and keep the write end of the stdout pipe open. The agent-server reader thread blocks waiting for EOF and never gets it — the agent-server declares the execution failed with "Execution completed without a result message" even though Claude actually completed its task successfully.
This issue has been observed consistently on long-running tasks (~18 min, 100+ turns). The success rate for affected agents is very low (roughly 2/15 recent executions).
Component
Agent Runtime / docker/base-image/agent_server/
Priority
P1
Error
WARNING:agent_server.utils.subprocess_pgroup:[Subprocess] Reader thread(s) still busy after process exit (pid=<N>, stuck_count=1) — killing process group, then waiting 30s for natural drain
WARNING:agent_server.services.claude_code:Failed to parse line as JSON: {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_...","
INFO:agent_server.utils.subprocess_pgroup:[Subprocess] Reader thread(s) drained naturally after grandchild termination (pid=<N>)
ERROR:agent_server.services.claude_code:[Headless Task] Execution completed without a result message after 0 tool calls / 132 turns (raw_messages=227). Likely cause: a tool or child subprocess inherited stdout and prevented the claude reader thread from capturing the final result block.
Location
- File:
docker/base-image/agent_server/utils/subprocess_pgroup.py - Function:
_kill_orphan_pipe_writers/terminate_process_group - Also affected:
docker/base-image/agent_server/services/claude_code.py— result capture logic
Root Cause
_kill_orphan_pipe_writers only kills processes that are in a different session (different pgid) from the Claude child process. However, stdio MCP subprocesses launched via npx are in the same process group as Claude — they are Claude's own children. So _kill_orphan_pipe_writers returns 0 (no orphans found) and does not kill them.
The fallback code then kills the entire process group (which does kill the MCP servers), but by then the execution result capture logic has already concluded there is no result and marked the execution as failed. The reader thread drains successfully after the failure is declared — but the result is discarded.
The sequence:
- Claude (pid N) exits after writing its
{"type":"result",...}message to stdout - Reader thread is still busy (waiting for EOF) — MCP subprocesses hold the write end of the pipe
_kill_orphan_pipe_writersfinds 0 orphans (they're in same pgid) — no-op- Process group is killed; 30s drain wait starts
- Reader thread drains naturally after MCP processes die
- But: execution result extraction has already declared failure before step 5 completes
Reproduction Steps
- Configure an agent with one or more stdio-based MCP servers (e.g.
npx -y some-mcp-serverin.mcp.json) - Run a long task on the agent (>50 agentic turns, ~10+ minutes)
- Observe
Reader thread(s) still busy after process exitwarning in agent-server logs - Observe
Execution completed without a result messageerror immediately after drain - Note that
Reader thread(s) drained naturally after grandchild terminationappears — the drain did succeed, just too late
Suggested Fix
Two complementary fixes:
Fix 1 — Check result after drain (most impactful):
In claude_code.py, after the process group kill + drain completes, check if the reader thread captured a result message during the drain before declaring failure. The result is read; it's just checked too early.
# After waiting for drain (the 30s wait in subprocess_pgroup.py terminates):
# Re-check if result_message was populated during the drain
if result_message is None:
result_message = reader_thread.captured_result # or however it's stored
if result_message is None:
raise ExecutionError("Execution completed without a result message...")Fix 2 — Kill same-pgid pipe holders too:
In _kill_orphan_pipe_writers, also check for processes in the same process group as the child that hold open the write end of the child's stdout pipe, but where the child itself has already exited. These are grandchildren of the agent-server that outlived their parent.
# In addition to checking /proc/<pid>/fdinfo for different-session processes,
# also check processes in the same pgid as child_pid where child_pid is deadFix 3 — Close-on-exec for stdout (preventive):
When launching Claude, set close_on_exec on the write end of the stdout pipe before exec, or ensure that any subprocess Claude launches cannot inherit the pipe fd. This prevents the problem at the source.
Environment
- Trinity version:
bfa99ef - Agent-server:
docker/base-image/agent_server/ - Affects agents with stdio MCP servers in
.mcp.jsonorclaude_desktop_config.json
Related
docker/base-image/agent_server/utils/subprocess_pgroup.py—_kill_orphan_pipe_writers,terminate_process_groupdocker/base-image/agent_server/services/claude_code.py— result capture and error reporting- Prior fix commits addressing related symptoms:
3b02871,44c6802,2266b48,c43748d
Activity
dolho commentedon May 4, 2026
Partial fix in #639 (per-line reader isolation + result-metadata recovery from raw_messages). #640 tracks the remaining wire-corruption / MCP stdout-inheritance root cause.
fix(agent-server): recover lost result metadata + harden per-line rea…
fix(agent-server): recover lost result metadata + harden per-line rea…
test(harness): repro scaffolding + negative results for #640 — 6 hypo…
vybe commentedon Jun 1, 2026
Shipped in v0.6.0 (released via #998).
Release: v0.6.1 (#1171)