jason810496 commented on code in PR #76493:
URL: https://github.com/apache/airflow/pull/76493#discussion_r4667553309


##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -573,13 +580,13 @@ def _authorize_backfill_in_path(method: ResourceMethod, 
dag_id: str | None, user
 
 def requires_access_backfill(
     method: ResourceMethod,
-) -> Callable[[Request, BaseUser, Session], Coroutine[Any, Any, None]]:
-    """Wrap ``requires_access_dag`` and extract the dag_id from the 
backfill_id."""
+) -> Callable[[Request, BaseUser, AsyncSession], Coroutine[Any, Any, None]]:
+    """Authorize against the backfill's Dag, or the Dag the request names when 
there is no backfill_id."""
 
     async def inner(
         request: Request,
         user: GetUserDep,
-        session: SessionDep,
+        session: AsyncSessionDep,

Review Comment:
   On `GET /backfills/{backfill_id}`, this session now holds an async 
connection until the sync handler returns. The PUT and create routes already do 
this through the audit log, but the GET routes and dry run have none.
   
   Could we open a short session here instead, as `execution_api/security.py` 
does?
   
   ```python
   async with create_session_async() as session:
       dag_id = await session.scalar(select(Backfill.dag_id).where(Backfill.id 
== backfill_id))
       ...
   ```



##########
airflow-core/src/airflow/models/dag.py:
##########
@@ -896,17 +898,34 @@ def calculate_dagrun_date_fields(
         )
 
     @staticmethod
-    @cached(_team_name_cache, key=lambda dag_id, **_: dag_id, 
lock=_team_name_cache_lock)
-    @provide_session
-    def get_team_name(dag_id: str, *, session: Session = NEW_SESSION) -> str | 
None:
-        """Return the team name associated to a Dag or None if it is not owned 
by a specific team."""
-        stmt = (
+    def _build_team_name_query(dag_id: str) -> Select[tuple[str]]:
+        return (
             select(Team.name)
             .join(DagBundleModel.teams)
             .join(DagModel, DagModel.bundle_name == DagBundleModel.name)
             .where(DagModel.dag_id == dag_id)
         )
-        return session.scalar(stmt)
+
+    @staticmethod
+    @cached(_team_name_cache, key=lambda dag_id, **_: dag_id, 
lock=_team_name_cache_lock)
+    @provide_session
+    def get_team_name(dag_id: str, *, session: Session = NEW_SESSION) -> str | 
None:
+        """Return the team name associated to a Dag or None if it is not owned 
by a specific team."""
+        return session.scalar(DagModel._build_team_name_query(dag_id))
+
+    @staticmethod
+    async def get_team_name_async(dag_id: str, *, session: AsyncSession) -> 
str | None:
+        """Return the team name of a Dag like :meth:`get_team_name`, sharing 
its cache."""
+        # The lock is a threading.Lock: held across the await, it would 
deadlock the next coroutine
+        # on this event loop that looks up a team name.
+        with _team_name_cache_lock:

Review Comment:
   This copies the `@cached` key and lock by hand. If the sync decorator 
changes, the two paths drift silently. Could both share a small cache get/put 
helper?



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