Skip to content

Add cleanup jobs for stuck executions, activities, and leaked slots #94

Closed

Description

@vybe

Problem

Multiple objects can get stuck in intermediate states with no automatic recovery:

Stuck Execution Records

  1. create_task_execution() sets status="running"
  2. If backend or agent crashes before update_execution_status(), record stays "running" forever
  3. Tasks tab shows perpetually "running" execution
  4. No TTL, no cleanup job, no automatic recovery exists

Stuck Activity Records

  1. track_activity() creates activity with activity_state="started"
  2. If exception occurs before complete_activity(), activity stays "started" forever
  3. Dashboard timeline shows perpetual spinner
  4. Code comment at chat.py:355 confirms this was a known bug (fixed for collaboration activities, but may exist in other paths)

Leaked Redis Slots

  1. SlotService uses Redis ZSET (agent:slots:{name}) to track active execution slots
  2. ZSET entries have no auto-expire — only metadata hashes expire (1800s)
  3. Stale cleanup only runs when next acquire_slot() is called
  4. If no new tasks arrive after a crash, slots stay reserved indefinitely
  5. Agent appears "at capacity" permanently

Leaked Redis Queue

  1. Execution queue key agent:running:{name} has 600s TTL (auto-cleans)
  2. But queue list agent:queue:{name} has no TTL
  3. If items get orphaned in the queue list, they stay forever

Required Changes

1. Background Cleanup Job (runs every 5 minutes)

async def cleanup_stale_records():
    # Executions stuck in "running" for >30 minutes → mark "failed"
    # Activities stuck in "started" for >30 minutes → mark "failed"
    # Redis slots older than SLOT_TTL → remove from ZSET
    # Redis queue items without matching running key → clear

2. Startup Cleanup

On backend startup, sweep for any records stuck from a previous crash.

3. Health Check Integration

Add stuck-record counts to the health/monitoring endpoint so operators can see problems.

Files to Change

  • src/backend/services/cleanup_service.py — NEW: periodic cleanup
  • src/backend/main.py — Register cleanup on startup
  • src/backend/db/schedules.py — Query for stuck executions
  • src/backend/db/activities.py — Query for stuck activities
  • src/backend/services/slot_service.py — Add periodic ZSET cleanup
  • src/backend/services/execution_queue.py — Add queue orphan cleanup

Activity

  1. added
    priority-p1
    and removed
    priority-p1
    on Mar 11, 2026
  2. vybe commented on Mar 30, 2026

    @vybe
    ContributorAuthor

    Actualization (2026-03-30)

    All items in this issue are fully implemented in src/backend/services/cleanup_service.py (271 lines):

    Implemented:

    • ✅ Background cleanup loop every 5 minutes
    • ✅ Stale execution marking (120-minute timeout) → status failed
    • ✅ Stale activity cleanup → activity_state='failed'
    • ✅ Redis slot cleanup and reclaim (orphaned ZSET entries)
    • ✅ Startup sweep on backend restart
    • ✅ Admin status endpoint: GET /api/monitoring/cleanup-status
    • ✅ Admin trigger endpoint: POST /api/monitoring/cleanup-trigger
    • ✅ No-session execution fast-fail (bug: Cleanup service misses 'skipped' executions and slow to detect no-session failures #106 follow-up)
    • ✅ Orphaned execution recovery

    Recent fixes:

    This issue can be closed.

  3. vybe commented on Mar 30, 2026

    @vybe
    ContributorAuthor

    Closing — fully implemented in cleanup_service.py. See actualization comment above.

  4. added
    complexity-medium
    on Jun 3, 2026
  5. rgsaura commented on Nov 20, 2026

    @rgsaura

    Owner decision (Option B, 2026-11-20): keep the warmed shared sending domain but replace the confusing <slug>@relay.exohuman.cc sender with a professional transactional identity — instance email is used only for password resets and sign-in codes.

    Scope agreed:

    • From address becomes [email protected] (single professional identity; no DNS change required — SPF/DKIM for relay.exohuman.cc are already aligned and warmed).
    • The human-readable display name carries the instance brand (configured company brand, else humanized instance label, else ExoHuman), per the white-label presentation policy.
    • The three active fleet identities are migrated to the new form as part of deployment.
    • The per-instance subdomain strategy (in-<slug>.mail.exohuman.cc) remains the documented future path; the ops lane established it requires source + DNS + DKIM work under the self-hosted MTA (it was only ever implemented for the retired Resend provider).

    #94 stays open until the new identity is live on production and verified (one authorized probe).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions