mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-31 05:50:59 -05:00
[PR #21657] [CLOSED] fix: reset generation state and message queue when switching chats [PoC] #97224
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/21657
Author: @silentoplayz
Created: 2/20/2026
Status: ❌ Closed
Base:
dev← Head:fix/chat-state-isolation📝 Commits (4)
46cd933fix(chat): reset generation state and message queue when switching chatsfba6ecbfix(chat): guard completion state assignments against chat switchingf94c7dffix(chat): save messageQueue to sessionStorage when background chat finishes1c65bd2fix(chat): guard taskIds and messageQueue execution strictly to active chat📊 Changes
1 file changed (+41 additions, -21 deletions)
View changed files
📝
src/lib/components/chat/Chat.svelte(+41 -21)📄 Description
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions to discuss your idea/fix with the community before creating a pull request, and describe your changes before submitting a pull request.
This is to ensure large feature PRs are discussed with the community first, before starting work on it. If the community does not want this feature or it is not relevant for Open WebUI as a project, it can be identified in the discussion before working on the feature and submitting the PR.
Before submitting, make sure you've checked the following:
devbranch. PRs targetingmainwill be immediately closed.devto ensure no unrelated commits (e.g. frommain) are included. Push updates to the existing PR branch instead of closing and reopening.Changelog Entry
Description
$chatId === _chatId), and smoothly persist background Queues intosessionStoragefor seamless task resumption upon revisiting the older chat.Added
messageQueuearray intosessionStoragefor background chats when generations finish, ensuring their messages are preserved and automatically executed upon reloading the chat.Changed
loadChatandinitNewChatmethods to thoroughly explicitly flush state variables (messageQueue,taskIds,generationController,generating).sendMessageSocketto verify that the target_chatIdis identical to$chatIdprior to applying anydone,history, ortaskIdstate updates, protecting the isolated active chat window.chatCompletedHandlerwith$chatId === _chatIdto prevent background generation logic from indiscriminately nullifyingtaskIdsof a separate active chat.Fixed
Fixed
TypeError: can't access property "parentId"when clicking the Stop Generation button after navigating to a new chat.Prevented new messages from getting queued in a newly created chat if the previous chat was still generating.
Background generations bleeding into and altering new chat sessions.
Stop button state incorrectly flagging responses in the wrong active chat.
Active taskIds accidentally deleting and halting queue sequences due to background
chatCompletedHandlerinvocations."Queued" messages silently failing to resolve when Svelte fires the chat completed event for an unfocused component.
This PR resolves the following error(s) that were previously thrown in the browser console (Firefox):
&
Additional Information
Context & Reasoning for PR Changes:
The core issue this PR addresses is State Leakage between active and background chat domains. Open WebUI's frontend architecture essentially relies on a singleton-esque component state for the currently active tab window. Variables like
$chatId,history,taskIds, andmessageQueueare inherently bound to the active user session.When a user triggers a message generation (e.g., in "Chat A") and quickly switches to "Chat B" or initiates a "New Chat", the original
generateOpenAIChatCompletionAPI stream resolves in the background. Because Svelte triggers the corresponding.then()and.catch()blocks in the same component module silently, it was unknowingly dropping the completed variables into Chat B!This led to a series of escalating edge cases, which this PR fixes anatomically:
fix(chat): reset generation state and message queue when switching chatsloadChat) or a new one (initNewChat), Svelte retains the previous variables fortaskIds,messageQueue, andgenerating. This resulted in newly opened chats falsely displaying a "Stop" button and thinking they were still waiting for a generation to finish.fix(chat): guard completion state assignments against chat switchinghistoryandtaskIds. This injected Chat A's message completion metadata into Chat B's history object._chatId) at the start ofsendMessageSocketand wrap the resolution and error blocks insideif (_chatId === $chatId). This ensures that background API completions gracefully abort UI state mutations if you've already navigated away.fix(chat): save messageQueue to sessionStorage when background chat finishesmessageQueue. When the background chat finally completes,chatCompletedHandlernormally clears the active task and submits the queue. However, if the user had navigated to Chat B, Svelte has no way to formulate a headlesssubmitPromptbackground execution payload since the component state (like file attachments and conversation context) has already been swapped out for Chat B. This causes the queue to get stuck or misfire.chatCompletedHandlerso that if$chatId !== _chatId(meaning we're resolving a background chat), Svelte isolates and stashes the queue natively intosessionStorage(specifically underchat-queue-${_chatId}). When the user eventually returns to Chat A, thenavigateHandlerpulls it back out and automatically initiates the queued execution with the correct refreshed scope.fix(chat): guard taskIds and messageQueue execution strictly to active chatchatCompletedHandlerfired for background Chat A, it was executingtaskIds = null;unconditionally. This would accidentally delete the activetaskIdsof Chat B (your current window!), causing any queued sequences in your active tab to drop and fail.taskIdsclearance andmessageQueuedirect execution blocks inside an identity checkif ($chatId === _chatId). This ensures background chats can't break your actively focused work execution.Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.