- Notifications
You must be signed in to change notification settings - Fork 16.2k
Fix DetachedInstanceError when accessing DagRun.created_dag_version #54362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kaxil merged 1 commit into apache:main from astronomer:fix-detached-instance-error-dag-version Aug 11, 2025
Merged
Fix DetachedInstanceError when accessing DagRun.created_dag_version #54362
kaxil merged 1 commit into apache:main from astronomer:fix-detached-instance-error-dag-version Aug 11, 2025
+12 −2
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Member Author
dstandish approved these changes Aug 11, 2025
Backport failed to create: v3-0-test. View the failure log Run details
You can attempt to backport this manually by running: cherry_picker 05d2229 v3-0-testThis should apply the commit to the v3-0-test branch and leave the commit in conflict state marking 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)
Member Author
| Manually cherry-picked 364d246 |
54 tasks
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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
The error occurred when trying to access
dag_run.created_dag_versionon a DagRun object that was detached from its SQLAlchemy session.Stacktrace:
Two-layer fix:
selectinloadforcreated_dag_versioninprocess_executor_eventsquery to ensure optimal performance for this critical pathDBDagBag._version_from_dag_runto 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.rstor{issue_number}.significant.rst, in airflow-core/newsfragments.