Skip to content

Commit 0c51321

Browse files
fix: cancel streaming generation before clearing chat
Prevent race condition where _messages.clear() invalidates the assistantIndex while the generation stream is still yielding tokens. This could cause RangeError if user taps "Clear Chat" during streaming. Now _clearChat() cancels the subscription and stops generation before clearing the message list.
1 parent e4202ec commit 0c51321

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

lib/screens/chat_screen.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class _ChatScreenState extends State<ChatScreen> {
145145
}
146146

147147
void _clearChat() {
148+
_generationSubscription?.cancel();
149+
_generationSubscription = null;
150+
gemmaService.stopGeneration();
148151
setState(() {
149152
_messages.clear();
150153
});

0 commit comments

Comments
 (0)