Skip to content

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]

fix(proxy): serialize the search tool router refresh

Yuneng Jiang3576c773eb31
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 7434ac3c494..18cc57cd135 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -7491,12 +7491,19 @@ class ProxyConfig:
Driven by the management endpoints so the worker that served the write is correct
immediately, and by the periodic job in store_model_in_db-off deployments. Gated the same
way as startup, so an admin who excluded search_tools from supported_db_objects opts out.
+
+ Serialized by MODEL_RECONCILE_LOCK for the reason add_deployment documents: the body is a
+ read-modify-write of the shared ``llm_router`` global, so two of them interleaving lets the
+ older snapshot's wholesale assignment land last and restore a tool the newer one deleted.
+ The lock belongs here rather than in _init_search_tools_in_db, which _init_non_llm_objects_in_db
+ already calls while holding it.
"""
if not self._should_load_db_object(object_type="search_tools"):
return
if prisma_client is None:
return
- await self._init_search_tools_in_db(prisma_client=prisma_client)
+ async with MODEL_RECONCILE_LOCK:
+ await self._init_search_tools_in_db(prisma_client=prisma_client)
@staticmethod
def _merge_config_and_db_search_tools(
diff --git a/tests/test_litellm/proxy/proxy_server/test_proxy_config.py b/tests/test_litellm/proxy/proxy_server/test_proxy_config.py
index 7678b0cab9e..d1dada4d10e 100644
--- a/tests/test_litellm/proxy/proxy_server/test_proxy_config.py
+++ b/tests/test_litellm/proxy/proxy_server/test_proxy_config.py
@@ -1304,6 +1304,51 @@ async def test_ProxyConfig_reload_search_tools_from_db_honors_supported_db_objec
mock_init.assert_not_awaited()
+async def test_ProxyConfig_reload_search_tools_from_db_serializes_overlapping_refreshes(monkeypatch):
+ """An older snapshot must not land last and restore a tool a newer refresh deleted."""
+ import asyncio
+
+ from litellm.proxy import proxy_server
+
+ pc = ProxyConfig()
+ pc.update_config_state({})
+ fake_router = MagicMock()
+ fake_router.search_tools = []
+
+ stale_read_started = asyncio.Event()
+ fresh_write_committed = asyncio.Event()
+ snapshots = iter(
+ (
+ [{"search_tool_name": "doomed-search", "litellm_params": {}}],
+ [],
+ )
+ )
+
+ async def _read_db(**_):
+ snapshot = next(snapshots)
+ if not stale_read_started.is_set():
+ stale_read_started.set()
+ await fresh_write_committed.wait()
+ return snapshot
+
+ monkeypatch.setattr(proxy_server, "llm_router", fake_router)
+ monkeypatch.setattr(proxy_server, "prisma_client", MagicMock())
+ monkeypatch.setattr(
+ "litellm.proxy.search_endpoints.search_tool_registry.SearchToolRegistry.get_all_search_tools_from_db",
+ _read_db,
+ )
+
+ stale = asyncio.create_task(pc.reload_search_tools_from_db())
+ await stale_read_started.wait()
+ deleter = asyncio.create_task(pc.reload_search_tools_from_db())
+ await asyncio.sleep(0)
+ fresh_write_committed.set()
+ await asyncio.gather(stale, deleter)
+
+ assert fake_router.search_tools == []
+
+
@pytest.mark.asyncio
async def test_ProxyConfig_reload_search_tools_from_db_noops_without_prisma(monkeypatch):
from litellm.proxy import proxy_server