vatsrahul1001 opened a new pull request, #77864:
URL: https://github.com/apache/airflow/pull/77864

   * Guard against response fields that pydantic validates lazily
   
   A response field annotated with Iterable is validated by pydantic lazily,
   while FastAPI serializes the response on the event loop. Each item's
   attributes are resolved there, on the sync engine: a lazy relationship or a
   selectin post-load becomes a query that blocks the whole API server once
   the connection pool is busy.
   
   PoolCollectionResponse.pools was fixed by hand in #74247, which left an
   explanatory comment in the datamodel, and the dags and deadlines fields in
   #76525. Twenty fields still have the shape, and nothing stops a new one
   from being added, because nothing about the annotation hints at the cost:
   list[...] validates on construction, inside the route, and renders to the
   same type: array in the OpenAPI schema, so the two read as interchangeable.
   
   The known fields are grandfathered as strict xfails rather than fixed here,
   so the list stays visible in the test report and can only shrink. The shape
   of the check follows the one proposed for the same class of problem in
   #76424.
   
   Incomplete models are rebuilt before their fields are read. An annotation
   naming something defined later in its own module stays an unparsed
   ForwardRef that get_origin cannot see through, as AssetResponse.aliases
   does, so a lazy field behind one would go unnoticed.
   
   * Exempt the lazy response fields whose items arrive resolved
   
   Declaring a response field Iterable[...] was deliberate in #57878, to
   spare the routes a list() or .all() that SQLA2's typing had made them
   add. Whether it costs anything is decided at the route, which the
   annotation cannot show, so listing all twenty fields as pending work
   misreported twelve of them.
   
   The split is by what reaches the event loop, not by whether the route
   built a list: pydantic validates each item during serialization either
   way, so an ORM row the response lazy-loads from is as costly as the
   query itself. Eight fields are handed something unresolved and stay in
   generated/known_lazy_response_fields.txt as strict xfails, tracked for
   migration in #76683. The other twelve hand over items that are already
   validated responses, or ORM rows with everything the response reads
   already loaded, and become exemptions in LAZY_FIELDS_WITHOUT_SYNC_QUERY.
   
   That set is closed, so a new field has to be list[...]; a route that
   resolves its items today is one refactor away from not doing so. A test
   fails if an exempt entry stops being declared lazily, so a dead entry
   cannot sit there unnoticed.
   
   * Stop the bulk clear Dag Runs route from querying on the event loop
   
   Only one of the twenty lazily-validated response fields is actually handed
   something unresolved: the bulk clearDagRuns route collects raw DagRun rows
   with no loader options, so DAGRunResponse's reads of dag_model and
   dag_versions happened while FastAPI serialized the response. The other
   nineteen were checked at every construction site and resolve nothing there,
   either because the route hands them finished response objects, or because
   the response reads only columns, or because the one relationship it reads
   through an AliasPath is eager-loaded.
   
   Grandfathering all twenty as pending migration therefore claimed a cost
   that nineteen of them do not have, which is what review asked about. They
   are recorded as exempt instead, so nothing is left to migrate and the
   generated ratchet list is unnecessary. Keeping the set closed is what still
   guards the next field: no test can tell from a datamodel whether the route
   behind an Iterable annotation resolves its items, and one that does today
   is one refactor away from not doing so.
   
   * State what makes a lazy response field safe to leave alone
   
   The exemption criterion left out post-loads. A route that hands the field an
   unconsumed session.scalars(...) can run a query on the event loop even when
   the response reads nothing from the relationship, because SQLAlchemy defers
   selectin, subquery and immediate loads to the result's first iteration, which
   here is serialization. Seven of the entries are that shape, and none of them
   has such a load today, so the set is unchanged -- but the comment claimed
   safety on grounds that do not cover them, and the maintainer drew the line at
   exactly this point.
   
   Iterator is likewise not ruled out by the schema error alone: a model that
   sets arbitrary_types_allowed takes it, and then it is passed through
   unvalidated and consumed during serialization, the same hazard the Iterable
   entries have. Checked on pydantic 2.14.1.
   
   DagVersion.bundle is read through the bundle_url property rather than an
   AliasPath, and Job.job_teams loads inside its route only because that route
   materializes with .all() -- the mapper strategy alone would not save it.
   
   * Say why Iterator can still reach the event loop
   
   The reason given for listing Iterator and the async iterables was wrong, not
   the decision. They are rejected at class definition only while no model sets
   arbitrary_types_allowed; under that config Iterator is passed through on an
   isinstance check and iterated during serialization, which is the hazard the
   Iterable entries already have. On pydantic 2.14.1 the async two error either
   way -- at validation for a sync iterable, at serialization for an async one.
   (cherry picked from commit da7dab32a844861f8d5d79991d059ed6dd40b4e6)
   
   Co-authored-by: Henry Chen <[email protected]>
   
   ---
   
   Clean cherry-pick of the merge commit, no conflicts.
   
   **Verified locally:** test_lazy_response_fields.py and public 
test_dag_run.py, 448 passed.
   
   Backport of #76537.
   
   Generated-by: Claude Code
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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]

Reply via email to