Skip to content

Commit 67763de

Browse files
committed
fix: prevent send guard from being cleared mid-flight during thread switch
Use a generation counter to ensure the finally block in handleSend only clears sendInFlightRef when the generation hasn't changed. When the user switches threads, the generation is incremented, so a stale finally block from a previous thread's send won't clear the guard for the new thread's in-flight send.
1 parent e4ec3cf commit 67763de

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

apps/mobile/src/features/threads/ThreadComposer.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
210210
const [isFocused, setIsFocused] = useState(false);
211211
const wasExpandedBeforePreviewRef = useRef(false);
212212
const sendInFlightRef = useRef(false);
213+
const sendGenerationRef = useRef(0);
213214
const { onExpandedChange } = props;
214215

215216
const [previewImageUri, setPreviewImageUri] = useState<string | null>(null);
@@ -218,6 +219,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
218219
const canSend = hasContent;
219220

220221
useEffect(() => {
222+
sendGenerationRef.current++;
221223
sendInFlightRef.current = false;
222224
}, [props.selectedThread.id]);
223225

@@ -456,10 +458,13 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
456458
const handleSend = useCallback(async () => {
457459
if (!canSend || sendInFlightRef.current) return;
458460
sendInFlightRef.current = true;
461+
const generation = sendGenerationRef.current;
459462
try {
460463
await onSendMessage();
461464
} finally {
462-
sendInFlightRef.current = false;
465+
if (sendGenerationRef.current === generation) {
466+
sendInFlightRef.current = false;
467+
}
463468
}
464469
}, [canSend, onSendMessage]);
465470
const handleCommandSelect = useCallback(

0 commit comments

Comments
 (0)