Skip to content

bug: budget-exceeded subprocess drain leaks a reader thread that pegs a CPU core until container restart (residual of #728) #1502

Closed

Description

@dolho

Context

#728 bounded the safe_close_pipes TextIOWrapper-lock deadlock with a 90s _DRAIN_BUDGET_SECONDS (docker/base-image/agent_server/services/subprocess_lifecycle.py, _drain_bounded). On budget-exceeded it returns budget_exceeded and leaks the reader thread to unwedge the task/executor. That fixed the up-to-7200s hang.

Problem (residual of #728)

On budget_exceeded, the leaked reader thread (and/or an un-reaped claude grandchild still holding the pipe) then spins at ~100% CPU with no visible claude child process, and stays there until the agent container is stopped/restarted. The 90s cap recovers the task/server but not the CPU — a single agent slot burns a full core with no automatic recovery.

The #728 fix explicitly accepted leaking the thread ("Leaked reader threads are daemon threads and die with the container") — but "dies with the container" means the only recovery is a manual restart, and in the meantime the host CPU is pegged.

Repro (observed on the Client Portal, but trigger-agnostic)

Dispatch a turn whose claude subprocess emits a large/blocking stream (e.g. the agent reads an ~83 KB binary file through claude --print --output-format stream-json). Then:

  • agent-server log: WARNING [Subprocess] Drain budget (90s) exceeded — safe_close_pipes may have deadlocked with reader thread's TextIOWrapper lock; reader threads are leaked daemon threads (pid=N). Issue #728.
  • docker stats → the agent container at ~100% CPU
  • container ps -eo pid,pcpu,comm → only agent-server.py (no claude/node child visible)
  • POST /api/agents/{name}/executions/{id}/terminate → 504 (agent-server can't be reached / is busy)
  • only docker stop <agent> clears it

Proposed fix

On budget_exceeded, actively tear down instead of leaking:

  • SIGKILL the claude process group (utils/subprocess_pgroup.py pgid) so any grandchild holding the pipe dies and the pipe EOFs, letting the reader thread unblock/exit;
  • only then leak as a last resort.

This frees the core without a container restart. Distinct from #728, which only unblocks the executor thread.

Severity

P1 — a single agent can hold a full CPU core indefinitely with no automatic recovery; terminate and the cleanup/CB recovery paths can't reach the wedged agent-server.

Related

Activity

  1. added
    priority-p1
    type-bug
    theme-reliability
    complexity-medium
    on Jul 7, 2026
  2. self-assigned this
    on Jul 7, 2026
  3. added
    status-in-progress
    on Jul 7, 2026
  4. added
    status-in-dev
    and removed
    status-in-progress
    on Jul 7, 2026
  5. added a commit that references this issue on Jul 17, 2026
  6. removed
    status-in-dev
    on Aug 7, 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