DEV Community

Kam Haripara
Kam Haripara

Posted on

Airflow tasks never time out by default: execution_timeout, dagrun_timeout and Deadline Alerts

A failed task is the easy case. It turns red, the failure callback fires, and someone gets a message. The harder case is a task that never finishes: a database lock that never clears, an HTTP call with no client timeout, a query waiting on a warehouse queue. Out of the box, Airflow lets that task stay in running indefinitely. No failure, no retry, no callback.

This post walks through the settings that put a ceiling on that, what each one actually does, and what to use now that SLAs are gone in Airflow 3. Everything below is from the Airflow 3 docs (linked at the end of each section).

1. The default: no limit at all

Every operator has an execution_timeout argument. If you don't set it, it is None. The [core] default_task_execution_timeout config option is what would supply a default, and its documentation is blunt: "If not specified, then the value is considered as None, meaning that the operators are never timed out by default."

So a task's runtime is unbounded unless you bound it.

There is one safety net that people sometimes mistake for a timeout: the task instance heartbeat timeout ([scheduler] task_instance_heartbeat_timeout, default 300 seconds). It exists for tasks whose job has stopped heartbeating, for example because the worker was OOM-killed or its pod was moved. These used to be called zombie tasks. It is about dead workers. It does not put any limit on a task that is alive and simply waiting.

Docs: Tasks: Timeouts and Task Instance Heartbeat Timeout, Configuration reference

2. execution_timeout: a ceiling per task

execution_timeout takes a datetime.timedelta. When a single execution runs longer, Airflow raises AirflowTaskTimeout and the task fails. Because it is an ordinary failure, retries apply and so does on_failure_callback.

from datetime import timedelta
from airflow.providers.standard.operators.bash import BashOperator

load_orders = BashOperator(
    task_id="load_orders",
    bash_command="python /opt/jobs/load_orders.py",
    execution_timeout=timedelta(minutes=30),
    retries=2,
    retry_delay=timedelta(minutes=5),
)
Enter fullscreen mode Exit fullscreen mode

A detail that matters for stuck native code: for tasks run by the Python task runner, the task supervisor also enforces the timeout from outside the task process. If the task hasn't stopped [core] killed_task_cleanup_time seconds after the timeout (default 60), the supervisor sends SIGTERM, then SIGKILL after the same period again. That covers code blocked somewhere that never hands control back to Python.

Note that the timeout applies to every execution, so with retries=2 the worst case above is three 30-minute attempts plus the retry delays.

Sensors have two clocks

Sensors get execution_timeout too, but they also have their own timeout parameter. The docs say timeout only matters for sensors in reschedule mode, and that it is the total time allowed for the sensor to succeed. When it is breached, AirflowSensorTimeout is raised and the sensor fails immediately without retrying. Retries caused by other errors do not reset that total.

from datetime import timedelta
from airflow.providers.sftp.sensors.sftp import SFTPSensor

sensor = SFTPSensor(
    task_id="wait_for_file",
    path="/root/test",
    execution_timeout=timedelta(seconds=60),  # each poke
    timeout=3600,                             # total, in seconds
    retries=2,
    mode="reschedule",
)
Enter fullscreen mode Exit fullscreen mode

Docs: Tasks: Timeouts

3. A global default so new tasks aren't unbounded

Setting execution_timeout on every task is easy to forget. Two ways to make it the default:

At the Dag level, through default_args:

default_args = {"execution_timeout": timedelta(hours=1)}
Enter fullscreen mode Exit fullscreen mode

Or for the whole deployment, with the config option (an integer number of seconds):

[core]
default_task_execution_timeout = 3600
Enter fullscreen mode Exit fullscreen mode

or as an environment variable:

export AIRFLOW__CORE__DEFAULT_TASK_EXECUTION_TIMEOUT=3600
Enter fullscreen mode Exit fullscreen mode

Per-task values still override it. Pick a deployment-wide number generous enough for your slowest legitimate task, then tighten it per Dag where you know the normal runtime.

Docs: Configuration reference: default_task_execution_timeout

4. dagrun_timeout: a ceiling for the whole run

Task timeouts don't catch everything. A run can stall between tasks too, or crawl along with each task inside its own limit. dagrun_timeout (also None by default) sets how long a Dag run may take before it times out and fails. Task instances still running at that moment are marked as skipped.

