From c7916a137d1ebd262a2b4a53413ff7636ca6a91b Mon Sep 17 00:00:00 2001 From: agustin18 Date: Tue, 20 Oct 2026 15:45:50 +0000 Subject: [PATCH] fix(presidio): release session lock before analyzer and anonymizer calls (#47579) --- .../guardrails/guardrail_hooks/presidio.py | 8 ++-- tests/test_presidio_latency.py | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index fe91d6d7a28..3bde6d79303 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -243,7 +243,8 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): async with self._session_lock: if self._http_session is None or self._http_session.closed: self._http_session = aiohttp.ClientSession() - yield self._http_session + session = self._http_session + yield session else: # Background thread/loop -> use loop-bound session cache # This avoids "attached to a different loop" or "no running event loop" errors @@ -487,9 +488,8 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): # The semaphore is shared per event loop across every chunked call on # this instance, so many oversized blocks in one request (or many # concurrent requests) still hold at most this many analyzer calls in - # flight. On the proxy's main thread the shared-session lock in - # _get_session_iterator additionally serializes the HTTP calls; the - # bound matters for loop-bound sessions (background threads). + # flight. This bound applies to both the shared session and the + # loop-bound sessions used by background threads. analyze_semaphore: Final = self._get_chunk_semaphore() async def _analyze_chunk_bounded( diff --git a/tests/test_presidio_latency.py b/tests/test_presidio_latency.py index 40a2cc42b25..3c9162ada4c 100644 --- a/tests/test_presidio_latency.py +++ b/tests/test_presidio_latency.py @@ -79,3 +79,43 @@ async def test_bug_presidio_session_explosion_background_thread_causes_latency() assert session_creations == 1 await presidio._close_http_session() + + +@pytest.mark.asyncio +async def test_session_lock_is_not_held_while_request_uses_session(): + """ + The shared session lock must only protect session creation. + + If it is held while callers use the session, every Presidio HTTP request in + a proxy process is serialized even though async_pre_call_hook fans out with + asyncio.gather. + """ + + class DummySession: + closed = False + + async def close(self): + self.closed = True + + presidio = _OPTIONAL_PresidioPIIMasking( + mock_testing=True, + presidio_analyzer_api_base="http://mock-analyzer", + presidio_anonymizer_api_base="http://mock-anonymizer", + ) + presidio._http_session = DummySession() + + entered = 0 + both_entered = asyncio.Event() + + async def use_session(): + nonlocal entered + async with presidio._get_session_iterator() as session: + assert session is presidio._http_session + entered += 1 + if entered == 2: + both_entered.set() + await asyncio.wait_for(both_entered.wait(), timeout=1.0) + + await asyncio.gather(use_session(), use_session()) + + await presidio._close_http_session()