jason810496 commented on code in PR #65587: URL: https://github.com/apache/airflow/pull/65587#discussion_r3213032857
########## airflow-core/src/airflow/process_context.py: ########## @@ -0,0 +1,53 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +import sys +from collections.abc import Generator +from contextlib import contextmanager +from contextvars import ContextVar +from typing import Literal + +_PROCESS_CONTEXT_OVERRIDE: ContextVar[str | None] = ContextVar( + "_AIRFLOW_PROCESS_CONTEXT_OVERRIDE", + default=None, +) + Review Comment: That's true, we avoid adding more module into utils/. The new module location LGTM. ########## airflow-core/src/airflow/process_context.py: ########## @@ -0,0 +1,59 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +import sys +from collections.abc import Generator +from contextlib import contextmanager +from contextvars import ContextVar +from typing import Literal + +__all__ = [ + "get_process_context", + "override_process_context", + "should_use_task_sdk_api_path", +] + +_PROCESS_CONTEXT_OVERRIDE: ContextVar[str | None] = ContextVar( + "_AIRFLOW_PROCESS_CONTEXT_OVERRIDE", + default=None, +) Review Comment: After thinking throughly, I'm not sure whether adding the `ContextVar` here will help resolve the issue or not. ########## airflow-core/src/airflow/models/variable.py: ########## @@ -241,7 +241,7 @@ def set( # If this is set it means we are in some kind of execution context (Task, Dag Parse or Triggerer perhaps) # and should use the Task SDK API server path - if hasattr(sys.modules.get("airflow.sdk.execution_time.task_runner"), "SUPERVISOR_COMMS"): + if should_use_task_sdk_api_path(): Review Comment: As the original statement pointed out, we need to ensure the `airflow.sdk.execution_time.task_runner.SUPERVISOR_COMMS` is initialized before going with Task SDK API path. ########## airflow-core/src/airflow/api_fastapi/execution_api/app.py: ########## @@ -300,6 +301,17 @@ def get_extra_schemas() -> dict[str, dict]: } +class _RequestScopedServerContextApp: + """Wrap an ASGI app so in-process requests behave like server-side API handling.""" + + def __init__(self, app: FastAPI) -> None: + self.app = app + + async def __call__(self, scope: Any, receive: Any, send: Any) -> None: + with override_process_context("server"): + await self.app(scope, receive, send) Review Comment: If we override the `ContextVar` to `server` but the `airflow.sdk.execution_time.task_runner.SUPERVISOR_COMMS` hasn't been initialized. It seems we will still encounter the same issue. -- 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]
