Repository navigation
Replay a raced timer cancel and an in-flight or refused signal correctly - #485
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two replay fixes for failure events. Both histories replayed without error, but their meaning was lost.
simpleflow/history.py,parse_timer_event,cancel_failedbranch:started(or unseen) timer becomescancel_failed. Afired,canceledorstart_failedtimer keeps its state.cancel_failed_cause, so it no longer overwrites astart_failedtimer'scause._get_future_from_timer_eventreadscancel_failed_causefor acancel_failedtimer.simpleflow/swf/executor.py:find_signal_event: when a signal has not been delivered, it falls back on the latest matching send inhistory.external_workflows_signaling. A send still in flight gives a running future. A refused send gives aTaskFailed(signal_name, cause)future.propagate_signals: it skips targets with a send in flight or refused, not only those already signaled.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'sSignalExternalWorkflowExecutionFailedCauseenum. The other two areUNKNOWN_EXTERNAL_WORKFLOW_EXECUTIONandOPERATION_NOT_PERMITTED.get_future_from_external_workflow_event: thesignal_execution_failedbranch readevent["name"]. The entry hassignal_name, so this would have raisedKeyError('name'). It now readssignal_name.simpleflow/swf/mapper/models/history/builder.py:SignalExternalWorkflowExecutionInitiated,ExternalWorkflowExecutionSignaledandSignalExternalWorkflowExecutionFailed.add_timer_cancelednow setsdecisionTaskCompletedEventId. 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
CancelTimerFailedwithTIMER_ID_UNKNOWN. The parser then overwrote the timer'sfiredstate withcancel_failed. As a result:TimerTaskfuture becameTaskFailedinstead of finished, and so did theCancelTimerTaskfuture;do_handle_failure;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_eventfound a sent signal only onceExternalWorkflowExecutionSignaledwas recorded. Signal delivery is asynchronous:SignalExternalWorkflowExecutionInitiatedis 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, soresume()sent it again on every decision._idempotent_tasks_to_submitdeduplicates only within one decision.propagate_signalshad the same gap. The effects:UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION) re-sent on every decision.This also left the initiated and failed branches of
get_future_from_external_workflow_eventas dead code.Choices
futures.waitstill blocks on a running future, the same way it blocks on a pending one.Nonefromfutures.wait:waitreturns.result, which does not raise for a failed future. To see the refusal, checkfuture.exception.start_failedtimers 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 indo_handle_failurereadsstate == "start_failed"andcause, so a later cancel refusal must not change either.causekey. It hascancel_failed_cause, visible throughget_event_details("timer", …). Botify does not readcauseon timers.handle_cancel_requested, including itscancel_failed_decision_task_completed_event_idcheck, handles the workflow-levelWorkflowExecutioncancel_failedfromparse_workflow_event. It is unrelated to timers and unchanged.Tests
New replay tests in
tests/test_simpleflow/swf/test_executor.py, built withsimpleflow.swf.mapper.models.history.builder.History:TestCaseNotNeedingDomain):TIMER_ID_UNKNOWN). Run withStartTimerWorkflowandCancelTimerWorkflow: both futures are finished with no exception, no decision other thanCompleteWorkflowExecution, the state staysfired,cancel_failed_causeis set, and there is nocause.canceled.start_failedandcausekeeps the start cause.TaskFailed) is unchanged and still passes.TestSentSignalReplay):SignalExternalWorkflowExecutiondecision, and the future is not done.UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION: no new decision, and aTaskFailed("a_signal", cause)future.main.UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION, and sent again after a rate-limit refusal.Results:
main, with only the new tests and builder helpers:mainre-sends everything;TRANSIENT_SIGNAL_FAILED_CAUSESemptied, the 2 rate-limit cases fail.uv run --frozen --all-extras --group=dev pytest): 459 passed, 10 skipped, 3 xfailed.test_executor.pyruns on its own, the three moto-backed tests fail withUnrecognizedClientException, as noted in Start a refused retry timer again instead of failing the workflow on replay #484. They fail the same way onmain, and they pass in the full suite.uvx prek run --all-filesis clean.git merge-tree). On the merged tree,test_executor.pypasses apart from those three moto tests.Not addressed
These come from the same audit of 0.39's failure-event replay paths:
find_signal_eventcheckshistory.signals[name]before the targetworkflow_id. A signal directed at another workflow is therefore skipped if this workflow received a signal of the same name.RecordMarkerFailed) is recorded again on every replay._get_future_from_marker_eventtestsstate == "failed", but the parser writesrecord_failed.ActivityTaskCanceledis parsed ascanceled, but the parser branch testscancelled. Not reachable today.schedule_failedandcancel_requestedactivity entries can lackname/id, soswf/stats/pretty.pyandswf/helpers.pycan raiseKeyError.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