Repository navigation
Description
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
- File:
services/cleanup_service.py(re-verify branch added for bug: Execution shows Failed then Success after cleanup (race condition) #378) - Symptom surface:
services/slot_service.pyslot table not reaped →Agent at capacityrejections fromrouters/chat.pycapacity check
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
- Configure 1 agent with
max_parallel_tasks=3and a long-running skill (10-30 min) - Drive 5+ overlapping task calls so the agent is at 3/3 for an extended period
- 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
runningin DB - While the agent is still under load (so re-verify HTTP request can't be answered quickly), watch
trinity-backendlogs - Observe
[Cleanup] Skipping ... — agent unreachable during re-verify (#378)repeating every cycle for the same execution ID - Observe
Agent at capacity (3/3)for incoming work despite no real third execution
Suggested Fix
Two complementary directions, either alone may be enough:
- Bound the deferral. Track per-execution skip count in the cleanup service. After N consecutive
agent unreachable during re-verifyskips (e.g., N=3, ~3 minutes at 60s cycle), escalate to forced reap with a clearly-labeled error likeStale execution — agent unresponsive across N re-verify attempts. Prevents an unreachable agent from holding slots indefinitely. - 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
- bug: Execution shows Failed then Success after cleanup (race condition) #378 (closed) — the original race fix that introduced the re-verify branch
- bug: long-running backend→agent task calls dropped when uvicorn worker recycles #408 (open) — uvicorn worker recycle that produces the orphans this code is supposed to reap
- refactor: collapse 9-path cleanup pyramid once agent is authoritative #429 (open) — refactor: collapse 9-path cleanup pyramid; the re-verify branch lives in that pyramid
Activity
docs(cleanup-service): document #497 force-fail on unreachable re-verify
fix(cleanup): force-fail orphan when agent unreachable during re-veri…
vybe commentedon May 11, 2026
Fixed by #783 (squash-merged to dev).