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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_dbReconnectNonBlocking_local

Yuneng Jiang4f6192a49e9f
diff --cc litellm/proxy/utils.py
index dbf1cfc1acf,93cc8bfd22b..d2dfa177515
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@@ -4183,8 -4198,8 +4198,11 @@@ class PrismaClient
Uses the _engine_confirmed_dead flag (set by waitpid thread / pidfd / poll
handlers) to choose between heavy reconnect (engine dead -- recreate
-- Prisma client, re-arm watcher) and lightweight reconnect (network
-- blip -- disconnect, connect, SELECT 1).
++ Prisma client, re-arm watcher) and direct reconnect (network blip --
++ recreate Prisma client, re-arm watcher, SELECT 1). Both paths recreate
++ the client via the non-blocking kill-then-construct flow rather than
++ calling disconnect(), which blocks the event loop on the synchronous
++ subprocess.Popen.wait() inside prisma-client-py (see issue #26191).
"""
effective_timeout = (
timeout_seconds
diff --cc tests/test_litellm/proxy/db/test_prisma_self_heal.py
index 57693a1a175,af65cad09e3..3f9ba6af3af
--- a/tests/test_litellm/proxy/db/test_prisma_self_heal.py
+++ b/tests/test_litellm/proxy/db/test_prisma_self_heal.py
@@@ -344,22 -327,157 +345,144 @@@ async def test_recreate_prisma_client_k
client = PrismaClient(
database_url="mock://test", proxy_logging_obj=mock_proxy_logging
)
- client.db.disconnect = AsyncMock(side_effect=Exception("disconnect failed"))
- client.db.connect = AsyncMock(return_value=None)
- client.db.query_raw = AsyncMock(return_value=[{"result": 1}])
+ disconnect_mock = AsyncMock(
+ side_effect=AssertionError("disconnect must not be called on reconnect path")
+ )
+ client.db._original_prisma.disconnect = disconnect_mock
with (
- patch.object(client, "_get_engine_pid", return_value=9999),
- patch("os.kill") as mock_kill,
- patch("asyncio.sleep", new_callable=AsyncMock),
+ patch.object(client.db, "_get_engine_pid", return_value=9999),
+ patch("litellm.proxy.db.prisma_client.os.kill") as mock_kill,
+ patch("litellm.proxy.db.prisma_client.asyncio.sleep", new_callable=AsyncMock),
):
- await client._run_reconnect_cycle(timeout_seconds=5.0)
+ # Return a Prisma instance whose connect() is awaitable.
+ fake_new_prisma = MagicMock()
+ fake_new_prisma.connect = AsyncMock(return_value=None)
+ with patch("prisma.Prisma", return_value=fake_new_prisma):
+ await client.db.recreate_prisma_client("postgresql://test")
mock_kill.assert_any_call(9999, signal.SIGTERM)
- client.db.connect.assert_awaited_once()
- client.db.query_raw.assert_awaited_once_with("SELECT 1")
-
-
-async def test_lightweight_reconnect_skips_kill_on_successful_disconnect(
- mock_proxy_logging,
-):
- """Lightweight reconnect must NOT kill when disconnect() succeeds."""
- client = PrismaClient(
- database_url="mock://test", proxy_logging_obj=mock_proxy_logging
- )
- client.db.disconnect = AsyncMock(return_value=None)
- client.db.connect = AsyncMock(return_value=None)
- client.db.query_raw = AsyncMock(return_value=[{"result": 1}])
-
- with patch("os.kill") as mock_kill:
- await client._run_reconnect_cycle(timeout_seconds=5.0)
-
- mock_kill.assert_not_called()
+ disconnect_mock.assert_not_awaited()
+ fake_new_prisma.connect.assert_awaited_once()
+
+
+ # ---------------------------------------------------------------------------
+ # get_generic_data: transport-reconnect-and-retry coverage (issue #25143)
+ # ---------------------------------------------------------------------------
+
+
+ @pytest.mark.asyncio
+ async def test_get_generic_data_retries_on_transport_error_for_config_table(
+ mock_proxy_logging,
+ ):
+ """`get_generic_data(table_name="config")` self-heals on a transient
+ `httpx.ReadError`: reconnect once, retry once, return the row.
+
+ Regression for issue #25143 — the 1.83.x line lost the reconnect-and-retry
+ branch that 1.82.6 had on this method. `_update_config_from_db` fans out
+ four concurrent `get_generic_data` calls, so a single transport flap used
+ to surface as four `db_exceptions` alerts and a stale config window.
+ """
+ client = PrismaClient(
+ database_url="mock://test", proxy_logging_obj=mock_proxy_logging
+ )
+
+ expected_row = {"param_name": "general_settings", "param_value": {"foo": "bar"}}
+ invocations: list[None] = []
+
+ async def _flaky_find_first(**kwargs):
+ invocations.append(None)
+ if len(invocations) == 1:
+ raise httpx.ReadError("simulated transport blip")
+ return expected_row
+
+ client.db.litellm_config.find_first = AsyncMock(side_effect=_flaky_find_first)
+ client.attempt_db_reconnect = AsyncMock(return_value=True)
+
+ result = await client.get_generic_data(
+ key="param_name",
+ value="general_settings",
+ table_name="config",
+ )
+
+ assert result == expected_row
+ assert len(invocations) == 2
+ client.attempt_db_reconnect.assert_awaited_once()
+ reconnect_kwargs = client.attempt_db_reconnect.await_args.kwargs
+ assert reconnect_kwargs["reason"] == "prisma_get_generic_data_config_lookup_failure"
+
+ # The failure_handler telemetry side-effect must NOT fire on the first
+ # transport blip — only if the post-retry call also fails. Drain the
+ # event loop so any spuriously-spawned task would have run by now.
+ await asyncio.sleep(0)
+ mock_proxy_logging.failure_handler.assert_not_called()
+
+
+ @pytest.mark.asyncio
+ async def test_get_generic_data_propagates_when_reconnect_fails(mock_proxy_logging):
+ """If reconnect itself does not succeed, propagate the original transport
+ error and let the existing failure_handler / db_exceptions telemetry fire."""
+ client = PrismaClient(
+ database_url="mock://test", proxy_logging_obj=mock_proxy_logging
+ )
+
+ client.db.litellm_config.find_first = AsyncMock(
+ side_effect=httpx.ReadError("simulated transport blip")
+ )
+ client.attempt_db_reconnect = AsyncMock(return_value=False)
+
+ with pytest.raises(httpx.ReadError):
+ await client.get_generic_data(
+ key="param_name",
+ value="general_settings",
+ table_name="config",
+ )
+
+ client.attempt_db_reconnect.assert_awaited_once()
+ # Failure telemetry IS expected here — the read genuinely failed.
+ await asyncio.sleep(0)
+ mock_proxy_logging.failure_handler.assert_called_once()
+
+
+ # ---------------------------------------------------------------------------
+ # _engine_confirmed_dead flag-reset bug (B2)
+ # ---------------------------------------------------------------------------
+
+
+ @pytest.mark.asyncio
+ async def test_engine_confirmed_dead_persists_across_failed_heavy_reconnect(
+ mock_proxy_logging,
+ ):
+ """Regression test for the flag-reset bug.
+
+ Before the fix, `_run_reconnect_cycle` cleared
+ `self._engine_confirmed_dead = False` *before* awaiting
+ `_do_heavy_reconnect()`. If the heavy reconnect raised (e.g. timeout,
+ missing DATABASE_URL, recreate failure), the flag was left cleared and the
+ next attempt could demote to the lightweight path even though the engine
+ was genuinely dead.
+
+ The fix moves the reset into the success branch — the flag must stay True
+ when heavy reconnect raises.
+ """
+ client = PrismaClient(
+ database_url="mock://test", proxy_logging_obj=mock_proxy_logging
+ )
+ client._engine_confirmed_dead = True
+ client._engine_pid = 0 # so `_is_engine_alive` is not consulted
+
+ # Make the heavy reconnect path raise.
+ client.db.recreate_prisma_client = AsyncMock(
+ side_effect=RuntimeError("simulated heavy reconnect failure")
+ )
+ client._start_engine_watcher = AsyncMock()
+ client._cleanup_engine_watcher = MagicMock()
+ client._reap_all_zombies = MagicMock()
+
+ with patch.dict(os.environ, {"DATABASE_URL": "postgresql://test"}):
+ with pytest.raises(Exception):
+ await client._run_reconnect_cycle(timeout_seconds=5.0)
+
+ # The flag must STILL be True so the next attempt re-enters the heavy
+ # branch instead of silently demoting to the lightweight path.
+ assert client._engine_confirmed_dead is True