Skip to content

Startup recovery for regular task executions #128

Closed

Description

@vybe

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

  1. Backend restarts
  2. schedule_executions rows with status='running' from before the crash still exist
  3. Agent containers may have been restarted too — process registry is empty
  4. The execution is effectively orphaned but still shows as "running" in the UI
  5. Cleanup service will eventually mark it failed — but only after 2 hours
  6. 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):

  1. Query schedule_executions WHERE status = 'running'
  2. 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 as failed with 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)
  3. Release any associated capacity slots for recovered executions
  4. Log recovery summary (count recovered, count still running)

Files to Change

  • src/backend/main.py — Add call to regular execution recovery in lifespan handler
  • src/backend/services/cleanup_service.py — Add recover_orphaned_executions() method (or new service)
  • src/backend/db/schedules.py — Add query for running executions with agent cross-check

Acceptance Criteria

  • On startup, running executions are checked against agent process registries
  • Orphaned executions (not found on agent) are marked failed with 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 cleanup
  • src/backend/services/slot_service.py — Slot release

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p1type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions