Skip to content

fix: a retry stores its tasks before the workflow is running again - #1892

Open
grafke wants to merge 1 commit into
conductor-oss:mainfrom
grafke:fix/retry-writes-tasks-before-running
Open

grafke wants to merge 1 commit into
conductor-oss:mainfrom
grafke:fix/retry-writes-tasks-before-running

Conversation

@grafke

@grafke grafke commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Pull Request type

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • WHOSUSING.md
  • Other (please describe):

NOTE: Please remember to run ./gradlew spotlessApply to fix any format violations.

Changes in this PR

A retry of a workflow that failed on one branch of a dynamic fork could return 204 and fail again at once, with the rescheduled branch CANCELED before it started.

retry stored the workflow as RUNNING and only then its task changes: the failed task marked retried, its copy, and the reopened JOIN. A decide in between, from the sweeper or an expedited decide, saw a RUNNING workflow whose failed task was not yet marked retried and failed it again. A JOIN evaluated before the copy had its seq found the original failed branch and failed too. A CANCELED JOIN was queued before anything was stored.

retry now writes in this order:

  1. Queue the decider message, with the workflow offset.
  2. Store the task changes, with the copies already numbered. The seq logic moved out of scheduleTask into a small assignSeq helper, unchanged for the other callers.
  3. Store RUNNING.
  4. Queue the decider message again, also when the RUNNING write throws after storing.
  5. Queue the copies (scheduleTask) and the reopened JOIN, EXCLUSIVE_JOIN and DO_WHILE tasks.

Until step 3 the workflow is terminal. The default sweeper drops a terminal workflow's message without deciding it, and retry has queued no task, so nothing acts on the half-written state. Because the message is queued first, a retry that cannot queue it stores nothing. A sweeper that read the workflow while it was still terminal can remove that first message, which is why it is queued again after step 3. Both pushes wait out the workflow offset (30 s by default), so the decide they trigger comes after the writes finish. The old comment about archived workflows does not hold: no ExecutionDAO.updateTask needs the workflow row, so the order works the same for them.

On a server with Postgres persistence, queue, lock and indexing, with 3 ms of added latency on Postgres, retrying race_parent from the issue 8 at a time:

retries rescheduled branch lost RUNNING without a decider message when the retry returned
main 240 203 not measured
this PR, first push only 240 0 210
this PR 240 0 0

These tests fail without the change: testRetryStoresTheRescheduledTasksBeforeTheWorkflowIsRunning, testRetryQueuesAReopenedJoinOnlyAfterTheRescheduledTasksAreStored and testRetryQueuesTheDeciderMessageBeforeTheWorkflowIsRunning. testRetryRequeuesTheDeciderMessageASweeperRemovedDuringItsTaskWrites fails without the second push, and testRetryRequeuesTheDeciderMessageWhenUpdateWorkflowFailsAfterItsWrite fails if a failed RUNNING write skips it. They run the real DeciderService, Join, AsyncSystemTaskExecutor and WorkflowSweeper against the stored state at each write.

Not in this PR:

  • A retry interrupted between steps 2 and 3 leaves the workflow FAILED, with the copy SCHEDULED and the JOIN IN_PROGRESS, and a second retry answers "There are no retryable tasks". PUT /api/workflow/decide/{workflowId} cancels those tasks, and the next retry works. On main, an interruption after the RUNNING write left a RUNNING workflow with no decider message, which retry refused as still running.
  • A remove of the decider message that lands after the second push, from the decide that failed the workflow or from a sweeper that read it as FAILED, can still leave the workflow RUNNING without a message. The live runs did not hit it. Closing it needs a remove that only deletes the message it took, in every queue backend.
  • With the legacy sweeper, or an explicit decide, during steps 2 to 3, the decide cancels the stored copies, as it would on main.

Fixes #1843

Alternatives considered

  • Holding the workflow execution lock across retry. The lock does nothing unless a lock type is configured, it does not stop the JOIN system task, and terminateWorkflow releases it before its final remove of the decider message. Ordering the writes fixes the race with or without a lock, as was done for rerun.
  • Writing the task changes after RUNNING but making readers skip a workflow under retry. That needs a new marker that every reader checks.

retry stored the workflow as RUNNING and only then its task changes:
the failed task marked retried, its copy, and the reopened JOIN. A
decide in between, from the sweeper or an expedited decide, failed the
workflow again on the failed task, and a JOIN evaluated before the copy
had its seq failed on it too. The copy was then canceled before it
started. A canceled JOIN was even queued before anything was stored.

retry now queues the decider message, stores the task changes with the
copies already numbered, stores RUNNING, queues the decider message
again, and then queues the copies and the reopened tasks. Until RUNNING
the workflow is terminal: the default sweeper drops it without deciding,
and retry has queued no task. Because the message is queued first, a
retry that cannot queue it stores nothing. A sweeper that read the
workflow while it was terminal can remove that message, so it is queued
again after the RUNNING write, also when that write fails after storing.
Both wait out the workflow offset, so a decide does not come before the
writes finish. No ExecutionDAO needs the workflow row to store a task,
so archived workflows are unaffected.

On a server whose workflows, queues, locks and index share one SQL
database with 3 ms of added latency, retrying a dynamic fork whose
branch failed, 8 at a time, lost the rescheduled branch in 203 of 240
retries; with the change, in 0 of 240. With only the first message,
210 of 240 workflows were RUNNING without one when the retry returned;
with both, none were.
testRetryStoresTheRescheduledTasksBeforeTheWorkflowIsRunning,
testRetryQueuesAReopenedJoinOnlyAfterTheRescheduledTasksAreStored and
testRetryQueuesTheDeciderMessageBeforeTheWorkflowIsRunning fail without
the change.
testRetryRequeuesTheDeciderMessageASweeperRemovedDuringItsTaskWrites
fails without the second message, and
testRetryRequeuesTheDeciderMessageWhenUpdateWorkflowFailsAfterItsWrite
fails if a failed RUNNING write skips it.

Fixes conductor-oss#1843

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Retry of a failed dynamic fork sometimes re-fails at once and cancels the rescheduled branch unstarted

1 participant