Fix: Enforce session capacity on restore and prevent session-creation race#268
Open
OmShrivastava19 wants to merge 4 commits into
Open
Fix: Enforce session capacity on restore and prevent session-creation race#268OmShrivastava19 wants to merge 4 commits into
OmShrivastava19 wants to merge 4 commits into
Conversation
Added last_access attribute to track session activity and implemented a background task to unload inactive sessions after 24 hours of idleness.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds session capacity protections and automatic cleanup of idle sessions, plus unit tests around restoring sessions at capacity.
Changes:
- Track per-session
last_accessand introduce a background loop to unload/persist sessions idle > 24 hours. - Add a placeholder “reservation” session during
create_session()to avoid concurrent creations exceedingMAX_SESSIONS. - Add tests for
ensure_session_loaded()behavior when restoring at/under server capacity.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| tests/unit/test_session_capacity.py | New async unit tests validating restore behavior at capacity and under capacity. |
| backend/session_manager.py | Implements idle unloading, last_access tracking, restore capacity check, and capacity reservation placeholder during creation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pull request adds a background cleanup mechanism to automatically unload long-idle sessions from memory, improves session resource management, and introduces robust session capacity checks to prevent server overload. It also adds new unit tests to verify session capacity logic. The most important changes are:
Session Idle Unloading and Resource Management:
_unload_inactive_sessions_loop) inSessionManagerthat periodically (every 10 minutes) unloads sessions idle for more than 24 hours, persisting their state and freeing server memory. The task is started on manager startup and properly cancelled on shutdown. [1] [2] [3]last_accessattribute inAgentSessionto track the last time a session was used, ensuring accurate idle detection. This timestamp is updated on session creation, restoration, and access. [1] [2] [3] [4] [5] [6]Session Capacity Enforcement:
AgentSessionobjects, preventing race conditions where more than the allowed number of sessions could be created concurrently. [1] [2] [3]ensure_session_loaded) to check server capacity before loading a session from persistence, denying restoration if the server is at maximum session capacity and logging a warning.Testing:
tests/unit/test_session_capacity.pyto verify that session restoration is denied when at capacity and allowed when under capacity, ensuring correct enforcement of session limits.