Repository navigation
Description
Apache Airflow version
2.7.0
What happened
I have the following DAG:
from __future__ import annotations
from datetime import datetime
from airflow.decorators import dag, task, task_group
from airflow.utils.trigger_rule import TriggerRule
@task
def get_records() -> list[str]:
return ["a", "b", "c"]
@task
def submit_job(record: str) -> None:
pass
@task
def fake_sensor(record: str) -> bool:
raise RuntimeError("boo")
@task
def deliver_record(record: str) -> None:
pass
@task(trigger_rule=TriggerRule.ONE_FAILED)
def handle_failed_delivery(record: str) -> None:
pass
@task_group(group_id="deliver_records")
def deliver_record_task_group(record: str):
(
submit_job(record=record)
>> fake_sensor(record=record)
>> deliver_record(record=record)
>> handle_failed_delivery(record=record)
)
@dag(
dag_id="demo_trigger_one_failed",
schedule=None,
start_date=datetime(2023, 1, 1),
)
def demo_trigger_one_failed() -> None:
records = get_records()
deliver_record_task_group.expand(record=records)
demo_trigger_one_failed()fake_sensoris simulating a task that raises an exception. (It could be a@task.sensorraising aAirflowSensorTimeout; it doesn't matter, the behavior is the same.)handle_failed_delivery'sTriggerRule.ONE_FAILEDmeans it is supposed to run whenever any task upstream fails. So whenfake_sensorfails,handle_failed_deliveryshould run.
But this does not work. handle_failed_delivery is skipped, and (based on the UI) it's skipped very early, before it can know if the upstream tasks have completed successfully or errored.
Here's what I see, progressively (see How to reproduce below for how I got this):
| started ... | skipped too early ... | fake sensor about to fail... | ... done, didn't run |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
If I remove the task group and instead do,
@dag(
dag_id="demo_trigger_one_failed",
schedule=None,
start_date=datetime(2023, 1, 1),
)
def demo_trigger_one_failed() -> None:
records = get_records()
(
submit_job(record=records)
>> fake_sensor.expand(record=records)
>> deliver_record.expand(record=records)
>> handle_failed_delivery.expand(record=records)
)then it does the right thing:
| started ... | waiting ... | ... done, triggered correctly |
|---|---|---|
![]() |
![]() |
![]() |
What you think should happen instead
The behavior with the task group should be the same as without the task group: the handle_failed_delivery task with trigger_rule=TriggerRule.ONE_FAILED should be run when the upstream fake_sensor task fails.
How to reproduce
-
Put the above DAG at a local path,
/tmp/dags/demo_trigger_one_failed.py. -
docker run -it --rm --mount type=bind,source="/tmp/dags",target=/opt/airflow/dags -p 8080:8080 apache/airflow:2.7.0-python3.10 bash -
In the container:
airflow db init airflow users create --role Admin --username airflow --email airflow --firstname airflow --lastname airflow --password airflow airflow scheduler --daemon airflow webserver -
Open
http://localhost:8080on the host. Login withairflow/airflow. Run the DAG.
I tested this with:
apache/airflow:2.6.2-python3.10apache/airflow:2.6.3-python3.10apache/airflow:2.7.0-python3.10
Operating System
Debian GNU/Linux 11 (bullseye)
Versions of Apache Airflow Providers
n/a
Deployment
Other Docker-based deployment
Deployment details
This can be reproduced using standalone Docker images, see Repro steps above.
Anything else
I wonder if this is related to (or fixed by?) #33446 -> #33732 ? (The latter was "added to the Airflow 2.7.1 milestone 3 days ago." I can try to install that pre-release code in the container and see if it's fixed.)
edit: nope, not fixed
Are you willing to submit PR?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct







Activity
benbuckman commentedon Sep 1, 2023
I was curious if #33732 (fe27031) fixes the same issue I'm describing here. That fix is on
main.(I'm at
3ae6b4e86fe807c00bd736c59df58733df2b9bf9)Nope – it exhibits the same incorrect behavior of skipping
handle_failed_deliverybefore the task group has finished, and not respecting thetrigger_rule.RNHTTR commentedon Sep 2, 2023
Seems as though it only affects mapped tasks. That is, it runs as expected if you replace
deliver_record_task_group.expand(record=records)withdeliver_record_task_group(record=records).RNHTTR commentedon Sep 2, 2023
Curiously, the GANTT chart shows the task group has been queued for 24 hours
RNHTTR commentedon Sep 2, 2023
Like @benbuckman reported, the task was skipped before
submit_joborfake_sensorran.debug logs:
[-]Trigger Rule ONE_FAILED does not work in task group[/-][+]Trigger Rule ONE_FAILED does not work in task group with mapped tasks[/+]benbuckman commentedon Sep 2, 2023
Yes, thanks @RNHTTR for that clarification. I updated the issue title accordingly.
hussein-awala commentedon Sep 4, 2023
@ephraimbuddy the problem is in the method
get_relevant_upstream_map_indexeswhere when we try to get the relevant map index for upstreamdeliver_recordand we call this methodwe call it with this values:
and this method doesn't take into account the mapped task group, so it return -1 instead of the same map index of the checked TI.
So we have two options:
get_relevant_upstream_map_indexesto make it handling mapped task groups.I'm already working on refactoring some queries in this class including the one which have this bug:
benbuckman commentedon Sep 5, 2023
Thanks @hussein-awala for digging into the fix so quickly.
Something else that's worth looking into and fixing here, is why unit tests with
DebugExecutorbehave differently. Take this unit test for example – simplified again for demonstration:Put that in a file
test_demo_trigger_one_failed.pynext to the file with the DAG above, and run it withpython -m unittest path/to/test_demo_trigger_one_failed.py.Note,
DebugExecutornotdag.test()because the latter cannot test error cases.fake_sensor_taskis in the DAG itself (contrived for demo); in a real test this would be stubbed.task_instance_statesat the end is shown to be:and the test passes, asserting that
handle_failed_deliverysucceeded. This is a misleading test output, because as we see above, in the real scheduler,handle_failed_deliverydoesn't run at all! (Itsti.current_state()should beNonenotsuccess.)We have unit tests like this in our application, and were confident that our actual task with
trigger_rule=ONE_FAILEDwould work correctly, and were very surprised when it broke in production.In the process of fixing this with the "real" executors (
SequentialExecutorin the simple demo above;KubernetesExecutoret al in a real production application), it would be great ifDebugExecutorbehaved at parity with the others, so users can rely on test coverage.Thank you!
36 remaining items
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix per-index evaluation of ONE_FAILED in mapped task groups (#67684)
[v3-2-test] Fix per-index evaluation of ONE_FAILED in mapped task gro…
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)
Fix pre-mature evaluation of tasks in mapped task group (#34337)