fix(chat): prevent duplicate first message send and UI flicker#185
Open
Saul-Gomez-J wants to merge 1 commit intolevante-hub:developfrom
Open
fix(chat): prevent duplicate first message send and UI flicker#185Saul-Gomez-J wants to merge 1 commit intolevante-hub:developfrom
Saul-Gomez-J wants to merge 1 commit intolevante-hub:developfrom
Conversation
Fixes race condition that caused the pending first message to be sent multiple times when the component re-rendered during processing. ## Issues Fixed 1. **Duplicate Message Sending**: The useEffect processing pendingFirstMessage would execute multiple times if re-renders occurred, causing: - Same message sent 2+ times to AI API - Unnecessary token/credit consumption - Duplicate messages in database - Multiple assistant responses 2. **UI Flicker**: Clearing pendingFirstMessage immediately caused the chat to show empty state (BreathingLogo) briefly during message processing, creating poor visual feedback ## Solution ### Processing Guard (pendingMessageProcessingRef) - New ref prevents concurrent executions of message sending logic - Checked before processing, set during, cleared in finally block - Ensures one-time execution per pending message ### Deferred State Clearing - pendingFirstMessage now cleared AFTER sendMessageAI completes - Also cleared on error to allow retry with restored input - Prevents premature transition to empty state ### Updated isChatEmpty Logic ```typescript // Before const isChatEmpty = messages.length === 0 && status !== 'streaming'; // After const isChatEmpty = messages.length === 0 && status !== 'streaming' && pendingFirstMessage === null; ``` Now considers pending message when determining empty state ## Testing Manually verified: - First message sends exactly once - No UI flicker during send - Error handling restores input correctly - Re-renders don't trigger duplicate sends Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical race condition in ChatPage that caused the first message of a new chat session to be sent multiple times, resulting in:
Root Cause
The
useEffectthat processespendingFirstMessagewould execute multiple times if the component re-rendered during async message sending (e.g., from state updates likesetIsStreaming,currentSessionchanges, or Zustand updates).Since
pendingFirstMessagewas cleared immediately at the start, subsequent re-renders would find it null - but if execution was already in progress, the async operation would continue, leading to duplicate sends.Changes
1. Processing Guard with Ref (
pendingMessageProcessingRef)truewhen processing startsfinallyblock to ensure cleanup even on errorsCode:
2. Deferred State Clearing
pendingFirstMessageandpendingFirstAttachmentsnow cleared AFTERsendMessageAIcompletes successfullyBefore:
After:
3. Updated
isChatEmptyLogicNow considers
pendingFirstMessagewhen determining if chat is empty:This prevents showing the BreathingLogo/empty state while the first message is being processed.
Impact
Before this fix:
After this fix:
Testing
Manually tested scenarios:
Files Changed
+21, -4)pendingMessageProcessingReffor execution guardisChatEmptyto consider pending message🤖 Generated with Claude Code