akhilpratap1991 opened a new pull request, #71348: URL: https://github.com/apache/airflow/pull/71348
`adopt_or_reset_orphaned_tasks()` reads deferred columns (the full row copied into `TaskInstanceHistory` by `prepare_db_for_next_try`, `last_heartbeat_at`) and the lazy `dag_run` relationship on TaskInstances that executors' `try_adopt_task_instances()` may hand back detached from the session. Those reads raise `DetachedInstanceError`, which is unhandled in `_run_scheduler_loop` — the scheduler exits, and since it dies before the orphan is reset, the same orphan crashes it again on every restart: an unrecoverable crash loop that halts all scheduling (observed in production with KubernetesExecutor as a scheduler `CrashLoopBackOff`). Where the attributes happen to be loaded, the writes to the detached instances are silently lost instead, so the orphans are never actually reset. This completes #67822, which fixed the first such read (`repr(ti)` on `state`/`map_index`) by widening `load_only` — but `TaskInstanceHistory(ti)` copies ~35 columns, so widening the column list further just moves the crash. Instead, re-select the rows to mutate by their already-loaded ids (with `dag_run` joined-loaded) so both the reset and adopt branches operate on fully loaded, session-bound instances, independent of what the executor did with the objects it was handed. The regression test simulates the executor detaching the TaskInstances it was handed (as observed in production): without this change it fails with the production `DetachedInstanceError` (deferred load of `try_number`); with it, the orphan is reset with a `TaskInstanceHistory` audit row and the adopted TI gets its heartbeat/dag_run handling. --- ##### Was generative AI tooling used to co-author this PR? - [X] Yes — Claude Code (Opus 4.8) Generated-by: Claude Code (Opus 4.8) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions) -- 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]
