-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[Feat] Cancel upstream LLM request on client disconnect #25776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
CreateRandom
wants to merge
4
commits into
BerriAI:litellm_internal_staging
from
CreateRandom:cancel-upstream-on-client-disconnect
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
26330c8
feat(proxy): cancel upstream LLM request on client disconnect
CreateRandom c5f48cc
fix(proxy): address review comments on client disconnect feature
CreateRandom 65bed54
fix(proxy): skip post_call_failure_hook for 499 client disconnect
CreateRandom c9c03eb
style: apply black formatting
CreateRandom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """ | ||
| Test client disconnection detection functionality. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import pytest | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from litellm.proxy.common_request_processing import _check_request_disconnection | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_request_disconnection_with_disconnect(): | ||
| """Test that _check_request_disconnection cancels task and sets event when client disconnects.""" | ||
| mock_request = AsyncMock() | ||
| mock_request.receive.side_effect = [ | ||
| {"type": "http.request"}, # First call | ||
| {"type": "http.disconnect"}, # Second call - disconnect | ||
| ] | ||
|
|
||
| mock_llm_task = MagicMock() # sync mock so .cancel() doesn't return a coroutine | ||
| disconnect_event = asyncio.Event() | ||
|
|
||
| with patch( | ||
| "litellm.proxy.common_request_processing.asyncio.sleep", new_callable=AsyncMock | ||
| ): | ||
| await _check_request_disconnection( | ||
| mock_request, mock_llm_task, disconnect_event | ||
| ) | ||
|
|
||
| mock_llm_task.cancel.assert_called_once() | ||
|
CreateRandom marked this conversation as resolved.
|
||
| assert disconnect_event.is_set() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_request_disconnection_no_disconnect(): | ||
| """Test that _check_request_disconnection does not cancel task during normal operation.""" | ||
| mock_request = AsyncMock() | ||
| mock_request.receive.return_value = {"type": "http.request"} | ||
|
|
||
| mock_llm_task = MagicMock() # sync mock so .cancel() doesn't return a coroutine | ||
| disconnect_event = asyncio.Event() | ||
|
|
||
| task = asyncio.create_task( | ||
| _check_request_disconnection(mock_request, mock_llm_task, disconnect_event) | ||
| ) | ||
| await asyncio.sleep(0.1) # Let it run briefly | ||
| task.cancel() | ||
|
|
||
| try: | ||
| await task | ||
| except asyncio.CancelledError: | ||
| pass | ||
|
|
||
| mock_llm_task.cancel.assert_not_called() | ||
| assert not disconnect_event.is_set() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.