Airflow "Cycle detected in DAG" circular dependency in CI
A DAG must be acyclic. When task dependencies form a loop (A waits on B and B waits on A), Airflow raises AirflowDagCycleException during parsing and refuses to register the DAG.
What this error means
DAG parsing fails with "airflow.exceptions.AirflowDagCycleException: Cycle detected in DAG. Faulty task: X" naming a task involved in the loop.
Airflow
airflow.exceptions.AirflowDagCycleException:
Cycle detected in DAG: failed to set relationship between
task 'transform' and task 'extract' (faulty task 'extract')Common causes
Dependencies form a loop
Two tasks set each other as upstream (directly or through a chain), producing a cycle that is not a valid DAG.
A copy-paste error in set_upstream/set_downstream
A misplaced >> or << wires a task back to one of its own ancestors.
How to fix it
Break the loop in the dependency wiring
- Find the faulty task named in the exception.
- Trace its upstream chain to the task that points back to it.
- Remove or redirect the edge so the graph is acyclic.
dags/etl.py
# acyclic: each edge points forward only
extract >> transform >> loadAssert acyclicity in a DAG test
Loading the DagBag in a test surfaces the cycle before merge.
tests/test_dags.py
from airflow.models import DagBag
assert not DagBag().import_errorsHow to prevent it
- Wire dependencies in one direction with
>>chains. - Add a DagBag import test to CI to catch cycles.
- Review dependency edits carefully in shared DAGs.
Related guides
Airflow "Broken DAG ... ImportError" in CIFix Airflow "Broken DAG: [path] ImportError" in CI - a DAG file raised an ImportError during parsing, often f…
Airflow "AirflowException ... DAG ... could not be found" in CIFix Airflow "airflow.exceptions.AirflowException: DAG X could not be found" in CI - the scheduler or a test a…
Airflow "DagBag import timeout" in CIFix Airflow "DagBag import timeout" / "AirflowTaskTimeout: DagBag import timeout" in CI - a DAG file took lon…