Skip to content

Commit 83f8163

Browse files
committed
fix(kilo-app): fix connect/disconnect race and clear connectError on retry
1 parent 13b8576 commit 83f8163

File tree

1 file changed

+10
-1
lines changed
  • kilo-app/src/components/kiloclaw

1 file changed

+10
-1
lines changed

kilo-app/src/components/kiloclaw/chat.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,23 @@ function StreamChatUI({
155155
const chatClient = StreamChat.getInstance(apiKey);
156156

157157
let cancelled = false;
158+
setConnectError(null);
158159

159160
const connect = async () => {
160161
try {
162+
// Await disconnect to prevent tokenManager.reset() from racing with the new connection
163+
if (chatClient.userID) {
164+
await chatClient.disconnectUser();
165+
}
166+
if (cancelled) {
167+
return;
168+
}
161169
await chatClient.connectUser({ id: userId }, tokenProvider);
162170
const ch = chatClient.channel('messaging', channelId);
163171
await ch.watch({ presence: true });
172+
// cancelled may change across awaits above
164173
if (!cancelled) {
174+
// eslint-disable-line typescript-eslint/no-unnecessary-condition
165175
setClient(chatClient);
166176
setChannel(ch);
167177
}
@@ -176,7 +186,6 @@ function StreamChatUI({
176186

177187
return () => {
178188
cancelled = true;
179-
void chatClient.disconnectUser();
180189
setClient(null);
181190
setChannel(null);
182191
};

0 commit comments

Comments
 (0)