From afb117f5d325b7c4f3a5afb12a08f953d8eaaf7e Mon Sep 17 00:00:00 2001 From: Harshit28j Date: Tue, 10 Mar 2026 13:03:29 +0530 Subject: [PATCH] fix: req changes from greptile --- litellm/router.py | 21 ++++++++++--------- .../test_router_silent_experiment.py | 8 ++----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/litellm/router.py b/litellm/router.py index fac9409b359..06def6ceb4d 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -1501,20 +1501,21 @@ class Router: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: - loop.run_until_complete( - self.acompletion( + + async def _run_silent_completion(): + await self.acompletion( model=silent_model, messages=cast(List[AllMessageValues], messages), **silent_kwargs, ) - ) - # Drain any remaining fire-and-forget tasks (e.g. alerting hooks) - # scheduled via asyncio.create_task during the acompletion call. - pending = asyncio.all_tasks(loop) - if pending: - loop.run_until_complete( - asyncio.gather(*pending, return_exceptions=True) - ) + # Drain any fire-and-forget tasks (e.g. alerting hooks) + # scheduled via asyncio.create_task during acompletion. + pending = asyncio.all_tasks() + pending.discard(asyncio.current_task()) + if pending: + await asyncio.gather(*pending, return_exceptions=True) + + loop.run_until_complete(_run_silent_completion()) finally: loop.close() except Exception as e: diff --git a/tests/test_litellm/test_router_silent_experiment.py b/tests/test_litellm/test_router_silent_experiment.py index 79056805d54..67d262f83d4 100644 --- a/tests/test_litellm/test_router_silent_experiment.py +++ b/tests/test_litellm/test_router_silent_experiment.py @@ -1,4 +1,5 @@ import asyncio +import time from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -203,15 +204,10 @@ def test_router_silent_experiment_completion(): assert response.choices[0].message.content == "hello" - # The sync background call uses a thread pool. We might need to wait a bit. - import time - + # The sync background call uses a thread pool. We might need to wait. time.sleep(2.0) # Should have 1 acompletion call (the silent background call) - # The primary completion call still goes to the real litellm.completion (or we can mock it separately, but here it's testing the background one) - # Wait, the primary call in the test is router.completion. - # Actually, let's just mock both to avoid real network calls if it's hitting one. assert mock_acompletion_mock.call_count == 1 call_args_list = mock_acompletion_mock.call_args_list