Late DAG parse result can replace the latest DAG after a newer bundle version is published
At 1311365a3872e5c0b89bc5e24ce913dfc59d8864, I reproduced a late-result ordering problem through Airflow's direct database write and read APIs.
- A prepares a v1 DAG containing
old_taskbut has not published its result. - B publishes v2 containing
new_task. An existing run gets a task instance pointing to that DAG version. - A publishes its earlier v1 result.
| Fresh read | Latest DAG | B's existing run |
|---|---|---|
| Before A publishes | Version 1, new_task |
Version 1, new_task |
| After A publishes | Version 2, old_task |
Version 1, new_task |
Each observation uses a new database session and DBDagBag. The script asserts that B's task instance points to version 1.
I expected A's result to be rejected or rescheduled once v2 had been published. Could the direct database publication path check whether the parsed bundle version is still current before updating DAG state? A late result can otherwise receive the highest DAG version number, which subsequent latest-DAG reads select.
This is a function-level reproduction using Airflow's database write and read APIs. I controlled the publication order and have not run two DAG processor processes against each other. The script sets min_update_interval=0 so the serialization throttle does not mask the ordering behavior.
Manager publication path · Version creation
Reproduction script and setup
Use an empty SQLite metadata database. From the repository root at the commit above, install its local packages and initialize the database:
python -m pip install -e . -e airflow-core -e task-sdk
export AIRFLOW_HOME=/tmp/airflow-test
export AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=sqlite:////tmp/airflow-test/airflow.db
export AIRFLOW__CORE__LOAD_EXAMPLES=False
airflow db migrate
python repro.py
Save this as repro.py:
import os
os.environ['AIRFLOW_HOME'] = '/tmp/airflow-test'
os.environ['AIRFLOW__DATABASE__SQL_ALCHEMY_CONN'] = 'sqlite:////tmp/airflow-test/airflow.db'
os.environ['AIRFLOW__CORE__LOAD_EXAMPLES'] = 'False'
from sqlalchemy import select
from airflow.models.dagbag import DBDagBag
from airflow.models.dag import DagModel
from airflow.models.dagbundle import DagBundleModel
from airflow.models.dagrun import DagRun
from airflow.models.dag_version import DagVersion
from airflow.models.serialized_dag import SerializedDagModel
from airflow.models.taskinstance import TaskInstance
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.sdk import DAG
from airflow.serialization.serialized_objects import LazyDeserializedDAG
from airflow.utils.session import create_session
from airflow.utils.state import DagRunState
from airflow.utils.types import DagRunType
def make_dag(task_id):
with DAG(dag_id='late_result_repro', schedule=None) as dag:
EmptyOperator(task_id=task_id)
return dag
def observe(label):
# A new Session and DBDagBag on each observation avoid either cache.
with create_session() as session:
latest = DagVersion.get_latest_version('late_result_repro', session=session)
latest_dag = DBDagBag().get_latest_version_of_dag('late_result_repro', session=session)
run = session.scalar(select(DagRun).where(DagRun.run_id == 'b_existing_run'))
run_dag = DBDagBag().get_dag_for_run(run, session=session)
task_instance = session.scalar(select(TaskInstance).where(TaskInstance.run_id == run.run_id))
assert task_instance.dag_version_id == run.created_dag_version_id
print(label, 'latest', latest.version_number, sorted(latest_dag.task_ids),
'B run', run.created_dag_version.version_number, sorted(run_dag.task_ids),
'TI version', run.created_dag_version.version_number, flush=True)
a = make_dag('old_task')
b = make_dag('new_task')
with create_session() as session:
session.add(DagBundleModel(name='repro', version='v2'))
session.flush()
session.add(DagModel(dag_id='late_result_repro', fileloc='/tmp/repro_dag.py',
bundle_name='repro', bundle_version='v2'))
# A has completed parsing v1 but its result has not been persisted.
SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(b), bundle_name='repro',
bundle_version='v2', min_update_interval=0)
with create_session() as session:
n = DagVersion.get_latest_version('late_result_repro', session=session)
run = DagRun(dag_id='late_result_repro', run_id='b_existing_run',
run_type=DagRunType.MANUAL, state=DagRunState.QUEUED,
bundle_version='v2')
run.created_dag_version_id = n.id
session.add(run)
session.flush()
session.add(TaskInstance(task=b.get_task('new_task'), dag_version_id=n.id,
run_id=run.run_id))
observe('BEFORE A')
# The same direct-DB writer receives A's already parsed result after B committed.
SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(a), bundle_name='repro',
bundle_version='v1', min_update_interval=0)
observe('AFTER A')
Observed output:
BEFORE A latest 1 ['new_task'] B run 1 ['new_task'] TI version 1
AFTER A latest 2 ['old_task'] B run 1 ['new_task'] TI version 1
Thanks for opening your first issue here! Be sure to follow the issue template! If you are willing to raise PR to address this issue please do so, no need to wait for approval.
I was curious about one thing. The reproduction seems to call write_dag directly, and the Dag processor seems to keep each parse result tied to the bundle snapshot it started from before it reaches write_dag. Have you come across this in a running deployment as well?
No, I haven’t seen this in a running deployment. I called write_dag directly with min_update_interval=0 so both writes would go through. Each result kept its original bundle version. I wrote v2 first, then v1, and a fresh database session returned the DAG from v1 as the latest. What I haven’t verified is whether two DAG processors can reach write_dag in that order.
I followed up with two DAG processors on commit 1311365a, using a Git bundle, SQLite, and the default 30-second update interval. I ran the update twice, once with a shared Git working directory and once with separate directories. Both processors parsed both revisions in each run. Fresh database reads switched from old_task to new_task and stayed there for the next 35 seconds.
Only the direct write_dag test produced the rollback, with the write order set by the script. I couldn't reproduce that ordering through the processors, so I'm withdrawing the report and closing the issue. Thanks for asking about the deployment path.