Added defensive "before" validator in DagRunContext to handle DetachedInstanceError #59857
+129
−2
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.
Description
A defensive (
mode = "before") Pydantic validator has been added toDagRunContextto safely handle detached ORMDagRunobjects. The validator reloads theDagRunobject from the DB withconsumed_asset_eventswhen relationship access would otherwise raiseDetachedInstanceError, preventing scheduler crashes inDagRuntimeout and callback paths. This complements existing eager-loading fixes added in PR #56916 and PR #59714.Rationale
DagRunContextmay be constructed with ORMDagRuninstances that are no longer session-bound (e.g. during scheduler timeout handling). Accessing lazily loaded relationships such asconsumed_asset_eventson detached instances can raiseDetachedInstanceError, which currently propagates and causes the scheduler to exit unexpectedly.This issue has surfaced multiple times in different
DagRuncode paths. Centralizing a defensive guard inDagRunContextensures that missing or detached relationship data does not crash the scheduler. Using a session to reload the DagRun within the validator is an acceptable tradeoff to maintain correctness with regards to the state ofconsumed_asset_events. Also, it is assumed that the reloadedDagRunentity must exist in the DB for code paths whereDagRunContextis called.Note that this validator only applies to ORM
DagRunobjects and not fastapi data models.Tests
DagRunContextconstruction with a detached ORMDagRun, asserting thatconsumed_asset_eventsis present afterwards.DagRunpath to ensure existing behavior is preserved.Closes: #59740