#1130

Workflow thread reset can delete checkpoints of a concurrent fire of the same workflow

Openaryanranderiyaopened 3mo ago

Summary

reset_workflow_threads (introduced in #1129) deletes a workflow conversation's LangGraph checkpoint threads before each fire so a run starts clean. It skips threads that have a write in flight, but that check is a snapshot: a second fire of the same workflow that starts writing between the snapshot and the delete has its checkpoints removed from under it. Two fires of one workflow can overlap today because manual and scheduled fires are dispatched under different ARQ job ids and only the scheduled path goes through a claim gate.

Raised by CodeRabbit on #1129 (apps/api/app/services/workflow/thread_reset.py, review thread on line 79). Deferred out of that PR because it is a scheduler-level concern shared by every trigger, not a playbook one.

Where

  • apps/api/app/services/workflow/thread_reset.py — reset_workflow_threads(conversation_id):
    1. SELECT DISTINCT thread_id FROM checkpoints WHERE … (the conversation's thread, the executor's, each subagent's)
    2. SELECT DISTINCT w.thread_id FROM checkpoint_writes w WHERE … checkpoint_id = max(...) → inflight
    3. for thread_id in deletable: await checkpointer.adelete_thread(thread_id) Steps 2 and 3 are not atomic. A run that starts between them is not in inflight and loses its threads.
  • Called once per fire from apps/api/app/workers/tasks/workflow_tasks.py (execute_workflow_by_id, await reset_workflow_threads(conversation_id)), before the run.
  • Why two fires can overlap:
    • Scheduled fires: WorkflowScheduler._build_job_args arms an ARQ job keyed by (workflow_id, scheduled_for) and claim_scheduled_for_execution refuses a fire whose stamp no longer matches trigger_config.next_run.
    • Manual fires: WorkflowQueueService.queue_workflow_execution enqueues execute_workflow_by_id under a different deterministic job id (sha256 of workflow + user + context) and does not go through the claim. Integration-triggered fires likewise.
    • So "run now" landing while a scheduled fire is in progress (or vice versa) runs concurrently. The per-conversation executor busy lock (executor:busy:<conv>) serialises the executor part of both runs, but the reset happens before that lock is taken.

Reproduction (shape)

  1. A workflow with a scheduled fire in progress whose executor is mid-run (checkpoints being written to executor_<conv>).
  2. Trigger POST /api/v1/workflows/{id}/execute at the same moment.
  3. The manual fire's reset_workflow_threads snapshots inflight just before the scheduled run's first write lands on a new thread (e.g. the first handoff subagent's <ns>_executor_<conv>), then deletes it.
  4. The scheduled run's next checkpoint write fails or the subagent resumes from nothing; the run degrades or repeats a side effect.

The window is small (tens of milliseconds) and needs two fires within the same second, which is why it has not been observed live, but nothing prevents it.

Proposed fix

Serialise the reset with the run it belongs to, using the lock primitives that already exist rather than a new mechanism:

  • Take a per-conversation lock around snapshot + delete + run start. app/agents/core/background/executor_queue.py already has try_acquire_lock / release_lock_if_owned / build_lock_value on Redis for the executor busy lock; a sibling key such as workflow:reset:<conversation_id> with a short TTL, acquired in execute_workflow_by_id before reset_workflow_threads and released after the run has taken the executor busy lock, closes the window.
  • Alternatively (bigger, arguably cleaner): route manual and integration fires through the same claim gate as scheduled fires (claim_scheduled_for_execution or a general claim_for_execution), so at most one fire of a workflow is ever in flight and the reset needs no lock at all. This also answers the "execution idempotency across overlapping fires" point raised on #1129's record_run_outcome thread.

Whichever is chosen, a fire that loses the race should be queued or recorded as queued, never silently dropped and never run concurrently. The existing WorkflowFireQueued path (execution_service.py) is the right shape for "this fire did not run".

Acceptance criteria

  • Two fires of one workflow dispatched within the same second never both run reset_workflow_threads while the other is between snapshot and delete; the second either waits or is recorded as queued.
  • A checkpoint write that lands after the first fire's snapshot is never deleted by that fire.
  • Nothing changes for the single-fire case (the reset still runs, still skips in-flight threads, still never raises).
  • threads_reset / threads_skipped_inflight in the wide event stay accurate; add the lock outcome (acquired / waited / queued) as a field.

Tests

  • tests/stress/ (in-process fakes, no sleeps): two concurrent execute_workflow_by_id for one workflow with a fake checkpointer that records deletes and writes with a logical clock; assert no delete targets a thread written after that fire's snapshot.
  • tests/unit/workers/: the second fire takes the queued path when the reset lock is held.
  • tests/integration/real/: the existing thread-reset suite (tests/unit/services/workflow/test_thread_reset.py plus the real-Postgres cases) keeps passing unchanged.

Out of scope

The nightly prune uses the same "write parked on the head checkpoint" condition; it is not affected because it never races a fire of the same conversation by design.

Activity

0

No activity yet.