Two things to get right:

  • Make it longer than the retry worst case. If a task can legitimately take three attempts, a dagrun_timeout shorter than that will cut retries off.
  • Pair it with a Dag-level on_failure_callback. The callbacks docs say that, from Airflow 3.2, on a Dag run timeout the context passed to the Dag callback contains the latest started but unfinished task, which is usually the one you want to look at.

If your Dag has max_active_runs=1, this matters even more: a run that never ends means the next scheduled run waits behind it.

Docs: Task SDK DAG reference, Callbacks

Putting it together

from datetime import datetime, timedelta
from airflow.sdk import dag, task


def alert_on_failure(context):
    ti = context["task_instance"]
    # replace print with your Slack / PagerDuty / email notifier
    print(f"FAILED: {ti.dag_id}.{ti.task_id} run_id={context['run_id']}")


@dag(
    schedule="@daily",
    start_date=datetime(2026, 1, 1),
    catchup=False,
    dagrun_timeout=timedelta(hours=3),
    on_failure_callback=alert_on_failure,
    default_args={
        "execution_timeout": timedelta(minutes=45),
        "retries": 2,
        "retry_delay": timedelta(minutes=5),
        "on_failure_callback": alert_on_failure,
    },
)
def orders_daily():
    @task(execution_timeout=timedelta(minutes=10))
    def extract():
        ...

    @task
    def load(rows):
        ...

    load(extract())


orders_daily()
Enter fullscreen mode Exit fullscreen mode

Here a task can take at most 45 minutes per attempt (10 for extract), three attempts, and the whole run at most three hours. Any breach ends up in alert_on_failure.

Two callback caveats from the docs worth knowing: callbacks only run when the state changes because of execution by a worker, so marking a task failed in the UI or CLI won't fire them. And errors inside a callback show up in the Dag processor logs, not the task logs, so a broken notifier can fail quietly.

5. What replaced SLAs in Airflow 3

In Airflow 2 the usual way to hear about "this is taking too long" was sla plus sla_miss_callback. The SLA feature was removed in Airflow 3.0. Its replacement, Deadline Alerts, arrived in Airflow 3.1 and is marked experimental, so the API may still change.

A Deadline Alert has three parts: a reference point, an interval and a callback. The useful difference from a timeout is that it doesn't need the run to be running. With DeadlineReference.DAGRUN_LOGICAL_DATE, the docs say the alert fires if the Dag hasn't completed the given interval after it was scheduled to start, "regardless of when (or if) it actually began executing." That covers runs stuck in the queue, which no task timeout can see.

from datetime import timedelta
from airflow.sdk import AsyncCallback, DAG, DeadlineAlert, DeadlineReference
from airflow.providers.slack.notifications.slack_webhook import SlackWebhookNotifier
from airflow.providers.standard.operators.empty import EmptyOperator

with DAG(
    dag_id="orders_daily_deadline",
    schedule="@daily",
    deadline=DeadlineAlert(
        reference=DeadlineReference.DAGRUN_LOGICAL_DATE,
        interval=timedelta(hours=2),
        callback=AsyncCallback(
            SlackWebhookNotifier,
            kwargs={"text": "Dag {{ dag_run.dag_id }} missed its deadline at {{ deadline.deadline_time }}"},
        ),
    ),
):
    EmptyOperator(task_id="placeholder")
Enter fullscreen mode Exit fullscreen mode

The AsyncCallback import moved to airflow.sdk in Airflow 3.2; on 3.1 it lives in airflow.sdk.definitions.deadline. Other references include DAGRUN_QUEUED_AT, FIXED_DATETIME and AVERAGE_RUNTIME, and you can attach a list of alerts for a warn-then-escalate pattern.

Docs: Deadline Alerts, Upgrading to Airflow 3

Checklist

  • Set [core] default_task_execution_timeout so no task is unbounded by default.
  • Override execution_timeout per task where you know the normal runtime.
  • Give sensors in reschedule mode a timeout as well.
  • Set dagrun_timeout longer than the retry worst case, with a Dag-level on_failure_callback.
  • Test your callback by forcing a timeout in a dev environment, and check the Dag processor logs if nothing arrives.
  • On Airflow 3.1+, replace old SLAs with Deadline Alerts, keeping in mind they are experimental.

One gap remains after all this: every setting above lives inside Airflow. If the scheduler itself is down, or a Dag is paused by mistake, none of these callbacks will run. That is why I think a check from outside Airflow is worth having too. It is also what the Airflow monitoring Beta in Row Monitor, which I'm building, looks at: late, slow, failed and never-arrived runs plus the scheduler heartbeat.

Top comments (0)