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]

[Bug Fix] Infra - ensure that stale Prisma clients disconnect DB connection (#13140)

Ishaan Jaff115d2480c10a
diff --git a/litellm/proxy/db/prisma_client.py b/litellm/proxy/db/prisma_client.py
index c4edc702b32..02a75c0b0f7 100644
--- a/litellm/proxy/db/prisma_client.py
+++ b/litellm/proxy/db/prisma_client.py
@@ -87,6 +87,12 @@ class PrismaWrapper:
self, new_db_url: str, http_client: Optional[Any] = None
):
from prisma import Prisma # type: ignore
+ try:
+ await self._original_prisma.disconnect()
+ except Exception as e:
+ verbose_proxy_logger.warning(
+ f"Failed to disconnect Prisma client: {e}"
+ )
if http_client is not None:
self._original_prisma = Prisma(http=http_client)
diff --git a/tests/test_litellm/proxy/db/test_prisma_client.py b/tests/test_litellm/proxy/db/test_prisma_client.py
index c7e99aa7540..1d7459d5cca 100644
--- a/tests/test_litellm/proxy/db/test_prisma_client.py
+++ b/tests/test_litellm/proxy/db/test_prisma_client.py
@@ -1,6 +1,7 @@
import json
import os
import sys
+from unittest.mock import AsyncMock, Mock, patch
import pytest
from fastapi.testclient import TestClient
@@ -10,7 +11,7 @@ sys.path.insert(
) # Adds the parent directory to the system path
-from litellm.proxy.db.prisma_client import should_update_prisma_schema
+from litellm.proxy.db.prisma_client import PrismaWrapper, should_update_prisma_schema
def test_should_update_prisma_schema(monkeypatch):
@@ -33,3 +34,36 @@ def test_should_update_prisma_schema(monkeypatch):
monkeypatch.setenv("DISABLE_SCHEMA_UPDATE", None) # Set env var opposite to param
assert should_update_prisma_schema(False) == True # Param False -> should update
+
+
+async def test_recreate_prisma_client_successful_disconnect():
+ """
+ Test that recreate_prisma_client works normally when disconnect succeeds.
+ """
+ # Mock the original prisma client
+ mock_prisma = AsyncMock()
+
+ # Create a PrismaWrapper instance
+ wrapper = PrismaWrapper(original_prisma=mock_prisma, iam_token_db_auth=False)
+
+ # Configure disconnect to succeed
+ mock_prisma.disconnect.return_value = None
+
+ # Mock the Prisma class constructor
+ with patch("prisma.Prisma") as mock_prisma_class:
+ mock_new_prisma = AsyncMock()
+ mock_prisma_class.return_value = mock_new_prisma
+
+ # Call the method
+ await wrapper.recreate_prisma_client("postgresql://new:new@localhost:5432/new")
+
+ # Verify that disconnect was called
+ mock_prisma.disconnect.assert_called_once()
+
+ # Verify that a new Prisma client was created and connected
+ mock_prisma_class.assert_called_once()
+ mock_new_prisma.connect.assert_called_once()
+
+ # Verify that the new client replaced the original
+ assert wrapper._original_prisma == mock_new_prisma