#3061·chainlit

A restored WebSocket session resumes its thread again on every reconnect

Author: ilkersigirciCreated Sep 27, 2026Updated Sep 27, 2026
Labelsneeds-triage

Describe the bug

When the browser reconnects with its existing sessionId, Chainlit restores the live WebsocketSession. The connection then resumes the same thread again, reruns @cl.on_chat_resume, and appends the persisted messages to cl.chat_context a second time. cl.chat_context.to_openai() consequently returns duplicate conversation history. Each further reconnect adds another copy.

To Reproduce

Steps to reproduce the behavior:

Use Chainlit 2.12.0 with the PostgreSQL data layer, two chat profiles, and an on_chat_resume hook.

  1. Start a chat using the second profile and send a message.
  2. Reload the thread page to resume that chat.
  3. Send another message and inspect cl.chat_context.to_openai().
  4. Observe the earlier turn duplicated in the conversation. The resume hook has run twice for the same session ID; on its second run, session.restored == True.

The issue also reproduces after an ordinary reconnect: while viewing a resumed thread, take the browser offline for about 60 seconds, then reconnect and send a message. The history is duplicated again.

Expected behavior

A restored session should keep its existing chat or resumed-thread state. It should not rerun on_chat_resume, reset user_session, or append the thread to the chat context again.

Desktop (please complete the following information):

  • OS: Ubuntu
  • Browser: Firefox & Chromium
  • Version 26.04

Additional context

  • Chainlit version: 2.12.0.
  • Regression: This started in 2.8.2. In 2.8.1, connection_successful returned early for every restored session (if context.session.restored: return), matching #159 (“persist user sessions on socket reconnection”) and the documented session_timeout behavior. #2549 narrowed that return to sessions without a first interaction to fix #2535. A restored session that has interacted now falls through to the resume branch. #2891 later made on_chat_start idempotent across reconnects, but the resume branch has no equivalent guard.
  • Technical context: The early return currently only covers restored sessions without a first interaction. For a resumed session, thread_id_to_resume remains set, so connection_successful calls resume_thread on every reconnect. That replaces user_sessions[session.id] with persisted thread metadata and appends the persisted message steps to cl.chat_context. ChatContext.add compares Message objects by identity, while Message.from_dict creates new objects, so the messages are not deduplicated.
  • Other trigger: Resuming a thread whose metadata chat_profile differs from the profile used when the page started. The resume_thread handler sets ChatProfile, connect depends on it, and App.tsx reconnects with the same sessionId.
  • Related issue: #2228 (on_chat_end triggered twice when resuming a thread).

One possible fix is to return for every restored session, while only invoking on_chat_start when the session has no first interaction and chat start has not already run:

diff
-    if context.session.restored and not context.session.has_first_interaction:
-        if config.code.on_chat_start and not context.session.chat_started:
+    if context.session.restored:
+        if (
+            not context.session.has_first_interaction
+            and config.code.on_chat_start
+            and not context.session.chat_started
+        ):
             context.session.chat_started = True
             task = asyncio.create_task(config.code.on_chat_start())
             context.session.current_task = task
         return