dabla commented on code in PR #73405: URL: https://github.com/apache/airflow/pull/73405#discussion_r4676749097
########## .agents/skills/airflow-async-endpoints/SKILL.md: ########## @@ -0,0 +1,134 @@ +--- +name: airflow-async-endpoints +description: Write or migrate Airflow FastAPI endpoints to async I/O, preserving API behavior, transaction ownership, and backend compatibility. Use for AsyncSessionDep adoption or auditing blocking work in an async route and its dependencies. +--- + +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# Write and migrate async endpoints + +Use the current checkout as the source of truth. A migration should release the event loop while waiting for I/O and preserve the endpoint's observable contract. An `async def` declaration alone establishes neither property. + +## Trace the request before editing + +Read the route, router/app dependencies, authorization, domain helpers, and response serialization. Classify their I/O: native async, synchronous code dispatched by FastAPI, or synchronous code called directly on the event loop. FastAPI can offload a synchronous dependency; an ordinary sync function called inside `async def` is still a direct blocking call. Converting a handler to `async def` also moves its CPU work from a threadpool worker onto the event loop, and awaiting database I/O does not offload it: ORM result processing (hydrating entities and their eagerly loaded relationships), JSON decoding, serialized-Dag deserialization, and response validation all run on the loop and stall every other request it serves. A replacement that looks mechanical can regress this way, so measure it as described under "Measure event-loop availability". + +Identify session ownership, lazy/deferred ORM attributes, external clients, secrets resolution, cache IPC, filesystem operations, and initialization on the request path. Choose a small route or cohesive batch whose complete path can be made safe. For new endpoints, also follow the target API's authorization and versioning rules. + +For pure migrations retain method/path, parameter validation, response shape, ordering/count semantics, errors, permissions, team filters, and API-version behavior. Leave sibling routes alone unless a shared helper forces an adaptation. + +## Use the existing database infrastructure + +- Inspect `airflow-core/src/airflow/api_fastapi/common/db/common.py` and `airflow-core/src/airflow/utils/session.py`. Reuse `AsyncSessionDep` and `create_session_async`; do not build a parallel engine/session configuration. On shutdown, the API server lifespan in `api_fastapi/app.py` and the in-process Execution API transport call `settings.dispose_async_engine()`, which closes the pooled connections and keeps the engine and session factory usable. Routes and helpers must not create or dispose engines. +- The async engine is configured apart from the sync engine, in `_configure_async_session` in `settings.py`. Before routing queries through it, confirm its MySQL sessions get the sync engine's setup: select `@@session.time_zone` and `@@session.transaction_isolation` through `settings.AsyncSession` and compare them with a sync session. +- Await I/O such as `execute`, `scalar`, `scalars`, `flush`, and `refresh`. Buffered results are consumed synchronously, for example `(await session.scalars(statement)).all()`. Operations such as `add` and `expunge` are not coroutines. Streaming results have different consumption rules. +- Keep transaction ownership at the existing boundary. `AsyncSessionDep` is function-scoped so commit failure reaches the client before success is sent. Do not add route-level commits. A helper receiving a session must not commit, close it, or create a second transaction to perform the same work. +- Preserve locks, update predicates, flush ordering, rowcount branches, and rollback behavior. SQLite cannot validate production row-lock semantics. Do not run concurrent operations on one `AsyncSession`. +- Audit each query's projection as described below, including queries inside reused helpers. Materialize response data before its owning session ends; treat streaming responses separately because function-scoped teardown precedes body consumption. +- Make shared decision helpers session-independent where practical: let each caller execute its own sync/async query, then pass plain data. The heartbeat history check in `execution_api/routes/task_instances.py` demonstrates this. Where a sync helper only executes a statement built by a separate builder, call the builder and await its statement instead of adapting the helper: `SerializedDagModel.get(dag_id)` only executes `SerializedDagModel.latest_item_select_object(dag_id)`, so an async caller awaits `session.scalar(SerializedDagModel.latest_item_select_object(dag_id))` and passes the row to the synchronous, session-free `DBDagBag._read_dag`. + +### Audit individual query projections + +Follow every `SELECT` reached by the route, including dependencies and backend helpers. For each query, identify the selected fields, the fields its consumers actually read, mapper-added eager relationships, and expected row count. Include fields used for authorization, errors, and helper decisions, not just the response. Record the proposed projection or why the full entity is required. + +When consumers need only values, select those columns directly rather than an ORM entity. A one-value lookup can use `session.scalar(select(Model.value).where(...))`; calling `scalar(select(Model))` still hydrates the entire entity. Preserve predicates, ordering, locks, and the distinction between a missing row and a nullable value: `scalar()` returns `None` for both, so when the column is nullable, fetch `(await session.execute(statement)).first()` and test the row. Keep shared callers that need model methods or relationships working; narrow a shared query only when its complete caller contract permits it. + +Inspect mapper loading too: `select(TaskInstance)` also loads its joined `dag_run`. Column projections avoid both unused database fields and entity hydration; they also prevent response access from triggering lazy SQL. `load_only` still constructs entities and is useful only when consumers actually need them. + +Replacing `session.get(TaskInstance, id)` with a column select changes which attempts are visible: primary-key lookups bypass the session hook that hides archived attempts, and a select filtered on `TaskInstance.id` does not (see the `TaskInstance` docstring). Write the intended `working_set` predicate into the query rather than relying on either behavior. + +Apply this audit even to one-row getters and pre-existing async helpers newly used by the conversion. Report the concrete fields avoided; measure before claiming a material latency or event-loop improvement. For an observed example, see [the asset-state-store review](references/asset-state-store-review.md). + +## Handle synchronous boundaries explicitly + +Do not pass an `AsyncSession` to `@provide_session` or synchronous model methods. The call does not fail at the call site: a body calling `session.scalar(...)` gets back an un-awaited coroutine, and one using `.query` or `.scalars(...).all()` raises `AttributeError`. + +Choose the adaptation for each synchronous helper on an async route by what the helper does: + +- **Executes a statement from a separate builder:** await the builder's statement on the route's session, as described under session-independent helpers above. +- **Only SQL, called by sync code too:** keep one implementation and call it through `AsyncSession.run_sync`, which passes a synchronous `Session` as the first positional argument. Most repository helpers take `session` as keyword-only, so wrap the call: `await session.run_sync(lambda s: helper(arg, session=s))`. Database waits then yield the event loop. Re-check the helper for non-SQL I/O on every conversion that relies on it. +- **Only SQL, no sync callers, or being restructured anyway:** write an async version, prefixed `a` as in `ExecutionAPISecretsBackend.aget_variable`, that takes a keyword-only `session: AsyncSession` and awaits its queries. Pass `session=session` at every call site, including tests, following [the repository session rule](../../../AGENTS.md#coding-standards). +- **Mixes database access with blocking I/O or costly CPU work:** split the path so ordinary queries use the async driver. Reserve `AsyncSession.run_sync` for SQL-only helpers because it runs the helper on the event loop. For a synchronous dependency without an async equivalent, keep a synchronous FastAPI dependency if it preserves the contract, or keep the route synchronous until the blocking work can be isolated. + +- **Needs an explicit worker offload:** scope the offload to the work that needs it and explain the trade-off in the PR. Explicit offloads compete with synchronous endpoints for worker capacity. Propose an intentional CPU-only API site in the guard's `ALLOWED_OFFLOADS` set with its rationale, as in [the task-state-store example](references/task-state-store-conversion.md#get-worker-boundaries). The guard treats entries in the [known-offload file](https://github.com/apache/airflow/blob/main/airflow-core/tests/unit/api_fastapi/known_api_server_threadpool_offloads.txt) as strict expected failures. Use that file for offloads tracked for removal and delete an entry when its offload is gone. + + A worker that needs a sync session opens its own `create_session(scoped=False)`. End an independent read-only transaction before waiting for a worker, and measure any extra checkout. Keep the reads and locks that a write depends on in the write's transaction. Preserve the order of access checks and errors when splitting the work. + +Never share a session across the thread boundary or hide a broken async DB configuration by silently retrying with a sync session. + +Code that receives a session from its caller must use it: pass the route's `AsyncSessionDep` session to every helper that accepts one. Do not add a create-a-session-if-omitted fallback, including an async counterpart of `provide_session`, to helpers whose callers hold a session: a forgotten `session=` silently checks out a second connection and can starve the pool. A fallback is acceptable only where the calling interface cannot carry a session, such as a secrets-backend method with the `get_variable(key, team_name)` signature of `BaseSecretsBackend`. Even there, accept an optional keyword-only `session`, pass it whenever the caller has one, and add the fallback in the PR that introduces its first caller. + +Variable/Connection value resolution is a separate integration from key listing. Trace the backend chain and inspect current async capabilities before choosing a conversion. The SDK already has async connection dispatch and `ExecutionAPISecretsBackend.aget_connection/aget_variable`; that does not make the core model resolvers or every provider async. Preserve backend precedence, legacy method overrides, team propagation, cache semantics, and terminal access denials. A synchronous custom backend must not run directly on the event loop. + +## Prove the conversion + +Run existing HTTP contract tests before and after the change, including supported Execution API versions. Preserve assertions; adapt async session mocks and fixtures where needed instead of requiring test source to remain byte-identical. Use spec/autospec and awaited mocks for coroutine methods, normal mocks for buffered result accessors. Add regressions only for behavior introduced or endangered by this conversion, including failure paths; reuse existing coverage for old behavior. An awaited-call assertion (`mock.patch.object(AsyncSession, "scalar", autospec=True)` plus `assert_awaited_once()`) adds signal only for a route whose handler calls the session directly; a route already exercised by DB-backed contract tests gains coupling, not coverage, from it. + +Run [`test_event_loop_blocking.py`](https://github.com/apache/airflow/blob/main/airflow-core/tests/unit/api_fastapi/test_event_loop_blocking.py) in Breeze. Its static check covers explicit offloads throughout `api_fastapi`. Its runtime sweep sends generic requests to core API and UI routes in both team modes. For a changed path the sweep misses, including Execution API routes, reuse the `event_loop_queries` fixture with a valid route-specific request. Assert the HTTP result and that the fixture recorded no synchronous SQL on the event loop. Review Comment: **[nit]** The runtime sweep has its own known-offender list, and a conversion has to edit it. The offload section explains `ALLOWED_OFFLOADS` and `known_api_server_threadpool_offloads.txt`, but this paragraph does not mention [`known_sync_db_on_event_loop.txt`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/tests/unit/api_fastapi/test_event_loop_blocking.py#L40), which lists 21 core API routes as strict xfails today. Converting one of them makes `test_known_route_no_longer_blocks` fail until its line is deleted, and the file is the nearest thing to a to-do list for this migration. One sentence would do: "delete the route's line from `known_sync_db_on_event_loop.txt` in the same PR, and pick the next route from it". ########## .agents/skills/airflow-async-endpoints/SKILL.md: ########## @@ -0,0 +1,134 @@ +--- +name: airflow-async-endpoints +description: Write or migrate Airflow FastAPI endpoints to async I/O, preserving API behavior, transaction ownership, and backend compatibility. Use for AsyncSessionDep adoption or auditing blocking work in an async route and its dependencies. +--- + +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# Write and migrate async endpoints + +Use the current checkout as the source of truth. A migration should release the event loop while waiting for I/O and preserve the endpoint's observable contract. An `async def` declaration alone establishes neither property. + +## Trace the request before editing + +Read the route, router/app dependencies, authorization, domain helpers, and response serialization. Classify their I/O: native async, synchronous code dispatched by FastAPI, or synchronous code called directly on the event loop. FastAPI can offload a synchronous dependency; an ordinary sync function called inside `async def` is still a direct blocking call. Converting a handler to `async def` also moves its CPU work from a threadpool worker onto the event loop, and awaiting database I/O does not offload it: ORM result processing (hydrating entities and their eagerly loaded relationships), JSON decoding, serialized-Dag deserialization, and response validation all run on the loop and stall every other request it serves. A replacement that looks mechanical can regress this way, so measure it as described under "Measure event-loop availability". + +Identify session ownership, lazy/deferred ORM attributes, external clients, secrets resolution, cache IPC, filesystem operations, and initialization on the request path. Choose a small route or cohesive batch whose complete path can be made safe. For new endpoints, also follow the target API's authorization and versioning rules. + +For pure migrations retain method/path, parameter validation, response shape, ordering/count semantics, errors, permissions, team filters, and API-version behavior. Leave sibling routes alone unless a shared helper forces an adaptation. + +## Use the existing database infrastructure + +- Inspect `airflow-core/src/airflow/api_fastapi/common/db/common.py` and `airflow-core/src/airflow/utils/session.py`. Reuse `AsyncSessionDep` and `create_session_async`; do not build a parallel engine/session configuration. On shutdown, the API server lifespan in `api_fastapi/app.py` and the in-process Execution API transport call `settings.dispose_async_engine()`, which closes the pooled connections and keeps the engine and session factory usable. Routes and helpers must not create or dispose engines. +- The async engine is configured apart from the sync engine, in `_configure_async_session` in `settings.py`. Before routing queries through it, confirm its MySQL sessions get the sync engine's setup: select `@@session.time_zone` and `@@session.transaction_isolation` through `settings.AsyncSession` and compare them with a sync session. +- Await I/O such as `execute`, `scalar`, `scalars`, `flush`, and `refresh`. Buffered results are consumed synchronously, for example `(await session.scalars(statement)).all()`. Operations such as `add` and `expunge` are not coroutines. Streaming results have different consumption rules. +- Keep transaction ownership at the existing boundary. `AsyncSessionDep` is function-scoped so commit failure reaches the client before success is sent. Do not add route-level commits. A helper receiving a session must not commit, close it, or create a second transaction to perform the same work. +- Preserve locks, update predicates, flush ordering, rowcount branches, and rollback behavior. SQLite cannot validate production row-lock semantics. Do not run concurrent operations on one `AsyncSession`. +- Audit each query's projection as described below, including queries inside reused helpers. Materialize response data before its owning session ends; treat streaming responses separately because function-scoped teardown precedes body consumption. +- Make shared decision helpers session-independent where practical: let each caller execute its own sync/async query, then pass plain data. The heartbeat history check in `execution_api/routes/task_instances.py` demonstrates this. Where a sync helper only executes a statement built by a separate builder, call the builder and await its statement instead of adapting the helper: `SerializedDagModel.get(dag_id)` only executes `SerializedDagModel.latest_item_select_object(dag_id)`, so an async caller awaits `session.scalar(SerializedDagModel.latest_item_select_object(dag_id))` and passes the row to the synchronous, session-free `DBDagBag._read_dag`. + +### Audit individual query projections + +Follow every `SELECT` reached by the route, including dependencies and backend helpers. For each query, identify the selected fields, the fields its consumers actually read, mapper-added eager relationships, and expected row count. Include fields used for authorization, errors, and helper decisions, not just the response. Record the proposed projection or why the full entity is required. + +When consumers need only values, select those columns directly rather than an ORM entity. A one-value lookup can use `session.scalar(select(Model.value).where(...))`; calling `scalar(select(Model))` still hydrates the entire entity. Preserve predicates, ordering, locks, and the distinction between a missing row and a nullable value: `scalar()` returns `None` for both, so when the column is nullable, fetch `(await session.execute(statement)).first()` and test the row. Keep shared callers that need model methods or relationships working; narrow a shared query only when its complete caller contract permits it. + +Inspect mapper loading too: `select(TaskInstance)` also loads its joined `dag_run`. Column projections avoid both unused database fields and entity hydration; they also prevent response access from triggering lazy SQL. `load_only` still constructs entities and is useful only when consumers actually need them. + +Replacing `session.get(TaskInstance, id)` with a column select changes which attempts are visible: primary-key lookups bypass the session hook that hides archived attempts, and a select filtered on `TaskInstance.id` does not (see the `TaskInstance` docstring). Write the intended `working_set` predicate into the query rather than relying on either behavior. Review Comment: **[warning]** A `working_set` predicate cannot bring archived attempts back; only `include_all_attempts` can. The paragraph is right that `session.get(TaskInstance, id)` sees archived attempts and a select on `TaskInstance.id` does not. The remedy it gives ("write the intended `working_set` predicate into the query") only works in one direction. The session hook adds `working_set IS TRUE` to every ORM select unless the statement carries the `include_all_attempts` execution option ([`_restrict_to_current_attempts`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/src/airflow/models/taskinstance.py#L3825-L3843)), so a query that adds `.where(TaskInstance.working_set.is_(None))`, or no predicate at all, still returns no archived row. For a pure migration that replaces `session.get` and has to keep its visibility (the 410-versus-404 branch in the heartbeat route depends on it), the statement needs `.execution_options(include_all_attempts=True)`, as the converted heartbeat already does ([`task_instances.py#L1323`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_instances.py#L1323)). Suggested wording: "To keep `session.get`'s visibility, add `.execution_options(include_all_attempts=True)`; to restrict to live attempts, rely on the hook or filter `working_set` explicitly." ########## .agents/skills/airflow-async-endpoints/SKILL.md: ########## @@ -0,0 +1,134 @@ +--- +name: airflow-async-endpoints +description: Write or migrate Airflow FastAPI endpoints to async I/O, preserving API behavior, transaction ownership, and backend compatibility. Use for AsyncSessionDep adoption or auditing blocking work in an async route and its dependencies. +--- + +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# Write and migrate async endpoints + +Use the current checkout as the source of truth. A migration should release the event loop while waiting for I/O and preserve the endpoint's observable contract. An `async def` declaration alone establishes neither property. + +## Trace the request before editing + +Read the route, router/app dependencies, authorization, domain helpers, and response serialization. Classify their I/O: native async, synchronous code dispatched by FastAPI, or synchronous code called directly on the event loop. FastAPI can offload a synchronous dependency; an ordinary sync function called inside `async def` is still a direct blocking call. Converting a handler to `async def` also moves its CPU work from a threadpool worker onto the event loop, and awaiting database I/O does not offload it: ORM result processing (hydrating entities and their eagerly loaded relationships), JSON decoding, serialized-Dag deserialization, and response validation all run on the loop and stall every other request it serves. A replacement that looks mechanical can regress this way, so measure it as described under "Measure event-loop availability". + +Identify session ownership, lazy/deferred ORM attributes, external clients, secrets resolution, cache IPC, filesystem operations, and initialization on the request path. Choose a small route or cohesive batch whose complete path can be made safe. For new endpoints, also follow the target API's authorization and versioning rules. + +For pure migrations retain method/path, parameter validation, response shape, ordering/count semantics, errors, permissions, team filters, and API-version behavior. Leave sibling routes alone unless a shared helper forces an adaptation. + +## Use the existing database infrastructure + +- Inspect `airflow-core/src/airflow/api_fastapi/common/db/common.py` and `airflow-core/src/airflow/utils/session.py`. Reuse `AsyncSessionDep` and `create_session_async`; do not build a parallel engine/session configuration. On shutdown, the API server lifespan in `api_fastapi/app.py` and the in-process Execution API transport call `settings.dispose_async_engine()`, which closes the pooled connections and keeps the engine and session factory usable. Routes and helpers must not create or dispose engines. +- The async engine is configured apart from the sync engine, in `_configure_async_session` in `settings.py`. Before routing queries through it, confirm its MySQL sessions get the sync engine's setup: select `@@session.time_zone` and `@@session.transaction_isolation` through `settings.AsyncSession` and compare them with a sync session. +- Await I/O such as `execute`, `scalar`, `scalars`, `flush`, and `refresh`. Buffered results are consumed synchronously, for example `(await session.scalars(statement)).all()`. Operations such as `add` and `expunge` are not coroutines. Streaming results have different consumption rules. +- Keep transaction ownership at the existing boundary. `AsyncSessionDep` is function-scoped so commit failure reaches the client before success is sent. Do not add route-level commits. A helper receiving a session must not commit, close it, or create a second transaction to perform the same work. +- Preserve locks, update predicates, flush ordering, rowcount branches, and rollback behavior. SQLite cannot validate production row-lock semantics. Do not run concurrent operations on one `AsyncSession`. +- Audit each query's projection as described below, including queries inside reused helpers. Materialize response data before its owning session ends; treat streaming responses separately because function-scoped teardown precedes body consumption. +- Make shared decision helpers session-independent where practical: let each caller execute its own sync/async query, then pass plain data. The heartbeat history check in `execution_api/routes/task_instances.py` demonstrates this. Where a sync helper only executes a statement built by a separate builder, call the builder and await its statement instead of adapting the helper: `SerializedDagModel.get(dag_id)` only executes `SerializedDagModel.latest_item_select_object(dag_id)`, so an async caller awaits `session.scalar(SerializedDagModel.latest_item_select_object(dag_id))` and passes the row to the synchronous, session-free `DBDagBag._read_dag`. + +### Audit individual query projections + +Follow every `SELECT` reached by the route, including dependencies and backend helpers. For each query, identify the selected fields, the fields its consumers actually read, mapper-added eager relationships, and expected row count. Include fields used for authorization, errors, and helper decisions, not just the response. Record the proposed projection or why the full entity is required. + +When consumers need only values, select those columns directly rather than an ORM entity. A one-value lookup can use `session.scalar(select(Model.value).where(...))`; calling `scalar(select(Model))` still hydrates the entire entity. Preserve predicates, ordering, locks, and the distinction between a missing row and a nullable value: `scalar()` returns `None` for both, so when the column is nullable, fetch `(await session.execute(statement)).first()` and test the row. Keep shared callers that need model methods or relationships working; narrow a shared query only when its complete caller contract permits it. + +Inspect mapper loading too: `select(TaskInstance)` also loads its joined `dag_run`. Column projections avoid both unused database fields and entity hydration; they also prevent response access from triggering lazy SQL. `load_only` still constructs entities and is useful only when consumers actually need them. + +Replacing `session.get(TaskInstance, id)` with a column select changes which attempts are visible: primary-key lookups bypass the session hook that hides archived attempts, and a select filtered on `TaskInstance.id` does not (see the `TaskInstance` docstring). Write the intended `working_set` predicate into the query rather than relying on either behavior. + +Apply this audit even to one-row getters and pre-existing async helpers newly used by the conversion. Report the concrete fields avoided; measure before claiming a material latency or event-loop improvement. For an observed example, see [the asset-state-store review](references/asset-state-store-review.md). + +## Handle synchronous boundaries explicitly + +Do not pass an `AsyncSession` to `@provide_session` or synchronous model methods. The call does not fail at the call site: a body calling `session.scalar(...)` gets back an un-awaited coroutine, and one using `.query` or `.scalars(...).all()` raises `AttributeError`. + +Choose the adaptation for each synchronous helper on an async route by what the helper does: + +- **Executes a statement from a separate builder:** await the builder's statement on the route's session, as described under session-independent helpers above. +- **Only SQL, called by sync code too:** keep one implementation and call it through `AsyncSession.run_sync`, which passes a synchronous `Session` as the first positional argument. Most repository helpers take `session` as keyword-only, so wrap the call: `await session.run_sync(lambda s: helper(arg, session=s))`. Database waits then yield the event loop. Re-check the helper for non-SQL I/O on every conversion that relies on it. +- **Only SQL, no sync callers, or being restructured anyway:** write an async version, prefixed `a` as in `ExecutionAPISecretsBackend.aget_variable`, that takes a keyword-only `session: AsyncSession` and awaits its queries. Pass `session=session` at every call site, including tests, following [the repository session rule](../../../AGENTS.md#coding-standards). +- **Mixes database access with blocking I/O or costly CPU work:** split the path so ordinary queries use the async driver. Reserve `AsyncSession.run_sync` for SQL-only helpers because it runs the helper on the event loop. For a synchronous dependency without an async equivalent, keep a synchronous FastAPI dependency if it preserves the contract, or keep the route synchronous until the blocking work can be isolated. + +- **Needs an explicit worker offload:** scope the offload to the work that needs it and explain the trade-off in the PR. Explicit offloads compete with synchronous endpoints for worker capacity. Propose an intentional CPU-only API site in the guard's `ALLOWED_OFFLOADS` set with its rationale, as in [the task-state-store example](references/task-state-store-conversion.md#get-worker-boundaries). The guard treats entries in the [known-offload file](https://github.com/apache/airflow/blob/main/airflow-core/tests/unit/api_fastapi/known_api_server_threadpool_offloads.txt) as strict expected failures. Use that file for offloads tracked for removal and delete an entry when its offload is gone. + + A worker that needs a sync session opens its own `create_session(scoped=False)`. End an independent read-only transaction before waiting for a worker, and measure any extra checkout. Keep the reads and locks that a write depends on in the write's transaction. Preserve the order of access checks and errors when splitting the work. + +Never share a session across the thread boundary or hide a broken async DB configuration by silently retrying with a sync session. + +Code that receives a session from its caller must use it: pass the route's `AsyncSessionDep` session to every helper that accepts one. Do not add a create-a-session-if-omitted fallback, including an async counterpart of `provide_session`, to helpers whose callers hold a session: a forgotten `session=` silently checks out a second connection and can starve the pool. A fallback is acceptable only where the calling interface cannot carry a session, such as a secrets-backend method with the `get_variable(key, team_name)` signature of `BaseSecretsBackend`. Even there, accept an optional keyword-only `session`, pass it whenever the caller has one, and add the fallback in the PR that introduces its first caller. + +Variable/Connection value resolution is a separate integration from key listing. Trace the backend chain and inspect current async capabilities before choosing a conversion. The SDK already has async connection dispatch and `ExecutionAPISecretsBackend.aget_connection/aget_variable`; that does not make the core model resolvers or every provider async. Preserve backend precedence, legacy method overrides, team propagation, cache semantics, and terminal access denials. A synchronous custom backend must not run directly on the event loop. + +## Prove the conversion + +Run existing HTTP contract tests before and after the change, including supported Execution API versions. Preserve assertions; adapt async session mocks and fixtures where needed instead of requiring test source to remain byte-identical. Use spec/autospec and awaited mocks for coroutine methods, normal mocks for buffered result accessors. Add regressions only for behavior introduced or endangered by this conversion, including failure paths; reuse existing coverage for old behavior. An awaited-call assertion (`mock.patch.object(AsyncSession, "scalar", autospec=True)` plus `assert_awaited_once()`) adds signal only for a route whose handler calls the session directly; a route already exercised by DB-backed contract tests gains coupling, not coverage, from it. + +Run [`test_event_loop_blocking.py`](https://github.com/apache/airflow/blob/main/airflow-core/tests/unit/api_fastapi/test_event_loop_blocking.py) in Breeze. Its static check covers explicit offloads throughout `api_fastapi`. Its runtime sweep sends generic requests to core API and UI routes in both team modes. For a changed path the sweep misses, including Execution API routes, reuse the `event_loop_queries` fixture with a valid route-specific request. Assert the HTTP result and that the fixture recorded no synchronous SQL on the event loop. + +Check that fixtures commit seed data visible to the async connection. + +Router-level and Cadwyn re-solved dependencies that still use `SessionDep` open a second session per request. An `after_begin` listener on the `Session` class also fires for `AsyncSession`, so counting distinct sessions across N requests shows whether the dependencies and the handler share the route's session. Cadwyn solves dependencies twice, once for each `current_dependency_solver` value (`"fastapi"`, then `"cadwyn"`), and only the `"cadwyn"` pass shares the endpoint's session. A router-level dependency that queries through `AsyncSessionDep` in the first pass holds a second async connection until the request ends. For an independent access or identity check that would otherwise hold a connection across a worker wait, use a short `create_session_async()` session and check whether both dependency passes need to query. Verify connection lifetimes with pool checkout and check-in listeners on both engines. + +Match engine and event-loop lifetimes. The async engine's pool binds each connection to the event loop that opened it. The harness builds a fresh `TestClient` loop per test and `asyncio_default_fixture_loop_scope` is `function`, so a pooled connection reused across tests fails with "attached to a different loop" on every backend, including SQLite (`aiosqlite` file databases use `AsyncAdaptedQueuePool`). + +In `tests/unit/api_fastapi/execution_api/conftest.py`, the `async_db_engine` fixture builds a fresh engine per test with `_configure_async_session()` and restores the previous `settings` globals afterwards. The `client` fixture depends on it and disposes that engine through `client.portal` while its loop is still alive. Tests that use `client` need no per-class handling. Do not add `reconfigure_async_db_engine` fixtures, `usefixtures` tags, or `_configure_async_session()` calls to Execution API tests: `client` disposes only the engine `async_db_engine` created, so an engine rebound inside the test keeps its pooled connections after teardown. A pytest-asyncio test in that directory that opens `settings.AsyncSession()` itself should request `async_db_engine` and `await async_db_engine.dispose()` before it returns; the next test gets a new engine either way, but skipping the dispose drops open connections at garbage collection. + +Any `with TestClient(app)` runs the API server lifespan, which closes the async engine's pooled connections on the client's loop at exit. The core API fixtures in `tests/unit/api_fastapi/conftest.py` enter `TestClient` as a context manager, so core API tests need no reconfigure fixture either. A `TestClient(app)` used without `with` skips the lifespan and its disposal. A pytest-asyncio test outside these fixtures that opens `settings.AsyncSession()` itself should call `await settings.dispose_async_engine()` on its own loop before returning, as `tests/unit/utils/test_session.py` does; the engine stays usable, so do not reconfigure it afterwards. A global loop-scope redesign is separate work. + +`_configure_async_session()` rebinds the module globals `settings.async_engine` and `settings.AsyncSession`. Read them through the module object (`from airflow import settings` then `settings.AsyncSession()`), or import inside the function after the call. `from airflow.settings import AsyncSession` at test or module top binds the stale object and silently keeps using the previous engine. Importing the function `_configure_async_session` by name is fine. + +Observe client status with server-exception reraising disabled when testing commit-before-response behavior. + +Exercise DB-dependent conversions through Breeze on SQLite, PostgreSQL, and MySQL using the current configured drivers. Inspect `settings.py` and the database setup guide rather than hardcoding an old asyncpg default. Record backend, driver, test command, and result; an unavailable backend is unverified, not a pass. Where the change affects SQL/driver or connection configuration, also cover supported overrides and the relevant PgBouncer mode. Apply the repository's Ruff, mypy, and prek requirements to the changed surface. + +For migrations, API version/schema generation is unnecessary when the HTTP contract is unchanged. If a contract change becomes necessary, separate that decision and follow `execution_api/AGENTS.md` and the versioning guide. + +### Measure event-loop availability + +Measure every converted route whose work grows with data: rows per Dag run, mapped tasks, XComs, serialized Dag size, or bytes/elements in a single JSON value. One database row can still require substantial decoding and validation. Run the same harness against the base revision and the conversion. The regression to catch is how long the route blocks the event loop, which can worsen while the request itself gets faster, so request duration alone does not reveal it. + +Before collecting samples, verify the interpreter and database drivers inside the container; a requested Breeze version can fall back to another runtime. Exercise a request and remove unrelated fixture errors from both revisions. For example, when auth is independently overridden, a fake bearer header can still trigger repeated token-refresh exceptions in middleware. Keep the intended auth coverage explicit. + +1. Write the probe as a test function in a temporary `dev/` script. The script calls `pytest.main` on an existing test of the route's module, passing a plugin whose `pytest_collection_modifyitems` swaps the collected item for the probe, so the probe gets that directory's `dag_maker`, `session`, and `client` fixtures. Anchor on a module-level test and replace the items with `pytest.Function.from_parent(anchor.parent, name=..., callobj=probe)`, where `probe` is a closure whose signature names the fixtures it needs; pytest resolves fixtures from that signature, and a collected bound method swapped for a free function would not receive `self`. Before calling a path unreachable from the probe, read how the module's own tests reach it; trigger-header requests, for example, go through `TestClient(InProcessExecutionAPI(triggerer_id=...).app)`. Run it with `breeze run bash -c "python dev/<harness>.py <args>"`: `breeze run` parses options it shares with the script, so `-p` reaches it as `--py thon`. An untracked file in `dev/` counts as a changed CI-tooling file, so move the harness out of the checkout before `breeze verify`, prek, or a commit. Seed realistic volume and commit it; for task-instance routes, use one Dag run with 1,000 and with 5,000 mapped instances. +2. In a coroutine run with `client.portal.call(...)`, start a ticker task that loops on `await asyncio.sleep(0.001)` and records each sleep's own duration with `time.monotonic()`. The maximum is the stall. Timing each sleep, rather than the gaps between recorded ticks, also captures a stall that ends as the request does. +3. Send the real HTTP request from a worker thread: `await asyncio.to_thread(client.get, url, params=...)`. Calling `client.get` directly in the coroutine blocks the loop being measured, and going through HTTP includes dependencies, validation, and serialization. +4. Assert the response status and the number of returned items in the harness, so an error response cannot pass as a fast one. +5. Send at least ten sequential requests per size, drop the first, which also pays for cold caches, and run each revision twice; medians can differ twofold between runs. Report the median and the maximum of the warm stalls. When the path can reach the DagBag (`TaskCoordinateResolver`, `get_dag*`, `DBDagBag`), also measure a cold scenario that clears `app.state.dag_bag` before every request: a cache miss deserializes the Dag, and under `run_sync` it does so on the loop, a regression no warm sample shows. +6. To locate the cost, profile the loop thread: enable a `cProfile.Profile()` inside the portal coroutine around the request and sort by `tottime`. It separates the handler's own code, ORM result processing, and response serialization. Try each candidate fix in a disposable worktree with the same harness before proposing it. Collect profiles on separate requests excluded from timing samples, with the same per-request setup, such as clearing the cache, as the scenario being explained. A loop-thread profile excludes the synchronous base handler's worker-thread CPU: it locates on-loop work, not a change in total CPU consumption. To explain an isolated maximum, record garbage-collector pauses with `gc.callbacks`, tagging the thread each one runs on; a full collection on a large heap lasts hundreds of milliseconds. + +Report, per revision and size: returned items, warm median and maximum stall, warm request duration, backend, driver, and the command. These are small local samples; present them as event-loop availability, not throughput. + +The bar is the base revision measured in the same session. The synchronous base stalls the loop too, while its threadpool worker holds the GIL, and occasionally by hundreds of milliseconds, so the bar is not zero. Expect a small fixed increase at every size: async statement execution and result handling now run on the loop instead of in a worker thread, about 1 ms per request on SQLite and PostgreSQL in the historical task-state-store measurements. No step below removes it; report it. Beyond that fixed increase, the conversion's warm median and maximum stall must not exceed the base's at any size. Until the conversion meets the bar, choose the applicable steps below in order and re-measure after each; row-processing remedies do not address a single large JSON value: + +1. Select only the columns the response uses. +2. Unpack rows as tuples (`.tuples()`), or use `scalars()` for a single column, and build the response directly; per-row `Row` attribute access and temporary containers add up across thousands of rows. +3. For results that grow with data, stream them and process each partition as it arrives, so the loop regains control between partitions: `result = await session.stream(statement.execution_options(yield_per=500))`, then `async for partition in result.tuples().partitions()`. This uses a server-side cursor, so consume it fully before the next statement on the session, run the contract tests on PostgreSQL and MySQL, and repeat the stall measurement on PostgreSQL, since each partition adds a database round trip. +4. Use the profile to isolate the remaining CPU work. For large JSON reads, measure decoding, model construction, and rendering together because recursive `JsonValue` validation can dominate. If the data already satisfies the response contract, consider returning a `JSONResponse` and retaining `response_model=` in the route decorator. Compare `/openapi.json?version=<v>` for every API version, and confirm no `convert_response_to_previous_version_for` targets the model. Cadwyn decodes and re-encodes a returned `JSONResponse` body on the loop (cadwyn#51) and double-encodes a plain `Response` with an `application/json` content type. If the remaining cost warrants a worker fallback, compare event-loop delay and request latency under concurrent load with and without it. Pass detached data to the worker when it only needs to render the response. See [the XCom example](references/xcom-conversion.md). Review Comment: **[nit]** `cadwyn#51` does not look like the reference for the `JSONResponse` re-encode. [zmievsa/cadwyn#51](https://github.com/zmievsa/cadwyn/issues/51) is "Optimize request migration for migration-less cases" (closed): it is about request migration, not about decoding and re-encoding a returned `JSONResponse` body. Either point to the issue or source lines that describe the response path, or drop the number and keep the measured statement from the XCom reference. ########## .agents/skills/airflow-async-endpoints/references/task-state-store-conversion.md: ########## @@ -0,0 +1,50 @@ +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# Task-state-store conversion evidence + +## GET worker boundaries + +The default `BaseStoreBackend.aget_value` awaits `aget`, then runs `json.loads` in a worker with `asyncio.to_thread`. Backends that return native values can override `aget_value` and skip that decode. The GET route separately constructs and validates `TaskStateStoreResponse` with `run_in_threadpool`. FastAPI still serializes the response on the event loop. + +The API guard scans `api_fastapi`, so its `ALLOWED_OFFLOADS` entry covers the route's CPU-only validation call. The default decoder lives in the shared state package. The shared-state test checks decoding on a worker for valid and malformed JSON. The HTTP route test checks response construction on a worker and the returned value. + +Newer SQLite and PostgreSQL measurements cover both offloads. The async GET was above the synchronous baseline in some rounds and below it in others, so stall parity remains unverified. [GET comparison in #75381](https://github.com/apache/airflow/pull/75381) + +## Historical benchmark + +These measurements cover the earlier conversion in commit `b265496e2c`, measured as patches on base `8cc830d4ef`. The GET offload variant combined decoding and model construction in one worker call. Review Comment: **[nit]** The measured revision cannot be looked up from this repository. Commit `b265496e2c` does not resolve in apache/airflow (the commits API answers "No commit found for SHA"), and `2610/async-xcoms` in the XCom reference is a fork branch name. The bases (`8cc830d4ef`, `fe589528ee`) are on `main` and fine. Since the numbers are presented as evidence, link the PR commit they were taken at (a full SHA under `/pull/75381/commits/…` stays reachable) or say plainly that the revision is no longer available. ########## .agents/skills/airflow-async-endpoints/references/xcom-conversion.md: ########## @@ -0,0 +1,82 @@ +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# XCom conversion evidence + +See [the reviewed XCom conversion](https://github.com/apache/airflow/pull/75396) for the split after the offload check landed. Ordinary lookups use the async driver. The Dag-aware fallback and three JSON renderers have documented offload entries. The conversion returns its read-only connection before waiting for either kind of worker call. The PR compares the alternatives under concurrency and reports the cost for requests that need Dag-aware resolution. Review Comment: **[warning]** Same as comment [4]: the opening paragraph and "the kept form" below describe #75396, which is open. On `main` every XCom route is still synchronous ([`xcoms.py#L568`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/src/airflow/api_fastapi/execution_api/routes/xcoms.py#L568)), so "Ordinary lookups use the async driver", "three JSON renderers have documented offload entries" and "Each now queries in a short `create_session_async()` session of its own" describe a branch, not the repository the skill ships in. "The reviewed XCom conversion" also reads as settled while its review is ongoing. The measurements are useful as they are; the framing needs either the merge order (after #75396) or "proposed in" wording. ########## .agents/skills/airflow-async-endpoints/references/task-state-store-conversion.md: ########## @@ -0,0 +1,50 @@ +<!-- SPDX-License-Identifier: Apache-2.0 + https://www.apache.org/licenses/LICENSE-2.0 --> + +# Task-state-store conversion evidence + +## GET worker boundaries + +The default `BaseStoreBackend.aget_value` awaits `aget`, then runs `json.loads` in a worker with `asyncio.to_thread`. Backends that return native values can override `aget_value` and skip that decode. The GET route separately constructs and validates `TaskStateStoreResponse` with `run_in_threadpool`. FastAPI still serializes the response on the event loop. Review Comment: **[warning]** "GET worker boundaries" describes #75381 in the present tense, and that PR is still open. `SKILL.md` opens with "Use the current checkout as the source of truth" and, at line 54, sends the reader here for the model `ALLOWED_OFFLOADS` entry. On `main` none of what this section states exists yet: - `BaseStoreBackend.aget_value` calls `json.loads` inline, no `asyncio.to_thread` ([`shared/state/.../__init__.py#L288-L294`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/shared/state/src/airflow_shared/state/__init__.py#L288-L294)); - the GET route is a synchronous `def get_task_state_store` without `run_in_threadpool` ([`task_state_store.py#L70`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/src/airflow/api_fastapi/execution_api/routes/task_state_store.py#L70)); - `ALLOWED_OFFLOADS` holds the two auth middleware entries only ([`test_event_loop_blocking.py#L47-L52`](https://github.com/apache/airflow/blob/08dd8a208d0e7766bce351687879cb30ef342469/airflow-core/tests/unit/api_fastapi/test_event_loop_blocking.py#L47-L52)). An agent that follows the skill before #75381 merges looks for an example that is not in its checkout, and if review changes the shape of #75381 this text goes stale without anyone touching it. Either merge this after #75381, or word the section as "as proposed in #75381" and keep the present tense for what is on `main`. (Same point in comment [5] for the XCom reference.) -- 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]
