Skip to content

bug: circuit breaker has no half-open recovery — stays open permanently after agent probe timeouts under load #687

Closed

Description

@vybe

Summary

The circuit breaker in services.agent_client opens when an agent's health probe times out under heavy CPU load (e.g. during a long-running Claude execution). Once open, the circuit never transitions to a half-open state to test recovery — the failure counter only climbs. The only reset is a backend process restart. As a secondary effect, the open circuit blocks the cleanup service's re-verify step (#378), causing completed executions to remain stuck as "running" in the DB (zombie executions) until the next restart.

Component

Backend / services.agent_client (circuit breaker) + services.cleanup_service (re-verify step)

Priority

P1 — scheduled execution reliability is impaired; workaround is docker restart trinity-backend

Observed Behaviour

  1. Agent starts a long, CPU-heavy execution (>15 min, agent CPU >120%)
  2. Health probe to the agent times out repeatedly (5s timeout) → circuit opens
  3. Failure counter climbs at ~1.5–2 per minute for the duration of the execution
  4. Execution completes normally (claude exits, drain finishes cleanly) — agent CPU drops back to ~30%
  5. Circuit does not recover: counter keeps climbing even after execution ends, because there is no half-open test
  6. Cleanup service runs its re-verify pass; circuit-open causes it to skip the stale execution:
[Cleanup] Skipping {execution_id} for '{agent_name}' — agent unreachable during re-verify (#378); Phase 1 stale cleanup is fallback
  1. Execution remains stuck as running in DB indefinitely (zombie)
  2. Only docker restart trinity-backend resets the in-memory circuit counter and allows startup recovery to clear the zombie

Reproduction Steps

  1. Deploy current dev branch (f7d7593+)
  2. Trigger a long-running, computationally heavy task on an agent (expect agent container CPU >120% for 15+ minutes)
  3. Monitor docker logs trinity-backend for Circuit OPENED for agent {name} after N failures
  4. Note that failure count climbs monotonically for the duration of the execution
  5. Wait for execution to complete (claude process exits, drain finishes)
  6. Observe circuit failure count continues climbing after completion — no recovery
  7. Observe cleanup service skipping the now-completed execution with the re-verify warning above
  8. Query DB: execution status remains running despite no active process

Root Cause

The circuit breaker in services.agent_client appears to implement only two states: closed and open. There is no half-open state. A standard circuit breaker pattern periodically allows a test probe through when open, and closes the circuit on success. Without this:

  • Once opened by load-induced probe timeouts, the circuit never self-heals
  • Every subsequent probe (from scheduler dispatches, cleanup re-verify, etc.) is short-circuited and logged as another failure, inflating the counter further
  • The circuit becomes permanently open until the backend process restarts

Secondary issue: the cleanup service's re-verify step (#378) checks agent reachability via the same circuit-breaker-wrapped path. When the circuit is open, re-verify always fails, so any execution that completed while the circuit was open will remain a zombie.

Suggested Fix

Add a half-open recovery mechanism to the circuit breaker:

# Pseudocode — adapt to actual implementation in services/agent_client.py
class CircuitBreaker:
    # After circuit has been open for N seconds, allow one probe through (half-open)
    HALF_OPEN_AFTER_SECONDS = 30  # or configurable
    
    def should_allow_request(self, agent_name):
        state = self.get_state(agent_name)
        if state == CLOSED:
            return True
        if state == OPEN:
            time_open = now() - self.opened_at[agent_name]
            if time_open >= self.HALF_OPEN_AFTER_SECONDS:
                self.set_state(agent_name, HALF_OPEN)
                return True  # let one probe through
            return False
        if state == HALF_OPEN:
            return False  # already testing, block others
    
    def record_success(self, agent_name):
        self.failure_counts[agent_name] = 0
        self.set_state(agent_name, CLOSED)  # probe succeeded — close circuit
    
    def record_failure(self, agent_name):
        # ... existing logic, but also handle HALF_OPEN → OPEN transition

Alternatively, consider a time-based auto-reset: if the circuit has been open for >60s and no new execution is active on that agent, reset to closed.

Also consider: the cleanup service's re-verify step should bypass the circuit breaker (or use a separate direct probe), since re-verify is specifically for cases where normal operation has already failed.

Observed Pattern

Across multiple restarts today, clean window durations before next runaway:

  • Restart 1 → runaway onset: ~55 min
  • Restart 2 → runaway onset: ~131 min
  • Restart 3 → runaway onset: ~94 min

Each runaway was triggered by a heavy execution on the same agent. Each drain completed cleanly (no deadlock — #657 fix confirmed working). The circuit was the sole cause of the zombie in each case.

Environment

  • Trinity version: f7d7593 (dev branch)
  • OS: Ubuntu 22.04
  • Docker: 24.x

Related

Activity

  1. vybe commented on May 6, 2026

    @vybe
    ContributorAuthor

    /claim

  2. github-actions commented on May 6, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/687-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #687
  3. added
    status-in-progress
    on May 6, 2026
  4. added
    status-in-dev
    and removed
    status-in-progress
    on May 6, 2026
  5. vybe commented on Jun 1, 2026

    @vybe
    ContributorAuthor

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

  6. 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

priority-p1type-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions