This is an automated email from the ASF dual-hosted git repository.

Miretpl pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 3892ada430a Fix unclosed sessions on concurrent first use of the MS 
Graph hook (#77202)
3892ada430a is described below

commit 3892ada430a4d00a71573fa32028aacc157a96f9
Author: David Blain <[email protected]>
AuthorDate: Thu Nov 19 23:57:15 2026 +0100

    Fix unclosed sessions on concurrent first use of the MS Graph hook (#77202)
    
    Coroutines that used the same connection for the first time at once, such
    as several triggers starting together or async tasks sharing an event loop,
    each found the adapter cache empty and built their own request adapter
    while awaiting the connection. Only the last one stayed in the cache. The
    others could no longer be reached by close(), so their credential sessions
    were never closed and asyncio reported "Unclosed client session" when they
    were garbage collected. Each of them also requested its own access token
    and opened its own connection to the API.
    
    Co-authored-by: Przemysław Mirowski 
<[email protected]>
---
 .../providers/microsoft/azure/hooks/msgraph.py       |  9 +++++++--
 .../tests/unit/microsoft/azure/hooks/test_msgraph.py | 20 ++++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git 
a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/msgraph.py
 
b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/msgraph.py
index b0e62b85d45..c12fb9d501f 100644
--- 
a/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/msgraph.py
+++ 
b/providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/msgraph.py
@@ -510,8 +510,13 @@ class KiotaRequestAdapterHook(BaseHook):
             connection = await get_async_connection(conn_id=self.conn_id)
             # Masks the extra's secrets without a synchronous call to the Task 
SDK on the event loop.
             config = await get_async_extra_dejson(connection)
-            api_version, request_adapter = 
self._build_request_adapter(connection, config)
-            self.cached_request_adapters[self.conn_id] = (api_version, 
request_adapter)
+            # Another coroutine may have cached an adapter while this one 
awaited the connection. Nothing
+            # awaits between this lookup and the write below, so a single 
adapter is built per connection.
+            api_version, request_adapter = 
self.cached_request_adapters.get(self.conn_id, (None, None))
+
+            if not request_adapter:
+                api_version, request_adapter = 
self._build_request_adapter(connection, config)
+                self.cached_request_adapters[self.conn_id] = (api_version, 
request_adapter)
 
         self.api_version = api_version
         self.allowed_netloc = urlparse(request_adapter.base_url).netloc.lower()
diff --git 
a/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_msgraph.py 
b/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_msgraph.py
index bc46d6e2495..aa323f1582d 100644
--- a/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_msgraph.py
+++ b/providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_msgraph.py
@@ -124,6 +124,26 @@ class TestKiotaRequestAdapterHook:
             assert isinstance(actual, HttpxRequestAdapter)
             assert actual.base_url == "https://graph.microsoft.com/v1.0/";
 
+    @pytest.mark.asyncio
+    async def 
test_get_async_conn_builds_one_adapter_for_concurrent_callers(self):
+        """Hooks awaiting the same connection for the first time at once share 
a single request adapter."""
+        with (
+            patch_hook(),
+            patch.object(
+                KiotaRequestAdapterHook,
+                "_build_request_adapter",
+                autospec=True,
+                side_effect=KiotaRequestAdapterHook._build_request_adapter,
+            ) as mock_build_request_adapter,
+        ):
+            adapters = await asyncio.gather(
+                
*(KiotaRequestAdapterHook(conn_id="msgraph_api").get_async_conn() for _ in 
range(5))
+            )
+
+        mock_build_request_adapter.assert_called_once()
+        assert all(adapter is adapters[0] for adapter in adapters)
+        assert 
KiotaRequestAdapterHook.cached_request_adapters["msgraph_api"][1] is adapters[0]
+
     @pytest.mark.asyncio
     async def test_get_async_conn_with_custom_base_url(self):
         with patch_hook(

Reply via email to