Skip to content

bug: stdout reader thread stuck 35s after process exit when hook subprocess escapes process group kill #586

Closed

Description

@vybe

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() in execute_headless_task

Root Cause

  1. Claude Code exits → agent-server starts drain with 5s grace period
  2. stdout reader thread still alive (pipe write-end still open)
  3. killpg(pgid, SIGKILL) kills Claude process group
  4. ssh (spawned by git push inside Stop hook) has called setsid() — new session/PGID — survives the kill
  5. 30s post-kill grace: ssh is doing network I/O (remote push), takes 10-30s
  6. After 30s: safe_close_pipes() closes the read-end of the stdout pipe
  7. readline() in reader thread raises ValueError: I/O operation on closed file
  8. 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

  1. Configure an agent with a Stop hook that runs git push to a remote repo
  2. Run any execution that produces output after 10+ turns
  3. Observe drain warning/error sequence in agent-server logs
  4. 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 +e

Environment

  • Trinity version: f199383
  • Reproducible with: any agent Stop hook that spawns ssh (e.g., git push)

Related

Activity

  1. vybe commented on Apr 29, 2026

    @vybe
    ContributorAuthor

    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 in drain_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) calls setsid(), placing itself in a new session and process group. killpg(pgid, SIGKILL) does not reach it. The 30s grace period is then spent waiting for ssh to 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:

    #!/bin/bash
    exec >/dev/null 2>&1   # release inherited pipe FD before git push spawns ssh
    set +e
    git push origin ...

    This closes the pipe write-end in the hook process before ssh is spawned, so ssh never inherits it. PGID kill becomes sufficient again.

    Option B (platform hardening): Before safe_close_pipes(), enumerate /proc/*/fd to find all PIDs holding a write-end of the stdout pipe FD and send them SIGKILL explicitly. This catches setsid() 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.

  2. added a commit that references this issue on May 7, 2026
  3. added
    status-in-progress
    and removed on May 11, 2026
  4. AndriiPasternak31 commented on May 11, 2026

    @AndriiPasternak31
    Contributor

    /claim

  5. github-actions commented on May 11, 2026

    @github-actions

    @AndriiPasternak31 This issue is already assigned to AndriiPasternak31. Use /unclaim first if you want to take over.

  6. added a commit that references this issue on May 23, 2026
  7. added
    status-in-dev
    and removed
    status-in-progress
    on May 28, 2026
  8. added a commit that references this issue on May 29, 2026
  9. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

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

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

Metadata

Metadata

Labels

priority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions