Repository navigation
Description
Summary
Scheduled and manual executions silently get stuck in status="running" with claude_session_id=NULL and duration_ms=NULL. The execution record is created in the scheduler's database but never synchronized to the backend's database, so when the backend tries to update the status (on success or failure), the update targets a non-existent record and silently does nothing. The record stays "running" forever.
This affects ~14 out of 15 observed orphaned executions — most orphans are NOT caused by backend restarts but by this silent failure path.
Component
Scheduler / Backend / TaskExecutionService
Priority
P2 — Feature impaired (executions still run, but tracking is broken; requires manual cleanup)
Error
No error is logged. The failure is completely silent:
- Scheduler creates execution record locally with
status="running" - Backend receives
execution_idbut doesn't create a matching record in its own database - When execution completes (or fails), backend calls
db.update_execution_status(execution_id=...)which silently no-ops - Record stays "running" in perpetuity with
claude_session_id=NULL,duration_ms=NULL
Location
- File:
src/scheduler/service.py~line 558 — creates execution in scheduler DB - File:
src/scheduler/service.py~line 585 — calls backend withexecution_id - File:
src/backend/services/task_execution_service.py~line 148-160 — skips record creation whenexecution_idis provided - File:
src/backend/services/task_execution_service.py~line 252-262 — tries to update non-existent backend DB record
Root Cause
The scheduler and backend use separate SQLite databases:
- Scheduler (
src/scheduler/database.py) creates execution record in its own DB - Scheduler calls backend's
/api/internal/execute-taskwithexecution_id - Backend (
TaskExecutionService) seesexecution_idis provided, so skips creating a record in its own DB (assumes it already exists) - Agent execution proceeds — on completion/failure, backend calls
db.update_execution_status()on its own DB - Record doesn't exist in backend DB → update is a silent no-op
- Scheduler DB record stays
"running"forever
# task_execution_service.py ~line 148
# This skips record creation when scheduler provides execution_id
if not execution_id:
execution = db.create_task_execution(...) # Only created when NO id provided
execution_id = execution.id if execution else NoneThe same issue affects error handling — when agent container calls fail (e.g., httpx.ConnectError after retries), the exception handler tries to update the backend DB, which also silently fails.
Reproduction Steps
- Have a scheduled task configured for any agent
- Wait for the scheduler to trigger the task
- If the agent container is slow to respond or the execution encounters any issue during launch, the record gets stuck
- Query:
SELECT * FROM schedule_executions WHERE status = 'running' AND claude_session_id IS NULL - Observe records that will never transition out of "running"
Also reproducible with manual triggers — the scheduler path is used for both.
Suggested Fix
Option A (recommended): Have the scheduler NOT create the execution record. Let the backend's TaskExecutionService create it in the backend database, which is the same database used for status updates:
# scheduler/service.py - DON'T create record here
# Instead, pass schedule details to backend and let it create the record
result = await self._call_backend_execute_task(
agent_name=schedule.agent_name,
message=schedule.message,
schedule_id=schedule.id,
triggered_by=triggered_by,
# execution_id=execution.id, # REMOVE - let backend create it
)# task_execution_service.py - ALWAYS create record in backend DB
# Remove the `if not execution_id` gate
execution = db.create_task_execution(...)
execution_id = execution.idOption B: When execution_id is provided, have TaskExecutionService verify the record exists in its DB and create/sync it if missing:
if execution_id:
existing = db.get_execution(execution_id)
if not existing:
# Sync from scheduler - create matching record in backend DB
execution = db.create_task_execution(
execution_id=execution_id, # Use same ID for consistency
...
)Evidence
Out of 15 orphaned executions observed over 3 weeks:
- 14 had
claude_session_id=NULLANDduration_ms=NULL(silent launch failures) - 1 had a session ID (genuine restart orphan)
Affected both scheduled and manual triggers across multiple agents.
Environment
- Trinity version:
e1998fd(fix: Route scheduled tasks through TaskExecutionService for capacity meter visibility) - Note: The recent commit
e1998fdrouted scheduled tasks throughTaskExecutionService, which may have introduced or amplified this issue by changing the execution flow
Related
src/scheduler/service.py— scheduler execution creationsrc/scheduler/database.py— scheduler's separate SQLite databasesrc/backend/services/task_execution_service.py— backend execution servicesrc/backend/db/schedules.py— backend execution database layer
Activity
vybe commentedon Mar 11, 2026
Investigation Results — Root Cause Clarification
The bug is confirmed real, but the "separate databases" framing in the description is incorrect. Here's what's actually happening.
Both services share the same SQLite file
TRINITY_DB_PATH=/data/trinity.db(src/backend/db/connection.py:12)DATABASE_PATH=/data/trinity.db(src/scheduler/config.py:17)trinity-data:/data(docker-compose.yml:55,169)So this is NOT a two-database problem. It's a concurrent SQLite access problem.
Actual root cause: dual-write without WAL mode
Neither the backend nor the scheduler enables WAL mode — both use SQLite's default DELETE journal mode (
src/backend/db/connection.py:18,src/scheduler/database.py:37). DELETE mode uses exclusive file locks for writes.The failure sequence:
service.py:558) callsself.db.create_execution()→ INSERTs row, commits, closes connection.service.py:585) calls backend'sPOST /api/internal/execute-taskwithexecution_id.TaskExecutionService(task_execution_service.py:148) seesexecution_idis provided → skips creating a record.db.update_execution_status()(task_execution_service.py:252).update_execution_status()(db/schedules.py:591) runsSELECT started_at FROM schedule_executions WHERE id = ?.Under normal conditions, step 5 finds the row. But with two separate processes doing short-lived
sqlite3.connect()calls against the same file via a Docker volume mount (no persistent connection, no WAL), there are visibility windows where:Compounding issue: silent failure
db.update_execution_status()returnsFalsewhen the row isn't found (db/schedules.py:593), butTaskExecutionServicenever checks this return value — at lines 252, 295, 329, and 352 the return value is discarded. So the failure is completely silent: no log, no error, no retry.Same problem in error paths
When the backend call itself fails (e.g., agent container unreachable), the scheduler catches the exception and calls its own
self.db.update_execution_status()(service.py:649). This writes to the same file, but the backend'sTaskExecutionServicehas already attempted (and silently failed) its own status update within the failed HTTP call. The two processes race to update the same row.Confirmed fix: Option A from the issue
Stop the scheduler from creating execution records. Let
TaskExecutionServicealways create the record in its own transaction:self.db.create_execution()call. Passschedule_id,agent_name,message,triggered_by,modelto the backend. Don't passexecution_id.InternalTaskExecutionRequest: Addschedule_idfield.if not execution_idgate). Passschedule_idthrough todb.create_task_execution().execution_idto the scheduler so it can use it for WebSocket events and run-time updates.service.py:649): Removeself.db.update_execution_status()— the backend owns all execution record lifecycle.This eliminates the dual-write entirely — only the backend writes execution records, and its updates always target rows it created.
vybe commentedon Mar 12, 2026
Fix Implemented
The bug has been fixed in this commit. However, the root cause analysis in the issue was incorrect.
Corrected Root Cause
There is NO database split. Both the scheduler and backend use the same SQLite database (
/data/trinity.db):database_path = os.getenv("DATABASE_PATH", "/data/trinity.db")(src/scheduler/config.py:18)DB_PATH = Path(os.environ.get("TRINITY_DB_PATH", "/data/trinity.db"))(src/backend/db/connection.py:8)The actual bug was in
TaskExecutionService.execute_task():execution_idis provided (scheduler case), the service correctly skips creating a new record since it already existstryblock that caught exceptions and updated execution status to FAILED only started after slot acquisition (step 4)db.get_max_parallel_tasks()orslot_service.acquire_slot()(steps 2-3), the exception propagated up without updating the execution recordclaude_session_idandduration_msFix
src/backend/services/task_execution_service.py:slot_acquired = Falseflag to track stateif slot_acquired: slot_service.release_slot(...)src/backend/routers/internal.py:_execute_task_internal_background()TaskExecutionService, the handler now updates execution status to FAILED (prevents stuck executions from async dispatch)Modified Files
src/backend/services/task_execution_service.pysrc/backend/routers/internal.pydocs/memory/changelog.mddocs/memory/feature-flows/task-execution-service.mdfix: Prevent stuck executions on slot acquisition failure (#90)