Skip to content

bug: agent-server.py develops futex spin loop after days of uptime #333

Closed as not planned

Description

@vybe

Summary

Agent containers develop excessive CPU usage (50%+ each) after running for several days. The agent-server.py process gets stuck in a futex spin loop, causing thread contention. Restarting the affected containers immediately resolves the issue.

Component

Agent Runtime / agent-server.py

Priority

P2 (workaround: restart containers)

Error

# strace output showing 100% time spent on futex with ~50% errors
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.061633          32      1897       948 futex
  0.00    0.000002           2         1         1 restart_syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.061635          32      1898       949 total

Location

  • File: src/agent-base/agent-server.py
  • Function/Class: Unknown - likely in async polling or thread pool logic

Root Cause

Unknown. The futex spin pattern (1897 calls with 948 errors in 5 seconds) suggests:

  • Thread pool exhaustion with busy-wait fallback
  • Accumulated orphaned async tasks
  • Memory/connection leak causing GC pressure
  • Race condition in the polling loop

The issue develops over time (2-6 days of uptime) and affects multiple containers simultaneously.

Reproduction Steps

  1. Deploy agents with scheduled tasks (runs every 2-6 hours)
  2. Let containers run for 2-6 days without restart
  3. Observe CPU usage gradually increasing
  4. Use strace -p <pid> -c to confirm futex spin loop
  5. Restart container to resolve

Suggested Fix

Investigation needed. Potential areas to examine:

  1. Thread pool management - Check for thread leaks or exhaustion
  2. Async task cleanup - Ensure completed/failed tasks are properly cleaned up
  3. Connection pooling - Check HTTP client connections aren't accumulating
  4. Polling backoff - Verify exponential backoff on repeated failures (e.g., 404s for operator-queue.json)
# Potential issue: polling loop without proper backoff
# If the loop doesn't sleep properly on failure, it could spin

# Current (suspected):
while True:
    response = await fetch_operator_queue()  # Returns 404
    # No delay on failure → spins

# Should be:
while True:
    response = await fetch_operator_queue()
    if response.status == 404:
        await asyncio.sleep(5)  # Backoff on missing file

Environment

  • Trinity version: be35c64
  • Docker version: 24.x
  • OS: Ubuntu 22.04
  • Container uptime when issue manifests: 2-6 days

Observed Behavior

  • 7 agent containers simultaneously consuming 50%+ CPU each (~350% total)
  • System load average: 8.5+ on 4-core system
  • Container logs show repeated polling for operator-queue.json (404) and chat/session (200)
  • No active executions running at the time
  • Zombie claude processes present in some containers

Related

  • Affected file: src/agent-base/agent-server.py
  • May relate to operator queue polling logic (OPS-001)

Activity

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

    @dolho
    Contributor

    PR #807 ships defensible accumulator-leak hardening + a /health diagnostics block. It does not claim to close this — root cause is still unproven.

    What shipped:

    • gemini_runtime per-call ThreadPoolExecutor → module-level singleton (mirrors claude_code.py:63).
    • state.conversation_history FIFO-bounded (default 1000, override via AGENT_HISTORY_LIMIT).
    • /health now exposes thread_count, asyncio_task_count, running_executions, history size/limit. Next repro = one curl, not strace.

    Why this stays open:

    Ask: if anyone hits 50%+ CPU on agent-server again, capture curl http://<agent>:8000/health over time (look for monotonic growth in any diagnostics field) plus strace -p <pid> -c for 5s, then drop the data here.

  4. added and removed
    status-in-progress
    on May 12, 2026
  5. added
    complexity-medium
    theme-reliability
    on Jun 3, 2026
  6. vybe commented on Jun 9, 2026

    @vybe
    ContributorAuthor

    Closing during 2026-06-09 reprioritization as stale/likely-irrelevant — filed 2026-04-14, never reproduced, and the agent-server execution path has changed substantially since (headless drain/finalize hardening #1025, heartbeat loop #307, stall-watchdog). Reopen with a fresh repro if a long-uptime futex spin recurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-mediumpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions