From efddf5fb9dfb3c64ea842ac5e5c315ea47d24e62 Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Mon, 20 Oct 2025 15:19:11 -0700 Subject: [PATCH] Refactor locking mechanism to use asyncio.Lock for asynchronous container initialization --- .../microsoft_agents/storage/cosmos/cosmos_db_storage.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py index 16c3246c..fdd4f6f8 100644 --- a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py +++ b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py @@ -2,7 +2,7 @@ # Licensed under the MIT License. from typing import TypeVar, Union -from threading import Lock +import asyncio from azure.cosmos import ( documents, @@ -49,7 +49,7 @@ def __init__(self, config: CosmosDBStorageConfig): self._container: ContainerProxy = None self._compatability_mode_partition_key: bool = False # Lock used for synchronizing container creation - self._lock: Lock = Lock() + self._lock: asyncio.Lock = asyncio.Lock() def _create_client(self) -> CosmosClient: if self._config.url: @@ -164,8 +164,8 @@ async def _create_container(self) -> None: async def initialize(self) -> None: if not self._container: - with self._lock: - # in case another thread attempted to initialize just before acquiring the lock + async with self._lock: + # in case another async task attempted to initialize just before acquiring the lock if self._container: return