A restored WebSocket session resumes its thread again on every reconnect
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.
- Start a chat using the second profile and send a message.
- Reload the thread page to resume that chat.
- Send another message and inspect
cl.chat_context.to_openai(). - 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_successfulreturned early for every restored session (if context.session.restored: return), matching #159 (“persist user sessions on socket reconnection”) and the documentedsession_timeoutbehavior. #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 madeon_chat_startidempotent 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_resumeremains set, soconnection_successfulcallsresume_threadon every reconnect. That replacesuser_sessions[session.id]with persisted thread metadata and appends the persisted message steps tocl.chat_context.ChatContext.addcomparesMessageobjects by identity, whileMessage.from_dictcreates new objects, so the messages are not deduplicated. - Other trigger: Resuming a thread whose metadata
chat_profilediffers from the profile used when the page started. Theresume_threadhandler setsChatProfile,connectdepends on it, andApp.tsxreconnects with the samesessionId. - Related issue: #2228 (
on_chat_endtriggered 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:
- 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
returnSource: Chainlit/chainlit