Skip to content

Conversation

@kaxil
Copy link
Member

@kaxil kaxil commented Aug 11, 2025

The error occurred when trying to access dag_run.created_dag_version on a DagRun object that was detached from its SQLAlchemy session.

Stacktrace:

[2025-08-08T19:03:28.761+0000] {scheduler_job_runner.py:935} ERROR - Marking task instance <TaskInstance: sell_oms_shipconfirm_mao_to_ecom_load_delta_quarterly.shipconfirm_delta_to_kafka_notebook_task scheduled__2025-08-08T07:30:00+00:00 [running]> as failed Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 925, in process_executor_events dag = scheduler_dag_bag.get_dag(dag_run=ti.dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 141, in get_dag version = self._version_from_dag_run(dag_run=dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 135, in _version_from_dag_run dag_version = dag_run.created_dag_version ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 487, in __get__ return self.impl.get(state, dict_) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 959, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 995, in _fire_loader_callables return self.callable_(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 863, in _load_for_state raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <DagRun at 0x7f46d309a5d0> is not bound to a Session; lazy load operation of attribute 'created_dag_version' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3) 

Two-layer fix:

  1. Proactive loading: Add selectinload for created_dag_version in process_executor_events query to ensure optimal performance for this critical path
  2. Defensive code: Add relationship loading check in DBDagBag._version_from_dag_run to handle all call sites safely using SQLAlchemy inspect pattern

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

The error occurred when trying to access `dag_run.created_dag_version` on a DagRun object that was detached from its SQLAlchemy session. Stacktrace: ``` [2025-08-08T19:03:28.761+0000] {scheduler_job_runner.py:935} ERROR - Marking task instance <TaskInstance: sell_oms_shipconfirm_mao_to_ecom_load_delta_quarterly.shipconfirm_delta_to_kafka_notebook_task scheduled__2025-08-08T07:30:00+00:00 [running]> as failed Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 925, in process_executor_events dag = scheduler_dag_bag.get_dag(dag_run=ti.dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 141, in get_dag version = self._version_from_dag_run(dag_run=dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 135, in _version_from_dag_run dag_version = dag_run.created_dag_version ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 487, in __get__ return self.impl.get(state, dict_) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 959, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 995, in _fire_loader_callables return self.callable_(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 863, in _load_for_state raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <DagRun at 0x7f46d309a5d0> is not bound to a Session; lazy load operation of attribute 'created_dag_version' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3) ``` Two-layer fix: 1. Proactive loading: Add `selectinload` for `created_dag_version` in `process_executor_events` query to ensure optimal performance for this critical path 2. Defensive code: Add relationship loading check in `DBDagBag._version_from_dag_run` to handle all call sites safely using SQLAlchemy inspect pattern
@kaxil kaxil added this to the Airflow 3.0.5 milestone Aug 11, 2025
@kaxil kaxil requested review from XD-DENG and ashb as code owners August 11, 2025 16:28
@kaxil kaxil added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Aug 11, 2025
@boring-cyborg boring-cyborg bot added the area:Scheduler including HA (high availability) scheduler label Aug 11, 2025
@kaxil
Copy link
Member Author

kaxil commented Aug 11, 2025

@kaxil kaxil merged commit 05d2229 into apache:main Aug 11, 2025
59 checks passed
@kaxil kaxil deleted the fix-detached-instance-error-dag-version branch August 11, 2025 17:25
@github-actions
Copy link

Backport failed to create: v3-0-test. View the failure log Run details

Status Branch Result
v3-0-test Commit Link

You can attempt to backport this manually by running:

cherry_picker 05d2229 v3-0-test

This should apply the commit to the v3-0-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue
kaxil added a commit that referenced this pull request Aug 11, 2025
…n` (#54362) The error occurred when trying to access `dag_run.created_dag_version` on a DagRun object that was detached from its SQLAlchemy session. Stacktrace: ``` [2025-08-08T19:03:28.761+0000] {scheduler_job_runner.py:935} ERROR - Marking task instance <TaskInstance: sell_oms_shipconfirm_mao_to_ecom_load_delta_quarterly.shipconfirm_delta_to_kafka_notebook_task scheduled__2025-08-08T07:30:00+00:00 [running]> as failed Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 925, in process_executor_events dag = scheduler_dag_bag.get_dag(dag_run=ti.dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 141, in get_dag version = self._version_from_dag_run(dag_run=dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 135, in _version_from_dag_run dag_version = dag_run.created_dag_version ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 487, in __get__ return self.impl.get(state, dict_) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 959, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 995, in _fire_loader_callables return self.callable_(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 863, in _load_for_state raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <DagRun at 0x7f46d309a5d0> is not bound to a Session; lazy load operation of attribute 'created_dag_version' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3) ``` Two-layer fix: 1. Proactive loading: Add `selectinload` for `created_dag_version` in `process_executor_events` query to ensure optimal performance for this critical path 2. Defensive code: Add relationship loading check in `DBDagBag._version_from_dag_run` to handle all call sites safely using SQLAlchemy inspect pattern (cherry picked from commit 05d2229)
@kaxil
Copy link
Member Author

kaxil commented Aug 11, 2025

Manually cherry-picked 364d246

RoyLee1224 pushed a commit to RoyLee1224/airflow that referenced this pull request Aug 15, 2025
…n` (apache#54362) The error occurred when trying to access `dag_run.created_dag_version` on a DagRun object that was detached from its SQLAlchemy session. Stacktrace: ``` [2025-08-08T19:03:28.761+0000] {scheduler_job_runner.py:935} ERROR - Marking task instance <TaskInstance: sell_oms_shipconfirm_mao_to_ecom_load_delta_quarterly.shipconfirm_delta_to_kafka_notebook_task scheduled__2025-08-08T07:30:00+00:00 [running]> as failed Traceback (most recent call last): File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 925, in process_executor_events dag = scheduler_dag_bag.get_dag(dag_run=ti.dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 141, in get_dag version = self._version_from_dag_run(dag_run=dag_run, session=session) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/airflow/jobs/scheduler_job_runner.py", line 135, in _version_from_dag_run dag_version = dag_run.created_dag_version ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 487, in __get__ return self.impl.get(state, dict_) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 959, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/attributes.py", line 995, in _fire_loader_callables return self.callable_(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/sqlalchemy/orm/strategies.py", line 863, in _load_for_state raise orm_exc.DetachedInstanceError( sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <DagRun at 0x7f46d309a5d0> is not bound to a Session; lazy load operation of attribute 'created_dag_version' cannot proceed (Background on this error at: https://sqlalche.me/e/14/bhk3) ``` Two-layer fix: 1. Proactive loading: Add `selectinload` for `created_dag_version` in `process_executor_events` query to ensure optimal performance for this critical path 2. Defensive code: Add relationship loading check in `DBDagBag._version_from_dag_run` to handle all call sites safely using SQLAlchemy inspect pattern
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Scheduler including HA (high availability) scheduler backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch

3 participants