Repository navigation
Closed
Description
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
Reacted by woodchuckgym, Tom and IndiTheo
Activity
boring-cyborg commentedon Sep 28, 2022
Thanks for opening your first issue here! Be sure to follow the issue template!
josh-fell commentedon Sep 28, 2022
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!
josh-fell commentedon Sep 28, 2022
Yeah, looking at the Callbacks documentation, I definitely agree it's misleading and could be improved.
uranusjr commentedon Sep 29, 2022
Perhaps we should rename the DAG-level argument to
on_dag_failure_callbackinstead?eladkal commentedon Sep 30, 2022
Actually both options are mentioned:
https://airflow.apache.org/docs/apache-airflow/stable/logging-monitoring/callbacks.html
josh-fell commentedon Oct 1, 2022
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
josh-fell commentedon Feb 2, 2023
This has been coming up more frequently for users. I can take on related improvements here.
viniciusdsmello commentedon Mar 12, 2023
Hello everyone. Could you let me know if there are any updates on this issue?
I had the same issue with the following DAG

I'm creating an on_failure_callback to send events to Pagerduty. In one of my tests, I noticed that the context contains information from the first task.
Here is the callback function

10 remaining items
ManiBharataraju commentedon May 8, 2024
@josh-fell - This doesn't appear to be happening. I have
on_failure_callbackset indefault_argsbut it gets fired as soon as the task fails while the DAG is still in running state. I have also encountered instances where it fired twice for a failed DAG, one for the DAG(where the task name was wrong) and other for the task that actually failed. So I am assuming there is some issue the way callbacks are handled.kyoshi-warrior commentedon Jun 21, 2024
@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.3fjmacagno commentedon Mar 26, 2025
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.
IndiTheo commentedon Jan 27, 2026
Did some research on this, here is what happens exactly.
The context you see in DAG-level callbacks is constructed here:
airflow/airflow-core/src/airflow/dag_processing/processor.py
Lines 324 to 342 in 056e24e
In normal case, the task details you see in context are of
last_ticoming from scheduler's request:airflow/airflow-core/src/airflow/dag_processing/processor.py
Lines 327 to 334 in 056e24e
airflow/airflow-core/src/airflow/jobs/scheduler_job_runner.py
Lines 2199 to 2211 in 056e24e
The
last_tiis computed here:airflow/airflow-core/src/airflow/models/dagrun.py
Lines 1414 to 1431 in 056e24e
Which just selects the last task in lexicographical order from the entire DAG.
I could reproduce it by creating a DAG with several tasks
a,b,c,... and no matter which task actually failed the execution or what order between the tasks was, the last one alphabetically was passed to the callback context.I think we need to change this behavior to include a task or multiple tasks in failed state from the DAG, or just pass nothing. Actually, if you wanted to know which task failed, you'd probably use task-level callbacks injected with
default_args, so passing no task info to DAG-level callbacks is pretty logical here, though backcomp breaks this way. I want to submit a PR to fix this and #61119 soon. Still got to figure out if we can select only one task as the "culprit" of the DAG failure.IndiTheo commentedon Jan 28, 2026
The code above is actually just one use of
dag_run.get_last_tiwhen there are multiple of them. Some failures (DAG timeouts or tasks deadlocked) can't be associated with a task failure, while other ones can be. If the dag isfail_fast, then one specific task can be pointed at. If not, we can prbly take the first failed task for backcomp (the two cases are pretty equivalent in code, just slightly different conceptually)Summary:
fail_fast, pass the first failed task's context)IndiTheo commentedon Feb 4, 2026
Tried to improve this behavior by passing a task that is somehow relevant to failure, in general case it's one of the tasks causing the DAG to fail:
#61274
I'd like to get a review on this, thank you!
potiuk commentedon Mar 2, 2026
@Asquator We are unassigning you from this issue as part of our updated assignment policy.
This is not meant to discourage your contribution — quite the opposite! You are still very welcome to work on this issue and submit a PR for it. Simply comment that you are working on it and open a PR when ready.
We found that formal assignments were not working well, as they often prevented others from contributing when the assignee was not actively working on the issue.
IndiTheo commentedon Mar 7, 2026
@potiuk Should be probably included back into 3.2.0 milestone.
eladkal commentedon Mar 8, 2026
Milestone on issues are not so meaningful. It's mostly for issue triage.
We mostly use milestones or PRs