Repository navigation
Closed
Labels
Description
Summary
When the backend crashes or restarts, regular task executions (scheduled, chat, MCP-triggered) stuck in running status are not recovered on startup. They remain in running until the cleanup service marks them as failed after 120 minutes. This wastes capacity slots and leaves stale state in the UI.
Context
Process Engine executions already have startup recovery via ExecutionRecoveryService in executions.py — this issue extends the same concept to regular schedule_executions.
What Exists Today
| Mechanism | Scope | Gap |
|---|---|---|
ExecutionRecoveryService |
Process Engine executions only | Does NOT cover regular task executions |
CleanupService (every 5 min) |
Marks running > 120 min as failed |
120 min is too long for crash recovery |
| Redis slot TTL | Auto-expires slots after timeout + 5min |
Doesn't update DB execution status |
What Happens After a Crash
- Backend restarts
schedule_executionsrows withstatus='running'from before the crash still exist- Agent containers may have been restarted too — process registry is empty
- The execution is effectively orphaned but still shows as "running" in the UI
- Cleanup service will eventually mark it failed — but only after 2 hours
- During this time, the capacity slot may also be orphaned in Redis
Proposed Solution
On backend startup (in main.py lifespan handler, after existing recovery):
- Query
schedule_executionsWHEREstatus = 'running' - For each execution:
a. Check if the agent container is running (docker inspect)
b. If container is running, check agent's process registry:GET /api/executions/{id}/status
c. If execution is NOT found on agent → mark asfailedwith error:"Execution orphaned — recovered on backend restart"
d. If container is not running → same treatment
e. If execution IS found on agent → leave it alone (still legitimately running) - Release any associated capacity slots for recovered executions
- Log recovery summary (count recovered, count still running)
Files to Change
src/backend/main.py— Add call to regular execution recovery in lifespan handlersrc/backend/services/cleanup_service.py— Addrecover_orphaned_executions()method (or new service)src/backend/db/schedules.py— Add query for running executions with agent cross-check
Acceptance Criteria
- On startup,
runningexecutions are checked against agent process registries - Orphaned executions (not found on agent) are marked
failedwith descriptive error - Capacity slots are released for recovered executions
- Executions still legitimately running on agents are left untouched
- Recovery summary logged at startup
- No regressions to existing process engine recovery
Related
- Supersedes Phase 1 of Orphaned Execution Recovery #23 (Orphaned Execution Recovery)
src/backend/routers/executions.py— Existing process engine recovery (reference pattern)src/backend/services/cleanup_service.py— Existing time-based cleanupsrc/backend/services/slot_service.py— Slot release
Activity
fix: Startup recovery for regular task executions (Abilityai#128)
fix: Startup recovery for regular task executions (#128) (#165)
merge: integrate upstream/main into feature branch
merge: Resolve conflicts with upstream/main — changelog and feature-f…