From 5952c6619f8d256bac30080c345f8dfa09122b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Sun, 5 Jul 2026 23:55:01 +0200 Subject: [PATCH] fixed a race condition in long-poll initialization by adding a token validation guard to prevent stale chat sessions from hijacking the current state during navigation or app resumes. --- lib/state/app/modules/chat/bloc/chat_bloc.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/state/app/modules/chat/bloc/chat_bloc.dart b/lib/state/app/modules/chat/bloc/chat_bloc.dart index 8ccfb9e..7785ec4 100644 --- a/lib/state/app/modules/chat/bloc/chat_bloc.dart +++ b/lib/state/app/modules/chat/bloc/chat_bloc.dart @@ -196,6 +196,13 @@ class ChatBloc void _startLongPoll(String token) { if (!_appResumed) return; + // A load chain may finish AFTER the user already switched chats — e.g. a + // notification tap resumes the app (lifecycle refresh starts loading the + // still-open chat A) and then navigates to chat B. Without this guard the + // stale chain's completion would hijack the long-poll back to A and its + // responses would merge A's messages into B's state (chat B showing chat + // A's content) while B never receives live updates. + if ((innerState?.currentToken ?? '') != token) return; if (_pollingToken == token) return; _stopLongPoll(); _pollingToken = token;