Skip to content

bug: cleanup watchdog re-verify defers indefinitely when agent unreachable, leaking slots under sustained overload #497

Closed

Description

@vybe

Summary

services/cleanup_service.py re-verifies whether an "orphaned" execution is still actually running on the agent before reaping its slot (per the issue-378 hardening). When the agent container is unreachable (CPU-pegged, network-stuck, or itself blocked on a downstream call), the re-verify request times out and the cleanup service logs Skipping ... — agent unreachable during re-verify (#378); Phase 1 stale cleanup is fallback and defers the cleanup. Under sustained load (fan-out workloads where agents are at 3/3 capacity for tens of minutes), this defers across many cleanup cycles, holding slots that should have been freed. New incoming work then bounces off Agent at capacity (N/N parallel tasks running) even though the underlying slot is dead.

⚠️ Validation needed during implementation — root cause framing inferred from cleanup logs and DB-vs-slots divergence on a live instance. Not yet reproduced in isolation.

Component

Backend / Cleanup Service / SlotService

Priority

P2

Error

Per cleanup cycle (sample, sanitized):

[Cleanup] Skipping <exec-id> for '<agent>' — agent unreachable during re-verify (#378); Phase 1 stale cleanup is fallback
[Cleanup] Skipping <exec-id> for '<agent>' — agent unreachable during re-verify (#378); Phase 1 stale cleanup is fallback
[Cleanup] Cycle complete: {'orphaned_executions': 0, 'auto_terminated': 0, 'stale_executions': 0, 'no_session_executions': 1, 'orphaned_skipped': 0, 'stale_activities': 0, 'stale_slots': 1, ...}

The same execution IDs appear across consecutive cycles (60s apart) without progress.

Location

Root Cause

The re-verify branch added to address #378 ("Execution shows Failed then Success after cleanup race") avoids prematurely marking still-running executions as failed by hitting the agent and confirming. The branch correctly handles the "agent says still running" case but treats "agent unreachable" as "be safe, defer". On a healthy fleet that's fine, but the failure modes that produce orphans (uvicorn worker recycle / GIL-stalled backend / agent container CPU-pinned) are exactly the conditions under which the re-verify call also fails. The defer-forever loop turns a recoverable orphan into an unbounded slot leak.

The Phase 1 fallback is described in the log as "fallback" but it doesn't actually fire when re-verify is unreachable — it fires only when the agent is reachable but reports nothing for the execution.

Reproduction Steps

  1. Configure 1 agent with max_parallel_tasks=3 and a long-running skill (10-30 min)
  2. Drive 5+ overlapping task calls so the agent is at 3/3 for an extended period
  3. Induce a backend-side connection drop (force-kill the uvicorn worker holding the in-flight call, mirrors bug: long-running backend→agent task calls dropped when uvicorn worker recycles #408) — execution row stays running in DB
  4. While the agent is still under load (so re-verify HTTP request can't be answered quickly), watch trinity-backend logs
  5. Observe [Cleanup] Skipping ... — agent unreachable during re-verify (#378) repeating every cycle for the same execution ID
  6. Observe Agent at capacity (3/3) for incoming work despite no real third execution

Suggested Fix

Two complementary directions, either alone may be enough:

  1. Bound the deferral. Track per-execution skip count in the cleanup service. After N consecutive agent unreachable during re-verify skips (e.g., N=3, ~3 minutes at 60s cycle), escalate to forced reap with a clearly-labeled error like Stale execution — agent unresponsive across N re-verify attempts. Prevents an unreachable agent from holding slots indefinitely.
  2. Use a recent-progress signal as authoritative when re-verify is unreachable. If the execution's last-heartbeat / last-activity timestamp is older than its timeout_seconds + grace, treat it as expired regardless of re-verify reachability. Right now the re-verify result outranks the slot TTL, which inverts the intended safety.

The slot TTL already exists (acquire_slot(... timeout_seconds=...) → TTL stored alongside slot). Letting TTL win when the agent is unreachable preserves the #378 fix for the common case (agent reachable, says "still running") while preventing the leak.

Environment

  • Trinity version: c3f3f1c
  • Observed on a live instance with a fan-out workload (cleon-style 7-way parallel dispatch) under sustained 30-60 min overload windows

Related

Activity

  1. self-assigned this
    on May 11, 2026
  2. added
    status-in-progress
    and removed on May 11, 2026
  3. added a commit that references this issue on May 11, 2026
  4. vybe commented on May 11, 2026

    @vybe
    ContributorAuthor

    Fixed by #783 (squash-merged to dev).

  5. added
    complexity-low
    theme-reliability
    on Jun 3, 2026
  6. removed
    status-in-progress
    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-lowpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions