Skip to content

Handle a shared task's failure once per task, not once per event - #488

Open
ybastide wants to merge 1 commit into
mainfrom
fix/handled-failures-per-task
Open

ybastide wants to merge 1 commit into
mainfrom
fix/handled-failures-per-task

Conversation

@ybastide

Copy link
Copy Markdown
Contributor

What

simpleflow/swf/executor.py, handle_failure: the handled_failures cache is keyed by (event id, swf_task.payload) instead of the event id alone. The payload is the task's activity, or its workflow class for a child workflow.

Why

Idempotent tasks with the same arguments get the same id, so two submissions of one activity in an execution share its history events. Since #486, propagated attributes stay on the task, so those two submissions can have different failure semantics.

Failure scenario: an idempotent activity is submitted once inside Group(..., raises_on_failure=False) and once standalone, where it is mandatory, and it fails for good.

  • If the optional submission is handled first, default_failure_handling returns the failed future and it is cached by event id. The mandatory submission hits the cache and gets that future instead of raising, so the workflow completes although a mandatory activity failed.
  • In the other order, the mandatory submission raises before anything is cached, and the workflow fails as it should.

Botify runs into this: two analysis features build the same idempotent base-reference activity, one of them inside an optional group.

Choices

  • The payload as the key, not raises_on_failure. on_task_failure can read any of the task's attributes, so the key is the object that holds them.
    • Tasks with no propagated attributes share the module-level Activity, or the workflow class. They keep sharing one cache entry, so on_task_failure is still called once per failure for them.
    • A task a canvas propagated attributes to has its own TaskActivity, or workflow subclass, and so its own entry.
  • Keyed by identity. Neither Activity nor a workflow class defines __eq__ or __hash__. The dict holds the payloads, so an id cannot be reused during the replay.
  • Behaviour change: two tasks with their own attributes that share a failed event now each call on_task_failure. They used to share one call.

Tests

tests/test_simpleflow/swf/test_executor.py, TestFailureOfASharedTask, replays of one idempotent activity that fails in the first decision and is used twice:

  • optional use, then mandatory use: the workflow fails;
  • mandatory use, then optional use (control): the workflow fails;
  • two uses sharing the Activity (control): on_task_failure is called once;
  • two uses inside two optional groups: on_task_failure is called once per task, with the same task id.

Results:

  • Before the fix: 2 of the 4 fail. Optional then mandatory completes the workflow, and the two-groups case gets 1 call instead of 2. The 2 controls pass.
  • Full suite as CI runs it (uv run --frozen --all-extras --group=dev pytest): 516 passed, 10 skipped, 3 xfailed.
  • uvx prek run --all-files is clean.

Not addressed

  • No release: this waits for the next one.

🤖 Generated with Claude Code

Executor.handle_failure cached its outcome by history event id only.
Idempotent tasks with the same arguments get the same id, so two
submissions of one activity in an execution share its events. Since
propagated attributes stay on the task (#486), those submissions can
differ: one inside Group(raises_on_failure=False), one standalone and
mandatory.

Failure scenario: the optional submission handles the terminal failure
first. default_failure_handling returns the failed future, which is
cached. The mandatory submission then hits the cache, gets the same
future, and never raises: the workflow completes although a mandatory
activity failed. In the other order the mandatory one raises before
anything is cached, so the workflow fails as it should.

The cache key is now (event id, payload). The payload is the task's
activity, or workflow class: shared by tasks with the same attributes,
their own TaskActivity or subclass once a canvas propagated attributes
to them. Neither defines __eq__ or __hash__, so they key by identity, and
the dict keeps them alive for the replay. Tasks that share the payload
still call on_task_failure once per failure; tasks with their own
attributes now each call it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Development

Successfully merging this pull request may close these issues.

1 participant