airflow: DAG on_failure_callback uses wrong context
Apache Airflow version
2.4.0
What happened
When a task fails in a DAG, the on_failure_callback registered while creating the dag is triggered using the context of a random task instance.
What you think should happen instead
The expectation is that one of the task instances that caused the dag failure should be used instead of a random task instance.
How to reproduce
Run the below dag.
import datetime
from airflow.models.dag import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.empty import EmptyOperator
def all_bad():
raise Exception("I have failed")
def all_good():
print("ALL GOOD")
def failure_callback_dag(context):
print("Inside failure_callback_dag")
print(context["task_instance"])
print(context["task"])
with DAG(
dag_id = "test_dag",
schedule_interval=None,
start_date=datetime.datetime(2021, 1, 1),
catchup=False,
on_failure_callback=failure_callback_dag
) as dag:
start = EmptyOperator(
task_id="start"
)
fail = PythonOperator(
provide_context = True,
task_id = "fail",
python_callable = all_bad
)
passs = PythonOperator(
provide_context = True,
task_id = "pass",
python_callable = all_good
)
start >> [passs, fail]
From the dag processor logs:
The context that is passed is from the task instance that has succeeded.
[2022-09-28T18:28:14.465+0000] {logging_mixin.py:117} INFO - [2022-09-28T18:28:14.463+0000] {dag.py:1292} INFO - Executing dag callback function: <function failure_callback_dag at 0x7fd17ca18560>
[2022-09-28T18:28:14.943+0000] {logging_mixin.py:117} INFO - Inside failure_callback_dag
[2022-09-28T18:28:14.944+0000] {logging_mixin.py:117} INFO - <TaskInstance: test_dag.pass manual__2022-09-28T18:27:59.612118+00:00 [success]>
[2022-09-28T18:28:14.944+0000] {logging_mixin.py:117} INFO - <Task(PythonOperator): pass>
Operating System
Debian GNU/Linux 10 (buster)
Versions of Apache Airflow Providers
Default providers that are present in the official airflow docker image.
Deployment
Docker-Compose
Deployment details
No response
Anything else
Not sure if it is an expected behaviour, incase it is it needs to be documented in Callbacks.
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
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 2
- Comments: 17 (9 by maintainers)
Perhaps we should rename the DAG-level argument to
on_dag_failure_callbackinstead?Callbacks are set at both the DAG and task level in the code snippets, but the copy in the doc only mentions tasks. Even the callback callable of
on_failure_callback=task_failure_alertset at the DAG is named in a misleading way. The callback will be triggered when the DagRun fails. Which is similar to a task failing but the context provided to the callable wouldn’t necessarily be related to the task that failed. IIRC the context passed to the DAG-level callback is either the first task’s context or just whatever is returned from the metadatabase first. IMO the doc can use a glow-up to help with these distinctions.+1
Hi @cklimkowski
One possible workaround you could use that I have employed in some of my dags to find “the earliest failed task instance from the context or even all the task instances that failed” is to use the following pattern when designing callback functions:
This should always get at least the first failed task instance. However, if a failed task triggers a dag failure and there are still other tasks running, and those tasks end up failing, it won’t pick these up as their state will be
runningwhen the callback fires. A potential workaround (that I’ve not tested) if you need all failed is to implement a retry with a backoff on your callback that waits until no tasks have the staterunning. This could lead to complications with undead tasks though, so be sure to set some limit on number of retries if you are to do this.@josh-fell
on_failure_callbackappears to pass incorrect context withpostgresas backend. works as intended when usingsqlitei.e with callback defined at DAG level, returns context of the failed task_instance.Tested with Airflow
V2.6.3Yeah, looking at the Callbacks documentation, I definitely agree it’s misleading and could be improved.
Wanted to bump this as i just ran into it as well, I assumed that the dag callback would get the ti for the task that actually failed.
Hi All!
Hi @josh-fell, If the same callback is applied to all tasks, I think, it would be triggered by each task failure instead of being executed only once at the DAG failure. Please share your thoughts.
It would be nice to be capable of getting the earliest failed task instance from the context or even all the task instances that failed.
Please advise.
Hey @vgupta3! Have you tried applying your
on_failure_callbacktodefault_argsinstead? Settingon_failure_callbackat the DAG level applies to the DagRun failing not tasks specifically.If you want to gain the context from the failed task, you can set
on_failure_callbackat the task level or, I presume you’d like the same callback to apply to all tasks, withindefault_args.And, of course, if the documentation isn’t clear enough you are absolutely welcomed and encouraged to open a PR to clarify concepts!