-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix: Enforce session capacity on restore and prevent session-creation race #268
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
Open
OmShrivastava19
wants to merge
4
commits into
huggingface:main
Choose a base branch
from
OmShrivastava19:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+563
−122
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
18786d9
Track last access time and unload inactive sessions
OmShrivastava19 635f57c
Created test_session_capacity
OmShrivastava19 6663d04
Potential fix for pull request finding
OmShrivastava19 304c100
backend: enforce session caps and add idle unload tests
OmShrivastava19 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import asyncio | ||
| import sys | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| import types | ||
|
|
||
| # Prevent importing heavy third-party modules when importing the backend module. | ||
| for _mod in ("litellm", "fastmcp", "thefuzz", "huggingface_hub"): | ||
| if _mod not in sys.modules: | ||
| m = types.ModuleType(_mod) | ||
| # fastmcp is imported as `from fastmcp import Client` in some codepaths | ||
| if _mod == "fastmcp": | ||
| class _DummyClient: | ||
| pass | ||
|
|
||
| setattr(m, "Client", _DummyClient) | ||
| sys.modules[_mod] = m | ||
|
OmShrivastava19 marked this conversation as resolved.
Outdated
OmShrivastava19 marked this conversation as resolved.
Outdated
|
||
|
|
||
| _BACKEND_DIR = Path(__file__).resolve().parent.parent.parent / "backend" | ||
| if str(_BACKEND_DIR) not in sys.path: | ||
| sys.path.insert(0, str(_BACKEND_DIR)) | ||
|
|
||
| from session_manager import SessionManager, AgentSession, MAX_SESSIONS | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_restore_denied_when_at_capacity(caplog): | ||
| manager = SessionManager() | ||
| # Fill in-memory sessions up to MAX_SESSIONS | ||
| for i in range(MAX_SESSIONS): | ||
| manager.sessions[str(i)] = AgentSession( | ||
| session_id=str(i), | ||
| session=object(), | ||
| tool_router=None, | ||
| submission_queue=asyncio.Queue(), | ||
| is_active=True, | ||
| ) | ||
|
|
||
| class DummyStore: | ||
| enabled = True | ||
|
|
||
| async def load_session(self, sid): | ||
| return {"metadata": {"user_id": "test", "model": "gpt"}, "messages": []} | ||
|
|
||
| manager.persistence_store = DummyStore() | ||
|
|
||
| caplog.set_level("WARNING") | ||
| res = await manager.ensure_session_loaded("restored", user_id="test") | ||
| assert res is None | ||
| assert any("Cannot restore session" in rec.message for rec in caplog.records) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_restore_allowed_under_capacity(monkeypatch): | ||
| manager = SessionManager() | ||
| manager.sessions.clear() | ||
|
|
||
| class DummyStore: | ||
| enabled = True | ||
|
|
||
| async def load_session(self, sid): | ||
| return {"metadata": {"user_id": "test", "model": "gpt"}, "messages": []} | ||
|
|
||
| manager.persistence_store = DummyStore() | ||
|
|
||
| # Replace the heavy sync constructor with a lightweight fake | ||
| def fake_create_session_sync(*, session_id, user_id, hf_username, hf_token, model, event_queue, notification_destinations): | ||
| class SimpleSession: | ||
| def __init__(self): | ||
| self.context_manager = SimpleNamespace(items=[object()]) | ||
| self.pending_approval = None | ||
| self.turn_count = 0 | ||
|
|
||
| return None, SimpleSession() | ||
|
|
||
| monkeypatch.setattr(manager, "_create_session_sync", fake_create_session_sync) | ||
|
|
||
| # Fake _start_agent_session to register the session without starting tasks | ||
| async def fake_start_agent_session(*, agent_session, event_queue, tool_router): | ||
| async with manager._lock: | ||
| manager.sessions[agent_session.session_id] = agent_session | ||
| return agent_session | ||
|
|
||
| monkeypatch.setattr(manager, "_start_agent_session", fake_start_agent_session) | ||
|
|
||
| res = await manager.ensure_session_loaded("restored", user_id="test") | ||
| assert res is not None | ||
| assert res.session is not None | ||
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.