Skip to content

bug: stdio MCP subprocess stdout inheritance causes 'Execution completed without a result message' #630

Closed

Description

@vybe

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:

  1. Claude (pid N) exits after writing its {"type":"result",...} message to stdout
  2. Reader thread is still busy (waiting for EOF) — MCP subprocesses hold the write end of the pipe
  3. _kill_orphan_pipe_writers finds 0 orphans (they're in same pgid) — no-op
  4. Process group is killed; 30s drain wait starts
  5. Reader thread drains naturally after MCP processes die
  6. But: execution result extraction has already declared failure before step 5 completes

Reproduction Steps

  1. Configure an agent with one or more stdio-based MCP servers (e.g. npx -y some-mcp-server in .mcp.json)
  2. Run a long task on the agent (>50 agentic turns, ~10+ minutes)
  3. Observe Reader thread(s) still busy after process exit warning in agent-server logs
  4. Observe Execution completed without a result message error immediately after drain
  5. Note that Reader thread(s) drained naturally after grandchild termination appears — 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 dead

Fix 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.json or claude_desktop_config.json

Related

  • docker/base-image/agent_server/utils/subprocess_pgroup.py — _kill_orphan_pipe_writers, terminate_process_group
  • docker/base-image/agent_server/services/claude_code.py — result capture and error reporting
  • Prior fix commits addressing related symptoms: 3b02871, 44c6802, 2266b48, c43748d

Activity

  1. added
    status-in-progress
    on May 4, 2026
  2. self-assigned this
    on May 4, 2026
  3. dolho commented on May 4, 2026

    @dolho
    Contributor

    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.

  4. added
    status-in-dev
    and removed
    status-in-progress
    on May 28, 2026
  5. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

    Shipped in v0.6.0 (released via #998).

  6. added
    complexity-medium
    theme-reliability
    and removed
    status-in-dev
    on Jun 1, 2026
  7. added a commit that references this issue on Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-mediumpriority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions