Skip to content

Prevent duplicate ECS submissions on task retry - #72421

Closed
1fanwang wants to merge 3 commits into
apache:mainfrom
1fanwang:ecs-resumable-retries
Closed

1fanwang wants to merge 3 commits into
apache:mainfrom
1fanwang:ecs-resumable-retries

Conversation

@1fanwang

@1fanwang 1fanwang commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

An Airflow retry can launch an ECS workload twice if its first ECS task has already succeeded. Reattachment searches running tasks, so it misses the completed one.

On Airflow 3.3+, non-deferrable execution now saves the task ARN before polling. A retry checks that task directly, resumes it if active, or recovers its successful result. Missing and failed tasks resubmit. Existing terminal checks still handle startup failures, nonzero exits and skip codes.

Recovery defaults on; durable=False disables it. Earlier Airflow versions keep non-durable execution. Deferrable execution keeps its existing path, and no-wait execution still skips polling. Logging, cancellation and XCom use the existing operator hooks.

Testing

The committed regression drives the real operator with a local ECS client and in-memory task state, not live AWS. The stored task has completed successfully; a retry must not submit again.

That test imports helpers added here, so it cannot run on upstream/main. The script below runs the same retry through the public operator on both refs, with only the ECS client and task state stubbed.

Recorded commands and raw logs

Before, on upstream/main (git checkout a99354726cec44718391a6b42172462f6c72046d):

$ uv run --project providers/amazon python - <<'EOF'
from unittest import mock

from airflow.providers.amazon.aws.hooks.ecs import EcsHook
from airflow.providers.amazon.aws.operators.ecs import EcsRunTaskOperator

arn = "arn:aws:ecs:us-east-1:123456789012:task/demo/0123456789abcdef"
container = {"name": "app", "lastStatus": "STOPPED", "exitCode": 0}
client = mock.MagicMock()
client.describe_tasks.return_value = {
    "failures": [],
    "tasks": [{"taskArn": arn, "lastStatus": "STOPPED", "containers": [container]}],
}
client.run_task.return_value = {"failures": [], "tasks": [{"taskArn": arn, "containers": [container]}]}


class TaskState(dict):
    def set(self, key, value):
        self[key] = value


# The first try already ran this ECS task to success and stored its ARN.
state = TaskState(ecs_task_arn=arn)
with mock.patch.object(EcsHook, "conn", client):
    op = EcsRunTaskOperator(task_id="run", task_definition="demo", cluster="demo", overrides={})
    op.execute({"ti": mock.MagicMock(stats_tags={}), "task_state_store": state})
print("RunTask calls on retry:", client.run_task.call_count)
EOF
RunTask calls on retry: 1

After, on this branch (git checkout 8ca04626e7df22cb16095ce29bfc5e77a40e4599), the same command:

RunTask calls on retry: 0

The committed regression on this branch:

$ uv run --project providers/amazon --locked pytest providers/amazon/tests/unit/amazon/aws/operators/test_ecs.py::TestEcsRunTaskOperator::test_retry_after_remote_success_does_not_submit_duplicate -xvs
========================= 1 passed, 1 warning in 2.29s =========================

The recorded pre-3.3 simulation exercised the fallback without task-state access; recovery-only cases were skipped. This does not establish live ECS behavior or validate an actual older Airflow installation.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: GitHub Copilot CLI (GPT-6 Astra) following the guidelines

ECS reattachment only finds running tasks, so a retry after the remote task finishes launches the workload again. Persisting the exact task ARN lets retries recognize completed work.

Generated-by: GitHub Copilot CLI (GPT-5.6 Sol)
Signed-off-by: 1fanwang <[email protected]>
@boring-cyborg boring-cyborg Bot added area:providers provider:amazon AWS/Amazon - related issues labels Sep 2, 2026
The Amazon provider compatibility matrix includes Airflow releases before task_state_store exists. Keep those releases on the intentional non-durable fallback while retaining their existing behavior coverage.

Generated-by: GitHub Copilot CLI (GPT-5.6 Sol)
Signed-off-by: 1fanwang <[email protected]>
@1fanwang 1fanwang changed the title Prevent duplicate ECS tasks after successful retries Prevent duplicate ECS submissions on task retry Sep 2, 2026
Main now builds the AwsLogsHook used after deferral with the operator's verify and botocore config. This branch moved that lookup into _get_last_log_message, so the helper now passes those settings too.

Signed-off-by: 1fanwang <[email protected]>
@potiuk potiuk added the closed because of open PR limit Closed as a one-time step of introducing the open pull request limit label Sep 25, 2026
@potiuk

potiuk commented Sep 25, 2026

Copy link
Copy Markdown
Member

Hello @1fanwang - thank you for your contributions to Apache Airflow!

The Airflow community has introduced a limit of 5 open pull requests at a time for contributors without write access to the repository. You currently have 34 open pull requests, so - as a one-time step of introducing the limit - we closed the ones where maintainers have not engaged yet:

These pull requests stay open because maintainers are already engaged in them - they count towards your limit:

This is not a judgement of you or of your changes. We never told contributors before that opening many pull requests at once was a problem, so there is nothing to feel bad about - and nothing is lost: your branches, commits and the review history stay where they are.

What we ask you to do is to make your first prioritization decision: choose which of the pull requests above matter most to you, and reopen them (up to 5 open at a time, including the ones still open) with the "Reopen pull request" button or gh pr reopen <PR_NUMBER> --repo apache/airflow. Reopen the ones you are ready to follow through - keep them rebased, respond to review comments and fix failing checks.

While your pull requests are waiting for review, the most valuable thing you can do is help in other ways - reviewing other contributors' pull requests, helping with issues, and taking part in the discussions on the devlist and Slack.

Why we introduced the limit, what it means for you and how to reopen or restore a pull request is explained in https://github.com/apache/airflow/blob/main/contributing-docs/32_open_pull_request_limit.rst.


Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting

@potiuk potiuk closed this Sep 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers closed because of open PR limit Closed as a one-time step of introducing the open pull request limit provider:amazon AWS/Amazon - related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants