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]

address greptile review: async sleep, SIGKILL Windows guard, trailing newlines

michelligabriele92b8e1acf8c1
diff --git a/litellm/proxy/db/prisma_client.py b/litellm/proxy/db/prisma_client.py
index fa00d117c81..82ee11a0f42 100644
--- a/litellm/proxy/db/prisma_client.py
+++ b/litellm/proxy/db/prisma_client.py
@@ -58,7 +58,7 @@ class PrismaWrapper:
return 0
@staticmethod
- def _kill_engine_process(pid: int) -> None:
+ async def _kill_engine_process(pid: int) -> None:
"""Force-kill an orphaned engine subprocess to prevent DB connection pool leaks.
Called when disconnect() fails and the old engine process may still be
@@ -76,9 +76,9 @@ class PrismaWrapper:
pid,
)
# Brief wait for graceful shutdown, then force-kill
- time.sleep(0.5)
+ await asyncio.sleep(0.5)
try:
- os.kill(pid, signal.SIGKILL)
+ os.kill(pid, getattr(signal, "SIGKILL", signal.SIGTERM))
verbose_proxy_logger.warning(
"Sent SIGKILL to prisma-query-engine PID %s (did not exit after SIGTERM).",
pid,
@@ -226,7 +226,7 @@ class PrismaWrapper:
await self._original_prisma.disconnect()
except Exception as e:
verbose_proxy_logger.warning(f"Failed to disconnect Prisma client: {e}")
- self._kill_engine_process(old_engine_pid)
+ await self._kill_engine_process(old_engine_pid)
if http_client is not None:
self._original_prisma = Prisma(http=http_client)
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index e067b4d3175..e3bf549ce31 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -4014,7 +4014,7 @@ class PrismaClient:
"Prisma DB disconnect before reconnect failed: %s",
disconnect_err,
)
- PrismaWrapper._kill_engine_process(old_pid)
+ await PrismaWrapper._kill_engine_process(old_pid)
await self.db.connect()
await self.db.query_raw("SELECT 1")
diff --git a/tests/test_litellm/proxy/db/test_prisma_client.py b/tests/test_litellm/proxy/db/test_prisma_client.py
index 9c62c6ffd50..f4ef933f219 100644
--- a/tests/test_litellm/proxy/db/test_prisma_client.py
+++ b/tests/test_litellm/proxy/db/test_prisma_client.py
@@ -106,7 +106,7 @@ async def test_recreate_prisma_client_kills_old_engine_on_disconnect_failure(
with (
patch("os.kill") as mock_kill,
- patch("time.sleep"),
+ patch("asyncio.sleep", new_callable=AsyncMock),
):
await wrapper.recreate_prisma_client("postgresql://new")
@@ -152,9 +152,9 @@ async def test_recreate_prisma_client_handles_missing_engine_pid(
with (
patch("os.kill") as mock_kill,
- patch("time.sleep"),
+ patch("asyncio.sleep", new_callable=AsyncMock),
):
await wrapper.recreate_prisma_client("postgresql://new")
mock_kill.assert_not_called() # PID was 0, kill skipped
- mock_new_prisma.connect.assert_awaited_once()
\ No newline at end of file
+ mock_new_prisma.connect.assert_awaited_once()
diff --git a/tests/test_litellm/proxy/db/test_prisma_self_heal.py b/tests/test_litellm/proxy/db/test_prisma_self_heal.py
index dbe1f2113b1..62fb1b5189c 100644
--- a/tests/test_litellm/proxy/db/test_prisma_self_heal.py
+++ b/tests/test_litellm/proxy/db/test_prisma_self_heal.py
@@ -293,7 +293,7 @@ async def test_lightweight_reconnect_kills_engine_on_disconnect_failure(mock_pro
with (
patch.object(client, "_get_engine_pid", return_value=9999),
patch("os.kill") as mock_kill,
- patch("time.sleep"),
+ patch("asyncio.sleep", new_callable=AsyncMock),
):
await client._run_reconnect_cycle(timeout_seconds=5.0)