SameerMesiah97 opened a new pull request, #59857: URL: https://github.com/apache/airflow/pull/59857
**Description** A defensive (`mode = "before"`) Pydantic validator has been added to `DagRunContext `to safely handle detached ORM `DagRun` objects. The validator normalizes consumed_asset_events to an empty list when relationship access would otherwise raise `DetachedInstanceError`, preventing scheduler crashes in `DagRun` timeout and callback paths. This complements existing eager-loading fixes added in PR #56916 and PR #59740. **Rationale** `DagRunContext` may be constructed with ORM `DagRun` instances that are no longer session-bound (e.g. during scheduler timeout handling). Accessing lazily loaded relationships such as `consumed_asset_events` on detached instances can raise `DetachedInstanceError`, which currently propagates and causes the scheduler to exit unexpectedly. This issue has surfaced multiple times in different `DagRun` code paths. Centralizing a defensive guard in `DagRunContext` ensures that missing or detached relationship data does not crash the scheduler. Falling back to an empty list is acceptable in these paths and preserves scheduler stability. Note that this validator only applies to ORM `DagRun` objects and not fastapi data models. **Tests** - Added a test covering `DagRunContext` construction with a detached ORM `DagRun`, asserting that `consumed_asset_events` is normalized to an empty list. - Added a test covering the attached ORM `DagRun` path to ensure existing behavior is preserved. - Existing DagRunContext tests for non-ORM/datamodel inputs remain unchanged and continue to pass. -- 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]
