Skip to content

fix: clear next_method and next_kwargs on task retry - #62853

Closed
YoannAbriel wants to merge 1 commit into
apache:mainfrom
YoannAbriel:fix/issue-62845
Closed

fix: clear next_method and next_kwargs on task retry#62853
YoannAbriel wants to merge 1 commit into
apache:mainfrom
YoannAbriel:fix/issue-62845

Conversation

@YoannAbriel

@YoannAbriel YoannAbriel commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

fix: clear next_method and next_kwargs on task retry

Problem

When a deferrable operator fails during trigger resumption and enters retry, next_method and next_kwargs are not cleared in Airflow 3.x. This means the retry attempt skips execute() entirely and jumps straight to the stale next_method(**next_kwargs) callback from the previous attempt.

This causes two failure modes:

  1. The task processes "zombie" trigger events from the previous attempt
  2. The task fails because initial setup logic in execute() was bypassed

This was a known issue in Airflow 2.x, fixed in #18146 / #18210 by nullifying next_method and next_kwargs on retry. With the transition to the Task SDK and Internal API in 3.x, the reset logic was not carried over to the new prepare_db_for_next_try() method.

Root Cause

TaskInstance.prepare_db_for_next_try() is the central method in 3.x for preparing a TI for its next attempt (generating new UUID, incrementing try_number, resetting state). It was missing a call to clear_next_method_args(), so the deferred execution state persisted across retries.

Fix

Added self.clear_next_method_args() in prepare_db_for_next_try(), right before the new attempt UUID is generated. This ensures the retry always starts fresh from execute().

One line change in airflow-core/src/airflow/models/taskinstance.py, plus two tests:

  • test_prepare_db_for_next_try_clears_next_method — verifies prepare_db_for_next_try() resets deferred fields
  • test_handle_failure_clears_next_method_on_retry — verifies the full handle_failure() path clears deferred state when retries remain

All 216 model tests + 127 execution API tests pass.

Closes: #62845


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code

Generated-by: Claude Code following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

@YoannAbriel
YoannAbriel requested review from XD-DENG and ashb as code owners March 4, 2026 10:14
When a deferred task fails and is eligible for retry, the next_method
and next_kwargs fields must be cleared so the retry starts fresh from
execute() rather than resuming from the stale deferral callback.

While the execution API route already clears these fields via a Core
UPDATE query, adding the clearing to prepare_db_for_next_try() provides
a defensive guarantee that any code path preparing a TI for retry will
properly reset the deferral state.

Closes: apache#62845
@tirkarthi

Copy link
Copy Markdown
Contributor

As confirmed by the user this is resolved as of Airflow 3.1.7. I am closing this PR. Please feel free to reopen if this can be reproduced.

#62845 (comment)

@tirkarthi tirkarthi closed this Mar 5, 2026
@YoannAbriel
YoannAbriel deleted the fix/issue-62845 branch March 5, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deferred TI's next_method and next_kwargs not cleared on retries in airflow 3.x

2 participants