Skip to content

Replay a raced timer cancel and an in-flight or refused signal correctly - #485

Merged
ybastide merged 1 commit into
mainfrom
fix/signal-and-cancel-failed-replay
Nov 28, 2026
Merged

ybastide merged 1 commit into
mainfrom
fix/signal-and-cancel-failed-replay

Conversation

@ybastide

Copy link
Copy Markdown
Contributor

What

Two replay fixes for failure events. Both histories replayed without error, but their meaning was lost.

  • A timer cancel that loses the race with the fire. simpleflow/history.py, parse_timer_event, cancel_failed branch:
    • only a started (or unseen) timer becomes cancel_failed. A fired, canceled or start_failed timer keeps its state.
    • the refusal's cause is stored as cancel_failed_cause, so it no longer overwrites a start_failed timer's cause. _get_future_from_timer_event reads cancel_failed_cause for a cancel_failed timer.
  • A sent signal that is in flight, or that SWF refused. simpleflow/swf/executor.py:
    • find_signal_event: when a signal has not been delivered, it falls back on the latest matching send in history.external_workflows_signaling. A send still in flight gives a running future. A refused send gives a TaskFailed(signal_name, cause) future.
    • propagate_signals: it skips targets with a send in flight or refused, not only those already signaled.
    • The exception is SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_RATE_EXCEEDED, which is sent again, as Start a refused retry timer again instead of failing the workflow on replay #484 does for transient timer refusals. The causes are taken from botocore's SignalExternalWorkflowExecutionFailedCause enum. The other two are UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION and OPERATION_NOT_PERMITTED.
    • get_future_from_external_workflow_event: the signal_execution_failed branch read event["name"]. The entry has signal_name, so this would have raised KeyError('name'). It now reads signal_name.
  • simpleflow/swf/mapper/models/history/builder.py:
    • new helpers for SignalExternalWorkflowExecutionInitiated, ExternalWorkflowExecutionSignaled and SignalExternalWorkflowExecutionFailed.
    • add_timer_canceled now sets decisionTaskCompletedEventId. The parser reads it, and SWF always sends it.

Why

Timer. A cancel decision and the timer's fire can cross: the timer fires, and the decider, working from an earlier history, cancels it. SWF answers CancelTimerFailed with TIMER_ID_UNKNOWN. The parser then overwrote the timer's fired state with cancel_failed. As a result:

  • the TimerTask future became TaskFailed instead of finished, and so did the CancelTimerTask future;
  • a delayed-retry timer in that state reached the "Unexpected timer state" branch of do_handle_failure;
  • callers that read get_event_details("timer", …)["state"] == "fired" missed the fire.

On 0.38 this history crashed the replay; on 0.39 it was misread without any error.

Signals. find_signal_event found a sent signal only once ExternalWorkflowExecutionSignaled was recorded. Signal delivery is asynchronous: SignalExternalWorkflowExecutionInitiated is recorded when the decision completes, and the signaled or failed event comes later. Until then, and forever after a refusal, the signal was not found, so resume() sent it again on every decision. _idempotent_tasks_to_submit deduplicates only within one decision. propagate_signals had the same gap. The effects:

  • duplicate signals during the in-flight window;
  • a signal to a closed workflow (UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION) re-sent on every decision.

This also left the initiated and failed branches of get_future_from_external_workflow_event as dead code.

Choices

  • In flight → running future, refused → failed future.
    • futures.wait still blocks on a running future, the same way it blocks on a pending one.
    • A refused send is not retried, apart from the rate-limit cause. A workflow that waits on the signal future gets None from futures.wait: wait returns .result, which does not raise for a failed future. To see the refusal, check future.exception.
  • Rate limit → send again. Failing for good on a momentary throttle would silently drop a signal that a re-send would deliver. The same loop risk as Start a refused retry timer again instead of failing the workflow on replay #484 applies: if SWF keeps refusing, each refusal triggers a decision that sends the signal again. There is no backoff.
  • start_failed timers keep their state too, not only fired or canceled ones. Start a refused retry timer again instead of failing the workflow on replay #484's transient-cause check in do_handle_failure reads state == "start_failed" and cause, so a later cancel refusal must not change either.
  • Interface change. A started timer whose cancel was refused no longer has a cause key. It has cancel_failed_cause, visible through get_event_details("timer", …). Botify does not read cause on timers.
  • handle_cancel_requested, including its cancel_failed_decision_task_completed_event_id check, handles the workflow-level WorkflowExecution cancel_failed from parse_workflow_event. It is unrelated to timers and unchanged.

Tests

New replay tests in tests/test_simpleflow/swf/test_executor.py, built with simpleflow.swf.mapper.models.history.builder.History:

  • Timers (TestCaseNotNeedingDomain):
    • started → fired → cancel refused (TIMER_ID_UNKNOWN). Run with StartTimerWorkflow and CancelTimerWorkflow: both futures are finished with no exception, no decision other than CompleteWorkflowExecution, the state stays fired, cancel_failed_cause is set, and there is no cause.
    • started → canceled → second cancel refused: the future is cancelled and the state stays canceled.
    • start refused → cancel refused: the state stays start_failed and cause keeps the start cause.
    • The existing started → cancel refused test (TaskFailed) is unchanged and still passes.
  • Signals (TestSentSignalReplay):
    • a directed signal in flight: no new SignalExternalWorkflowExecution decision, and the future is not done.
    • refused with UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION: no new decision, and a TaskFailed("a_signal", cause) future.
    • refused with the rate-limit cause: sent again.
    • delivered: the future is finished. This one is a control and also passes on main.
    • a propagated signal to a started child: not sent again while in flight or after UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION, and sent again after a rate-limit refusal.

Results:

  • On main, with only the new tests and builder helpers:
    • 8 of the new tests (or subtests) fail;
    • the 2 rate-limit cases pass, because main re-sends everything;
    • the delivered-signal control passes.
  • With this change, but TRANSIENT_SIGNAL_FAILED_CAUSES emptied, the 2 rate-limit cases fail.
  • Full suite as CI runs it (uv run --frozen --all-extras --group=dev pytest): 459 passed, 10 skipped, 3 xfailed.
  • If test_executor.py runs on its own, the three moto-backed tests fail with UnrecognizedClientException, as noted in Start a refused retry timer again instead of failing the workflow on replay #484. They fail the same way on main, and they pass in the full suite.
  • uvx prek run --all-files is clean.
  • The branch merges cleanly with Start a refused retry timer again instead of failing the workflow on replay #484 (git merge-tree). On the merged tree, test_executor.py passes apart from those three moto tests.

Not addressed

These come from the same audit of 0.39's failure-event replay paths:

  • find_signal_event checks history.signals[name] before the target workflow_id. A signal directed at another workflow is therefore skipped if this workflow received a signal of the same name.
  • A refused marker (RecordMarkerFailed) is recorded again on every replay. _get_future_from_marker_event tests state == "failed", but the parser writes record_failed.
  • A refused activity schedule is retried on every decision with no backoff, even for a permanent cause.
  • ActivityTaskCanceled is parsed as canceled, but the parser branch tests cancelled. Not reachable today.
  • CLI/stats only: schedule_failed and cancel_requested activity entries can lack name/id, so swf/stats/pretty.py and swf/helpers.py can raise KeyError.

The retry-delay timer refusal (KeyError('control')) is #484.

Found through the AI review of a Botify dependency bump to 0.39.0. That project needs a release that carries both this fix and #484.

🤖 Generated with Claude Code

A CancelTimerFailed overwrote the timer's state. A cancel that loses the
race with the fire (TIMER_ID_UNKNOWN) turned a fired timer into
cancel_failed, so both the TimerTask and the CancelTimerTask futures
replayed as TaskFailed instead of finished, and a retry timer in that
state hit do_handle_failure's "unexpected timer state" branch. Only a
started (or unseen) timer now becomes cancel_failed; a fired, canceled or
start_failed timer keeps its state. The refusal's cause goes to
cancel_failed_cause, so it no longer clobbers a start_failed timer's
cause.

A sent signal was found on replay only once ExternalWorkflowExecution-
Signaled was recorded. While it was in flight, or after SWF refused it,
find_signal_event returned nothing and resume() sent it again, on every
decision; propagate_signals had the same gap. A refused signal to a
closed workflow (UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION) was re-sent forever.
Both now fall back on the latest send in external_workflows_signaling:
in flight gives a running future (futures.wait still blocks on it),
refused gives a TaskFailed future, except a refusal for
SIGNAL_EXTERNAL_WORKFLOW_EXECUTION_RATE_EXCEEDED, which is sent again,
as #484 does for transient timer refusals. That reaches the initiated/failed
branches of get_future_from_external_workflow_event for the first time;
the failed one read event["name"], which the entry does not have, and
now reads signal_name.

The history builder gains SignalExternalWorkflowExecutionInitiated,
ExternalWorkflowExecutionSignaled and SignalExternalWorkflowExecution-
Failed helpers, and add_timer_canceled now sets the
decisionTaskCompletedEventId that the parser reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ybastide
ybastide merged commit 8d7270b into main Nov 28, 2026
8 checks passed
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