Skip to content

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

Open
axelray-dev wants to merge 3 commits 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 3 commits 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. Stops unbounded memory growth and OOM on long-running servers.

  • Bug Fixes
    • Added ChatContext.delete_session(session_id) and call it in socket clear() after user_sessions cleanup.
    • Added tests for removal, unknown IDs, and no active context.

Written for commit 5485c3f. Summary will update on new commits.

Review in cubic

Co-Authored-By: Codex <codex@openai.com>
@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

@dokterbob

Copy link
Copy Markdown
Collaborator

@codex review

@dokterbob

Copy link
Copy Markdown
Collaborator

@axelray-dev This looks good to me but I would love it if at least one other user/dev could validate that this doesn't have unintended side-effects. Unfortunately, I lack the time to do this investigation myself.

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

2 participants