Skip to content

bug: APScheduler max_instances=1 causes skipped executions despite agent having parallel slots #132

Closed

Description

@vybe

Summary

APScheduler's per-job max_instances=1 setting is independent of Trinity's per-agent parallel capacity (Redis slots). When a scheduled execution gets stuck (e.g., due to a silent launch failure), the scheduler's synchronous polling blocks the Python job function for up to 1 hour, causing APScheduler to skip subsequent triggers with "maximum number of running instances reached (1)" — even though the agent has available capacity slots.

Component

Scheduler / APScheduler Job Configuration

Priority

P2

Error

[WARNING] apscheduler.scheduler: Execution of job "agent-name:Heartbeat (trigger: cron[...])" 
  skipped: maximum number of running instances reached (1)
[WARNING] scheduler.service: Job schedule_XXX skipped: previous execution still running (max_instances reached)

Two Different Concurrency Systems

System Controls Default Where
APScheduler max_instances Concurrent Python function invocations per cron job 1 In-process
Trinity SlotService Concurrent Claude sessions per agent 3 Redis ZSET

These are completely independent. An agent can have 3 available Redis slots, but APScheduler will still refuse to fire the same job twice if the previous invocation's Python function hasn't returned.

Causal Chain

  1. Execution X is launched at :10 — backend accepts, acquires slot, but Claude session never starts (silent launch failure)
  2. Scheduler's job function enters polling loop: checks execution status every 10s for up to 3600s
  3. At :10+1h — next cron trigger fires, but APScheduler sees the previous job function is still running (polling), skips it
  4. At :11+1h — polling deadline exceeded, execution X marked failed, job function finally returns
  5. At :10+2h — next trigger works fine because the job function is no longer blocked

The skip creates a "skipped" execution record in the database that never transitions to a terminal state (separate DB cleanup issue).

Reproduction Steps

  1. Configure an agent with a scheduled task running every hour
  2. Simulate a stuck execution (e.g., create an execution record that never completes)
  3. Wait for the next cron trigger
  4. Observe APScheduler skip warning in scheduler logs

Suggested Fix

Option A: Return immediately after backend accepts (preferred)

Change the scheduler's job function to return as soon as the backend accepts the execution (returns execution_id). Move status polling to a separate async background task. This decouples APScheduler's job lifecycle from execution completion.

# BEFORE: synchronous polling blocks the job function
async def execute_schedule(self, schedule_id, agent_name, ...):
    result = await self.backend_client.start_execution(agent_name, ...)
    execution_id = result["execution_id"]
    # This blocks for up to 1 hour:
    await self.poll_until_complete(execution_id, timeout=3600)

# AFTER: fire-and-forget with async polling
async def execute_schedule(self, schedule_id, agent_name, ...):
    result = await self.backend_client.start_execution(agent_name, ...)
    execution_id = result["execution_id"]
    # Start polling in background, don't block APScheduler
    asyncio.create_task(self.poll_until_complete(execution_id, timeout=3600))
    return  # Job function returns immediately

Option B: Increase max_instances to match agent capacity

# When adding jobs, use the agent's max_parallel_tasks
scheduler.add_job(
    execute_schedule,
    trigger=cron_trigger,
    max_instances=agent.max_parallel_tasks,  # e.g., 3 instead of 1
    ...
)

Option C: Mark skipped executions as terminal

At minimum, ensure the "skipped" execution record is immediately marked as a terminal state (skipped → cancelled or failed) so it doesn't require manual cleanup:

if job_skipped:
    execution.status = "cancelled"
    execution.error = "Skipped: previous execution still polling"
    execution.completed_at = datetime.utcnow()

Environment

  • Trinity version: ccf573d
  • APScheduler version: (check pip show apscheduler in scheduler container)
  • Docker version: 28.3.3
  • OS: Debian GNU/Linux 12 (bookworm)

Related

Activity

  1. vybe commented on Mar 14, 2026

    @vybe
    ContributorAuthor

    Related: #131 (uvicorn worker deaths — one of the root causes of stuck executions that trigger this skip behavior)

  2. added a commit that references this issue on Apr 14, 2026
  3. added
    status-in-progress
    on Apr 14, 2026
  4. added a commit that references this issue on Apr 14, 2026
  5. added
    complexity-low
    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

No one assigned

    Labels

    complexity-lowpriority-p2type-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions