Repository navigation
Description
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
- Execution X is launched at
:10— backend accepts, acquires slot, but Claude session never starts (silent launch failure) - Scheduler's job function enters polling loop: checks execution status every 10s for up to 3600s
- At
:10+1h— next cron trigger fires, but APScheduler sees the previous job function is still running (polling), skips it - At
:11+1h— polling deadline exceeded, execution X marked failed, job function finally returns - 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
- Configure an agent with a scheduled task running every hour
- Simulate a stuck execution (e.g., create an execution record that never completes)
- Wait for the next cron trigger
- 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 immediatelyOption 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 apschedulerin scheduler container) - Docker version: 28.3.3
- OS: Debian GNU/Linux 12 (bookworm)
Related
- Issue bug: Executions stuck in 'running' state due to scheduler/backend database split #90 (scheduler/backend database split — original cause of silent launch failures)
- Companion issue: uvicorn worker deaths (another source of stuck executions that trigger this bug)
Activity
vybe commentedon Mar 14, 2026
Related: #131 (uvicorn worker deaths — one of the root causes of stuck executions that trigger this skip behavior)
docs(planning): Sprint A now 4/6 complete
fix(scheduler): fire-and-forget dispatch to prevent APScheduler skips (…
fix(scheduler): fire-and-forget dispatch to prevent APScheduler skips (…
docs(planning): fix PR number reference for #132 (was #327, actual #328)