You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2025/09/17 23:14:17 UTC

[PR] Fix async connection retrieval in triggerer context [airflow]

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

   Add async support for secrets backends in _async_get_connection using sync_to_async to prevent event loop blocking. This resolves the 'async_to_sync forbidden in event loop' error when triggers call get_connection() and secrets backends need to be checked.
   
   Also fix Databricks provider to use async connection methods in async contexts instead of cached properties that cause sync calls.
   
   Fixes triggerer failures when providers access connections during async trigger execution.
   
   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix async connection retrieval in triggerer context [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #55812:
URL: https://github.com/apache/airflow/pull/55812#discussion_r2359657460


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -172,13 +172,33 @@ def _get_connection(conn_id: str) -> Connection:
 
 
 async def _async_get_connection(conn_id: str) -> Connection:
-    # TODO: add async support for secrets backends
+    from asgiref.sync import sync_to_async
 
     from airflow.sdk.execution_time.comms import GetConnection
+    from airflow.sdk.execution_time.supervisor import ensure_secrets_backend_loaded
     from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
 
-    msg = await SUPERVISOR_COMMS.asend(GetConnection(conn_id=conn_id))
+    # TODO: check cache first
+    # enabled only if SecretCache.init() has been called first
 
+    # Try secrets backends first using async wrapper
+    backends = ensure_secrets_backend_loaded()
+    for secrets_backend in backends:
+        try:
+            conn = await sync_to_async(secrets_backend.get_connection)(conn_id)

Review Comment:
   It's a different value of `secrets_backed` in every iteration though



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix async connection retrieval in triggerer context [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #55812:
URL: https://github.com/apache/airflow/pull/55812


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix async connection retrieval in triggerer context [airflow]

Posted by "Programmer-RD-AI (via GitHub)" <gi...@apache.org>.
Programmer-RD-AI commented on code in PR #55812:
URL: https://github.com/apache/airflow/pull/55812#discussion_r2358330003


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -172,13 +172,33 @@ def _get_connection(conn_id: str) -> Connection:
 
 
 async def _async_get_connection(conn_id: str) -> Connection:
-    # TODO: add async support for secrets backends
+    from asgiref.sync import sync_to_async
 
     from airflow.sdk.execution_time.comms import GetConnection
+    from airflow.sdk.execution_time.supervisor import ensure_secrets_backend_loaded
     from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
 
-    msg = await SUPERVISOR_COMMS.asend(GetConnection(conn_id=conn_id))
+    # TODO: check cache first
+    # enabled only if SecretCache.init() has been called first
 
+    # Try secrets backends first using async wrapper
+    backends = ensure_secrets_backend_loaded()
+    for secrets_backend in backends:
+        try:
+            conn = await sync_to_async(secrets_backend.get_connection)(conn_id)

Review Comment:
   maybe just pull `sync_to_async(secrets_backend.get_connection)` up once and reuse it? feels a bit cleaner than wrapping it every loop



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix async connection retrieval in triggerer context [airflow]

Posted by "Programmer-RD-AI (via GitHub)" <gi...@apache.org>.
Programmer-RD-AI commented on code in PR #55812:
URL: https://github.com/apache/airflow/pull/55812#discussion_r2359666549


##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -172,13 +172,33 @@ def _get_connection(conn_id: str) -> Connection:
 
 
 async def _async_get_connection(conn_id: str) -> Connection:
-    # TODO: add async support for secrets backends
+    from asgiref.sync import sync_to_async
 
     from airflow.sdk.execution_time.comms import GetConnection
+    from airflow.sdk.execution_time.supervisor import ensure_secrets_backend_loaded
     from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
 
-    msg = await SUPERVISOR_COMMS.asend(GetConnection(conn_id=conn_id))
+    # TODO: check cache first
+    # enabled only if SecretCache.init() has been called first
 
+    # Try secrets backends first using async wrapper
+    backends = ensure_secrets_backend_loaded()
+    for secrets_backend in backends:
+        try:
+            conn = await sync_to_async(secrets_backend.get_connection)(conn_id)

Review Comment:
   ah, sorry i had missed it 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org