The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
test(bedrock): detect Vault reads on the event-loop thread (#47597)
Mateo Wang4b1928387f41
diff --git a/tests/unit/llms/bedrock/count_tokens/test_bedrock_count_tokens_handler.py b/tests/unit/llms/bedrock/count_tokens/test_bedrock_count_tokens_handler.pyindex 5dadeff6d7c..591205b4602 100644--- a/tests/unit/llms/bedrock/count_tokens/test_bedrock_count_tokens_handler.py+++ b/tests/unit/llms/bedrock/count_tokens/test_bedrock_count_tokens_handler.py@@ -81,7 +81,7 @@ async def test_handle_count_tokens_request_looks_up_its_region_off_the_event_loo)assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert result == {"input_tokens": 7}diff --git a/tests/unit/llms/bedrock/count_tokens/test_bedrock_mantle_count_tokens_handler.py b/tests/unit/llms/bedrock/count_tokens/test_bedrock_mantle_count_tokens_handler.pyindex 2d21a3ddef5..a6b309c1b83 100644--- a/tests/unit/llms/bedrock/count_tokens/test_bedrock_mantle_count_tokens_handler.py+++ b/tests/unit/llms/bedrock/count_tokens/test_bedrock_mantle_count_tokens_handler.py@@ -124,6 +124,6 @@ async def test_region_and_endpoint_lookups_leave_the_event_loop_free(monkeypatch)assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert result == {"input_tokens": 2177}assert str(mantle.only_request().url) == MANTLE_COUNT_URLdiff --git a/tests/unit/llms/bedrock/event_loop_probe.py b/tests/unit/llms/bedrock/event_loop_probe.pyindex cf6cd5975ed..8bf856582b5 100644--- a/tests/unit/llms/bedrock/event_loop_probe.py+++ b/tests/unit/llms/bedrock/event_loop_probe.py@@ -16,7 +16,6 @@ from litellm.types.secret_managers.main import KeyManagementSettings, KeyManagemREFRESH_RELEASE_TIMEOUT_SECONDS: Final = 2.0REFRESH_START_TIMEOUT_SECONDS: Final = 10.0-LOOP_SERVE_TIMEOUT_SECONDS: Final = 2.0class EventLoopProbe:@@ -63,26 +62,21 @@ class EventLoopProbe:class LoopProbingVault:- """A Vault holding none of its hosted secrets, whose read waits for the event loop to run a callback.+ """A Vault with no hosted secrets that records reads on the event-loop thread."""- A read made on the loop thread can never see that callback, so `stalled_the_loop` is set only then.- """-- def __init__(self, loop: asyncio.AbstractEventLoop) -> None:- self._loop: Final = loop+ def __init__(self) -> None:+ self._loop_thread_id: Final = threading.get_ident()self.read: Final = threading.Event()- self.stalled_the_loop: Final = threading.Event()+ self.read_on_loop: Final = threading.Event()def sync_read_secret(self, secret_name: str) -> None:self.read.set()- served: Final = threading.Event()- self._loop.call_soon_threadsafe(served.set)- if not served.wait(timeout=LOOP_SERVE_TIMEOUT_SECONDS):- self.stalled_the_loop.set()+ if threading.get_ident() == self._loop_thread_id:+ self.read_on_loop.set()def host_in_loop_probing_vault(monkeypatch: pytest.MonkeyPatch, *secret_names: str) -> LoopProbingVault:- vault: Final = LoopProbingVault(asyncio.get_running_loop())+ vault: Final = LoopProbingVault()monkeypatch.setattr(litellm, "secret_manager_client", vault)monkeypatch.setattr(litellm, "_key_management_system", KeyManagementSystem.HASHICORP_VAULT)monkeypatch.setattr(diff --git a/tests/unit/llms/bedrock/realtime/test_bedrock_realtime_handler.py b/tests/unit/llms/bedrock/realtime/test_bedrock_realtime_handler.pyindex ccea4ba44d7..2ac31b6887d 100644--- a/tests/unit/llms/bedrock/realtime/test_bedrock_realtime_handler.py+++ b/tests/unit/llms/bedrock/realtime/test_bedrock_realtime_handler.py@@ -643,7 +643,7 @@ class TestBedrockRealtimeSessionLifecycle:)assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert stub_aws_sdk_client["config_kwargs"]["region"] == "us-west-2"assert stub_aws_sdk_client["config_kwargs"]["endpoint_uri"] == "https://bedrock-runtime.us-west-2.amazonaws.com"diff --git a/tests/unit/llms/bedrock/test_claude_platform_provider.py b/tests/unit/llms/bedrock/test_claude_platform_provider.pyindex e43184df50e..2353a45139b 100644--- a/tests/unit/llms/bedrock/test_claude_platform_provider.py+++ b/tests/unit/llms/bedrock/test_claude_platform_provider.py@@ -705,7 +705,7 @@ async def test_anthropic_messages_claude_platform_workspace_and_region_lookups_l)assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert response["content"][0]["text"] == "ok"_assert_gateway_accepted(requests[0], sigv4_without_region)diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.pyindex 205c610a413..63cc45b29df 100644--- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py+++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py@@ -1392,7 +1392,7 @@ async def test_bedrock_secret_lookups_leave_the_event_loop_free(_ = await call()assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert ".us-west-2." in upstream.calls.last.request.url.hostdiff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.pyindex 75d713028d4..69b3e113b8f 100644--- a/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py+++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py@@ -8103,5 +8103,5 @@ async def test_guardrail_secret_lookups_leave_the_event_loop_free()assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert upstream.calls.last.request.url.host == "bedrock-runtime.us-west-2.amazonaws.com"diff --git a/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.pyindex 66c52a76318..0186a33dbc4 100644--- a/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py+++ b/tests/unit/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py@@ -2558,7 +2558,7 @@ async def test_bedrock_agent_runtime_region_lookup_leaves_the_event_loop_free(mo)assert vault.read.is_set()- assert not vault.stalled_the_loop.is_set()+ assert not vault.read_on_loop.is_set()assert retrieve.calls.last.request.headers["authorization"].startswith("AWS4-HMAC-SHA256")