Skip to content

fix(chat-context): clean up session entries on disconnect#2959

Open
axelray-dev wants to merge 1 commit into
Chainlit:mainfrom
axelray-dev:fix/2951-chat-context-memory-leak
Open

fix(chat-context): clean up session entries on disconnect#2959
axelray-dev wants to merge 1 commit into
Chainlit:mainfrom
axelray-dev:fix/2951-chat-context-memory-leak

Conversation

@axelray-dev

@axelray-dev axelray-dev commented Jun 12, 2026

Copy link
Copy Markdown

Fixes #2951

Problem

The global chat_contexts dict in backend/chainlit/chat_context.py accumulates a message list per session_id but never removes entries when sessions disconnect. Over time this causes unbounded memory growth and eventual OOM.

Root Cause

The disconnect handler in socket.py cleans up user_sessions and calls session.delete(), but does not remove the session's entry from chat_contexts.

Fix

  1. Added ChatContext.delete_session(session_id) to remove a session's entry from chat_contexts.
  2. Called it from the clear() function inside the disconnect handler, alongside the existing user_sessions cleanup.

Tests

3 new tests in backend/tests/test_chat_context.py:

  • test_delete_session_removes_entry: verifies entry is removed after adding a message
  • test_delete_session_nonexistent: verifies no error for unknown session ID
  • test_delete_session_without_session: verifies deletion works without an active Chainlit context

All 31 tests pass: PYTHONPATH=. python -m pytest tests/test_chat_context.py -v


Summary by cubic

Fixes a memory leak by deleting session entries from chat_contexts on disconnect. Prevents unbounded memory growth and OOM in long-running servers.

  • Bug Fixes
    • Added ChatContext.delete_session(session_id) to remove a session’s chat context.
    • Called it in the socket disconnect clear() flow after user_sessions cleanup.
    • Added tests for deletion, nonexistent IDs, and no active context.

Written for commit 91a73f8. Summary will update on new commits.

Review in cubic

@axelray-dev axelray-dev marked this pull request as ready for review June 13, 2026 14:24
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. backend Pertains to the Python backend. bug Something isn't working unit-tests Has unit tests. labels Jun 13, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Pertains to the Python backend. bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files. unit-tests Has unit tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak in chat_context

1 participant