Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Databases and Redis · Step-by-step tutorial

Airflow gives up on a DAG file because parsing it takes longer than 30 seconds

Short answer

Importing the DAG file took longer than [core] dagbag_import_timeout, 30 seconds by default, so the DAG processor stopped it. The cause is nearly always top-level code: heavy imports, Variable.get, database or API calls that run on every parse. Time the file, move that work into tasks, and give extra time only to a file that stays slow.

  • Applies to Apache Airflow 3.4.0, Apache Airflow 3.0 and later, Apache Airflow 2.3 and later (per-file timeout hook)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

The DAG processor imports every DAG file as a Python module, over and over: [dag_processor] min_file_process_interval says each file is parsed again every 30 seconds by default. Each import runs inside a timer set by dagbag_import_timeout. If the module has not finished in time, the import is cut off, nothing from that file is loaded on that pass, and the message lands in the import errors list shown in the UI.

Raising the number makes the message go away and leaves the cost in place, paid on every parse of every file. The procedure below finds where the seconds go, removes them from parse time, and only then decides whether one file truly needs a longer limit.

Before you start

  • Shell access to the machine or container that runs the DAG processor, with the same Python environment and variables
  • The path of the failing file, taken from the import error
  • Permission to edit the DAG file and, for the last step, airflow_local_settings.py
  • A way to restart the DAG processor

Step 1

Three limits that are easy to confuse

Before changing anything, know which number you are looking at. The configuration reference lists them separately, and they work at different levels.

SettingDefaultWhat it limits
`[core] dagbag_import_timeout`30.0 (float)How long one Python file import may run; this is the one in the error
`[dag_processor] dag_file_processor_timeout`50 (integer)How long the processor handling a file may run in total
`[dag_processor] min_file_process_interval`30How often each file is parsed again, which is why slow top-level code costs so much

If you raise the import timeout above the processor timeout, the processor is stopped first, so the longer import limit never gets the chance to apply.

Step 2

Step 1: time the file where Airflow parses it

The Airflow best-practices guide calls this the Dag loader test: run the file with plain Python in an environment that matches the scheduler's, and read the real figure. Run it a few times, since the first run pays for cold caches, and subtract the interpreter's own start-up, which time python -c '' shows.

The Dag loader test from the Airflow docs

time python /opt/airflow/dags/etl.py
time python -c ''
  • Far under 30 seconds here but timing out in the cluster: the difference is the environment (CPU limits, network to the database, a missing cache). Repeat the test inside the DAG processor's container.
  • Over 30 seconds here as well: continue to step 2.

Step 3

Step 2: find the lines that cost the time

Python's -X importtime option prints each module with its self time and cumulative time in microseconds. Sorting on the cumulative column puts the expensive imports at the bottom. If the imports are cheap and the total is still high, the time is spent running code, not loading it.

Slow imports, then calls that run at parse time

python -X importtime /opt/airflow/dags/etl.py 2>&1 | sort -t'|' -k2 -n | tail -15
grep -nE "Variable\.get|requests\.|\.get_conn\(|get_records|read_csv|boto3\.client" /opt/airflow/dags/etl.py

Step 4

Step 3: push heavy imports down into the tasks

The guide is explicit that import statements are top-level code too. Keep only Airflow and cheap standard library imports at module level, and import pandas, ML frameworks and large SDKs inside the function that runs as the task. The import then happens once per task run on a worker, not every 30 seconds in the processor.

from airflow.sdk import dag, task

@dag(schedule=None)
def etl():
    @task()
    def transform():
        import pandas as pd      # loaded only when this task runs
        ...
    transform()

etl()

Step 5

Step 4: stop calling services while the file is parsed

Variable.get() at module level, or in an operator argument written as an f-string, goes to the metadata database or secrets backend on every parse. The guide recommends Jinja templates instead, such as {{ var.value.get('target_bucket') }}, which are rendered only when the task runs. The same applies to hooks, HTTP calls and queries used to decide how many tasks to build.

A Variable read at run time, not parse time

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

report = BashOperator(
    task_id="report",
    bash_command="echo target=$TARGET",
    env={"TARGET": "{{ var.value.get('target_bucket') }}"},
)
  • If the DAG shape really depends on outside data, write that data to a file in the DAG folder from a separate job and read the file at parse time.
  • When top-level Variables cannot be removed yet, [secrets] use_cache (experimental, off by default) caches them during parsing only, for cache_ttl_seconds (900 by default). Changes then take up to that long to show.

Step 6

Step 5: give one slow file its own limit

Some files are legitimately slow, such as a factory that builds hundreds of DAGs from a config file. The Airflow FAQ describes a get_dagbag_import_timeout function in airflow_local_settings.py, available since 2.3, that is called right before each file is parsed. In 3.4.0 it receives the file's path as a string; return a number of seconds, or 0 or less for no limit. Everything else keeps the global value.

Per-file timeout from the Airflow FAQ

# airflow_local_settings.py
from airflow.configuration import conf

def get_dagbag_import_timeout(dag_file_path: str):
    if "generated_" in dag_file_path:
        return 90
    return conf.getfloat("core", "DAGBAG_IMPORT_TIMEOUT")

# keep the whole-file limit above it, in the DAG processor's environment:
# AIRFLOW__DAG_PROCESSOR__DAG_FILE_PROCESSOR_TIMEOUT=120

Step 7

The DAG appears without an import error

  1. Repeated time python runs on the file are well under the limit, ideally a few seconds.
  2. airflow dags list-import-errors no longer lists the file after the next parse.
  3. The DAG and its tasks are back in the UI, and the DAG processor log shows no new timeout for that path.
  4. A CI job runs the loader test on every DAG file and fails when one exceeds a third of the limit, so the next slow import is caught in review.

It worked when

  • The timeout is per file import and resets on every parse.
  • Measure in the processor's own environment first.
  • Imports and service calls at module level are the usual cost; move them into tasks.
  • Raise the limit per file with get_dagbag_import_timeout, and keep dag_file_processor_timeout above it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.