Skip to content

Conversation

@nathadfield
Copy link
Collaborator

@nathadfield nathadfield commented Jan 2, 2026

Summary

Closes #60049

The bug was in the task runner's DagRunTriggerException handler, which checked the deferrable flag before checking wait_for_completion. This caused tasks with deferrable=True to incorrectly enter DEFERRED state even in fire-and-forget mode (wait_for_completion=False).

Root Cause

In task_runner.py, the handler for DagRunTriggerException was structured like:

if drte.deferrable:
    # Create and return TaskDeferred
    ...
if drte.wait_for_completion:
    # Wait logic
    ...

This meant that tasks would defer based solely on the deferrable flag, ignoring wait_for_completion.

Fix

Nested the deferrable check inside the wait_for_completion check:

if drte.wait_for_completion:
    if drte.deferrable:
        # Create and return TaskDeferred
        ...

Now tasks only defer when BOTH deferrable=True AND wait_for_completion=True.

Changes

  • task-sdk/src/airflow/sdk/execution_time/task_runner.py: Fixed the exception handler logic
  • task-sdk/tests/task_sdk/execution_time/test_task_runner.py: Updated test expectations to validate correct behavior

The bug was in the task runner's DagRunTriggerException handler, which
checked the deferrable flag before checking wait_for_completion. This
caused tasks with deferrable=True to incorrectly enter DEFERRED state
even in fire-and-forget mode (wait_for_completion=False).

Fixed by nesting the deferrable check inside the wait_for_completion
check, ensuring tasks only defer when BOTH conditions are true.

Also updated test expectations to validate the correct behaviour.
@nathadfield nathadfield force-pushed the fix/deferred_trigger_dag branch 2 times, most recently from 1891ea3 to 2b7eb2c Compare January 5, 2026 11:07
@nathadfield nathadfield requested a review from shahar1 January 6, 2026 08:26
@nathadfield
Copy link
Collaborator Author

@kaxil @ashb Would either of you be able to take a look at this?

@uranusjr
Copy link
Member

uranusjr commented Jan 7, 2026

Is it ever useful for wait_for_completion=True to enter defer mode? Since it is fire-and-forget, there’s nothing to do after the run has been triggered successfully, and the task can simply end. Maybe it’d be a good idea to just ignore deferrable?

@nathadfield
Copy link
Collaborator Author

@uranusjr Setting wait_for_completion=True means that the task doesn't end until the DAG that was triggered finishes so, yes, deferring the task does makes sense in that context. The issue is when deferrable=True AND wait_for_completion=False does the task get stuck in deferred.

@uranusjr
Copy link
Member

uranusjr commented Jan 7, 2026

Sorry I mistyped. I meant deferrable=True can perhaps be ignored when wait_for_completion=False since there’s nothing to resume to anyway?

@nathadfield
Copy link
Collaborator Author

@uranusjr Yes, that's exactly the change I am proposing! Initially I modified the operators __init__ but looking more deeply I discovered that it could be resolved in the DagRunTriggerException instead.

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically no problem. I’d recommend adding a log message (warning or at leat info) when deferrable=True is ignored so the user has better idea what happened.

@nathadfield
Copy link
Collaborator Author

Logically no problem. I’d recommend adding a log message (warning or at leat info) when deferrable=True is ignored so the user has better idea what happened.

Ok, will do!

@nathadfield nathadfield force-pushed the fix/deferred_trigger_dag branch from 2b7eb2c to 551f04e Compare January 7, 2026 09:30
@nathadfield
Copy link
Collaborator Author

@uranusjr Thanks for approving!

@ashb ashb added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Jan 7, 2026
@ashb ashb added this to the Airflow 3.1.7 milestone Jan 7, 2026
@ashb ashb merged commit 2daadf4 into apache:main Jan 7, 2026
100 checks passed
@github-actions
Copy link

github-actions bot commented Jan 7, 2026

Backport failed to create: v3-1-test. View the failure log Run details

Status Branch Result
v3-1-test Commit Link

You can attempt to backport this manually by running:

cherry_picker 2daadf4 v3-1-test

This should apply the commit to the v3-1-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue

dstandish pushed a commit to astronomer/airflow that referenced this pull request Jan 7, 2026
apache#60052)

The bug was in the task runner's DagRunTriggerException handler, which
checked the deferrable flag before checking wait_for_completion. This
caused tasks with deferrable=True to incorrectly enter DEFERRED state
even in fire-and-forget mode (wait_for_completion=False).

Fixed by nesting the deferrable check inside the wait_for_completion
check, ensuring tasks only defer when BOTH conditions are true.

Also updated test expectations to validate the correct behaviour.
),
method_name="execute_complete",
)
return _defer_task(defer, ti, log)
if drte.wait_for_completion:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @nathadfield

chirodip98 pushed a commit to chirodip98/airflow-contrib that referenced this pull request Jan 9, 2026
apache#60052)

The bug was in the task runner's DagRunTriggerException handler, which
checked the deferrable flag before checking wait_for_completion. This
caused tasks with deferrable=True to incorrectly enter DEFERRED state
even in fire-and-forget mode (wait_for_completion=False).

Fixed by nesting the deferrable check inside the wait_for_completion
check, ensuring tasks only defer when BOTH conditions are true.

Also updated test expectations to validate the correct behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch provider:standard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TriggerDagRunOperator defers even when wait_for_completion=False

5 participants