fix(chat-context): clean up session entries on disconnect#2959
Open
axelray-dev wants to merge 1 commit into
Open
fix(chat-context): clean up session entries on disconnect#2959axelray-dev wants to merge 1 commit into
axelray-dev wants to merge 1 commit into
Conversation
Co-Authored-By: Codex <[email protected]>
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.
Fixes #2951
Problem
The global
chat_contextsdict inbackend/chainlit/chat_context.pyaccumulates a message list persession_idbut never removes entries when sessions disconnect. Over time this causes unbounded memory growth and eventual OOM.Root Cause
The
disconnecthandler insocket.pycleans upuser_sessionsand callssession.delete(), but does not remove the session's entry fromchat_contexts.Fix
ChatContext.delete_session(session_id)to remove a session's entry fromchat_contexts.clear()function inside thedisconnecthandler, alongside the existinguser_sessionscleanup.Tests
3 new tests in
backend/tests/test_chat_context.py:test_delete_session_removes_entry: verifies entry is removed after adding a messagetest_delete_session_nonexistent: verifies no error for unknown session IDtest_delete_session_without_session: verifies deletion works without an active Chainlit contextAll 31 tests pass:
PYTHONPATH=. python -m pytest tests/test_chat_context.py -vSummary by cubic
Fixes a memory leak by deleting session entries from
chat_contextson disconnect. Prevents unbounded memory growth and OOM in long-running servers.ChatContext.delete_session(session_id)to remove a session’s chat context.clear()flow afteruser_sessionscleanup.Written for commit 91a73f8. Summary will update on new commits.