Skip to content

Start a refused retry timer again instead of failing the workflow on replay - #484

Merged
ybastide merged 1 commit into
mainfrom
fix/retry-timer-start-failed-replay
Nov 28, 2026
Merged

ybastide merged 1 commit into
mainfrom
fix/retry-timer-start-failed-replay

Conversation

@ybastide

Copy link
Copy Markdown
Contributor

What

A workflow no longer fails on replay when SWF refuses the timer behind a delayed retry. A delayed retry is on_task_failure returning decide_retry(retry_wait_timeout=...), which starts the timer __simpleflow_task_<task id>.

  • simpleflow/swf/executor.py, do_handle_failure:
    • reads timer["control"] only for a fired or canceled timer, where it is used.
    • for a refusal SWF may accept later (TIMER_CREATION_RATE_EXCEEDED, OPEN_TIMERS_LIMIT_EXCEEDED), it falls through to the normal failure handling. on_task_failure decides again, and the executor starts the retry timer again.
    • for any other cause (TIMER_ID_ALREADY_IN_USE, OPERATION_NOT_PERMITTED), it treats the task as failed, the way an abort decision does. If the task raises on failure, the workflow fails with TaskFailed(timer_id, cause). Otherwise the decider gets the task's failed future.
  • simpleflow/history.py, parse_timer_event: a start_failed now records its own decision_task_completed_event_id, as a started timer already does.

Why

do_handle_failure decoded timer["control"] before looking at timer["state"]. A timer that SWF refused with no earlier TimerStarted is stored with no control. So every replay raised KeyError('control'), and the executor failed the execution with Cannot replay the workflow: KeyError('control').

The start_failed branch below it was never reached. It would have failed the execution anyway: it raised a bare TaskFailed, which replay also reports as "Cannot replay the workflow". Its arguments were ("timer", id, cause); they are now (id, cause), as in _get_future_from_timer_event.

There was a second bug, found while testing later retries. When a start_failed arrived for an id already in the history, parse_timer_event kept the decision id of the earlier timer. On a second or later retry, that decision is older than the latest failure. So find_timer_associated_with ignored the refused timer, and the executor started it again whatever the cause. In 0.39.0, a refusal therefore crashed the workflow on the first retry, and on later retries it was re-issued forever even when the cause was permanent.

#482 fixed the same kind of KeyError for plain timers. This is the retry-delay path, which it did not touch.

Retry the refused timer, or fail the task?

This PR retries on the two transient causes and fails on the others.

  • Precedent. A refused ScheduleActivityTask (ActivityTaskScheduleFailed) is already rescheduled on the next decision, whatever its cause. Failing a long workflow because SWF throttled timer creation for a moment would be worse than retrying.
  • Same id, not a new one. The timer starts again under the same __simpleflow_task_<task id>:
    • SWF refuses a timer id only while a timer with that id is open, and the refused one never opened.
    • Successive retries of one task already reuse this id once the previous timer has fired.
    • find_timer_associated_with looks the timer up by this fixed id, so a new id would also mean rewriting that lookup.
  • Risk. Each StartTimerFailed triggers a new decision at once, and that decision re-issues the timer. If SWF keeps refusing, this loops at about four history events per cycle, toward the 25,000-event limit. The activity precedent behaves the same way, and this PR adds no backoff.

Tests

  • Two replay tests in tests/test_simpleflow/swf/test_executor.py, built with HistoryBuilder.add_timer_start_failed: an activity fails, the workflow asks for a delayed retry, and SWF refuses the retry timer. Each runs with no earlier retry and with one earlier full retry cycle.
    • TIMER_CREATION_RATE_EXCEEDED: the replay starts the same timer again, with the same id and control. After that timer fires, the activity is scheduled again.
    • OPERATION_NOT_PERMITTED: the replay fails the workflow with Workflow execution error in activity-tests.data.activities.raise_on_failure: "OPERATION_NOT_PERMITTED".
  • Results on main without this change:
    • with no earlier retry, both tests fail on KeyError('control');
    • with one earlier retry, the OPERATION_NOT_PERMITTED test gets a StartTimer.
  • With only the history.py change reverted, the OPERATION_NOT_PERMITTED test still gets a StartTimer after one earlier retry.
  • Full suite as CI runs it (uv run --frozen --all-extras --group=dev pytest):
    • with this change: 452 passed, 10 skipped, 3 xfailed, 4 subtests passed;
    • on main: 450 passed, 10 skipped, 3 xfailed.
  • If test_executor.py runs on its own, three moto-backed tests in it fail with UnrecognizedClientException (test_submit_resolves_priority and the two jumbo-field tests). They fail the same way on main, and they pass in the full suite.
  • uvx prek run --all-files is clean.

Found through the AI review of a Botify dependency bump to 0.39.0. That project retries some activities with a delay, and it needs a release carrying this fix.

🤖 Generated with Claude Code

…replay

When on_task_failure asks for a delayed retry, the executor starts the
timer __simpleflow_task_<task id> and, on later replays, looks at it in
do_handle_failure. That method decoded timer["control"] before looking
at timer["state"]. If SWF refused the timer (StartTimerFailed, e.g.
TIMER_CREATION_RATE_EXCEEDED), History.parse_timer_event records a
start_failed entry with no "control", so every replay raised KeyError
and the executor failed the execution with "Cannot replay the workflow:
KeyError('control')". The start_failed branch below was never reached,
and it would have failed the execution all the same: it raised a bare
TaskFailed, which replay also turns into "Cannot replay the workflow".

Read "control" only where it is used, for a fired or canceled timer.

For a refusal SWF may accept later (TIMER_CREATION_RATE_EXCEEDED,
OPEN_TIMERS_LIMIT_EXCEEDED), fall through to the normal failure
handling: on_task_failure decides again and the executor starts the
retry timer again, under the same id. SWF only refuses an id while a
timer with it is open, the refused one never opened, and successive
retries already reuse that id once the previous timer fired. This
mirrors ActivityTaskScheduleFailed, whose activity is scheduled again
on the next decision.

For any other cause (TIMER_ID_ALREADY_IN_USE, OPERATION_NOT_PERMITTED),
treat the task as failed the way an abort decision does: raise the
task's exception with TaskFailed(timer_id, cause) when it raises on
failure, else return its failed future.

History.parse_timer_event kept the decision_task_completed_event_id of
an existing entry when a start_failed arrived for the same id. On a
second or later retry the entry still held the first timer's decision,
which is older than the latest failure, so find_timer_associated_with
ignored the refused timer and the executor started it again whatever
the cause. A start_failed now records its own decision, as a started
timer already does.

The two new replay tests (an activity that fails with a delayed retry,
then a refused retry timer) run with no earlier retry and with one. On
the previous code, a refusal of the first retry timer fails with
KeyError('control') for both causes, and a permanent refusal on the
second retry starts the timer again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ybastide
ybastide merged commit c049dfe into main Nov 28, 2026
8 checks passed
ybastide added a commit that referenced this pull request Nov 29, 2026
…used

When SWF refused the StartTimer of a refused activity or child
workflow, resume_refused_task scheduled the task again at once. Under
OPEN_ACTIVITIES_LIMIT_EXCEEDED plus OPEN_TIMERS_LIMIT_EXCEEDED that is
a loop: task refused, timer refused, task refused..., two decisions per
turn, until a limit clears or the execution hits its event cap.

Now, by the timer's cause:

- OPEN_TIMERS_LIMIT_EXCEEDED: the limit is per execution (1,000 open
  timers), so this execution's own timers will fire. The decision that
  the refusal brings waits (pending future, no decision); a later
  decision, i.e. one after a DecisionTaskCompleted that follows the
  refusal, starts the same timer again. A decision task timeout adds no
  DecisionTaskCompleted, so it waits again.
- TIMER_CREATION_RATE_EXCEEDED: start the same timer again at once.
  This is an account-wide token bucket (StartTimer: 2,000, refilled at
  200/s); nothing else is sure to wake the execution, so this stays a
  loop paced by decision round trips while SWF throttles. Same choice
  as the failure-retry timer (#484).
- anything else (OPERATION_NOT_PERMITTED, TIMER_ID_ALREADY_IN_USE): a
  failed attempt, through the same path and attempt count as a
  permanent refusal. TaskFailed keeps the task's refusal cause as
  reason; details name the timer and its cause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ybastide added a commit that referenced this pull request Nov 29, 2026
…imers limit

do_handle_failure treats a refused failure-retry timer
(__simpleflow_task_<id>) with a cause in
TRANSIENT_START_TIMER_FAILED_CAUSES as never started, and asks
on_task_failure again, which starts the same timer again in the same
decision. For OPEN_TIMERS_LIMIT_EXCEEDED that is a loop, timer refused,
timer started again, refused..., until a timer of the execution happens
to fire between two turns.

The limit is per execution (1,000 open timers), so this execution's own
timers will fire and bring a decision. Now the decision that the
refusal brings returns a pending future and no decision; a later one,
i.e. after a DecisionTaskCompleted that follows the refusal, starts the
timer again as before. A decision task timeout adds no
DecisionTaskCompleted, so it waits again. TIMER_CREATION_RATE_EXCEEDED
still starts it again at once, as #484 chose.

Same rule as for the backoff timer of a refused task, through the same
decided_since helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

1 participant