anmolxlight opened a new pull request, #73451: URL: https://github.com/apache/airflow/pull/73451
Closes: #73311 **Root cause:** in `TaskMap.expand_mapped_task`, the Dag run is obtained via a lazy relationship (`unmapped_ti.dag_run`) or a scalar fetch, but `context_carrier` is only read much later after flushes, queries, and mutation hooks. By then session commits elsewhere in the scheduling chain can have expired and detached the Dag run, so the late attribute access raises `DetachedInstanceError`. The scheduling error handler then touches detached `run.run_id`/`run.dag_id`, raising a second `DetachedInstanceError` that escapes and kills the scheduler. **Fix:** - Read the trace carrier into a local at both points where the Dag run is obtained, while it is still bound to the session, and pass the local to `new_task_run_carrier`. A plain dict local is immune to later expire/detach, with zero extra queries. - Capture `run_id`/`dag_id` into locals before the try block in `_schedule_all_dag_runs` so the error handler can never throw. **Verification:** new logic proven with a standalone SQLAlchemy repro (old access pattern raises, captured local survives commit plus detach); `test_taskmap.py` green via breeze (3 passed); mypy, ruff, ruff-format green. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
