Skip to content

fix(web): stabilize WebSocket connect to prevent reconnection storms#1419

Open
howardpen9 wants to merge 1 commit intoMoonshotAI:mainfrom
howardpen9:fix/web-reconnection-storm-refs
Open

fix(web): stabilize WebSocket connect to prevent reconnection storms#1419
howardpen9 wants to merge 1 commit intoMoonshotAI:mainfrom
howardpen9:fix/web-reconnection-storm-refs

Conversation

@howardpen9
Copy link
Contributor

@howardpen9 howardpen9 commented Mar 12, 2026

Summary

Fixes #1409 — web mode keeps refreshing with rapid WebSocket connect/close cycles (visible as incrementing client ports in terminal logs).

Root cause: The connect callback captured multiple frequently-changing callbacks (handleMessage, onError, sendInitialize, etc.) as direct dependencies. When any of these changed identity — often triggered by deep dependency chains through processEventconnect was recreated, which cascaded to reconnectsendMessage → queue auto-send effects, triggering new connections that closed the previous one.

Fix: Extends the ref-based stabilization pattern from #1386 to all callbacks used inside connect(). By reading from refs (handleMessageRef, onErrorRef, sendInitializeRef, etc.) instead of capturing callbacks directly, connect() only depends on sessionId and stable state setters — breaking the identity cascade entirely.

Changes

  • Added refs for handleMessage, onError, sendInitialize, sendPendingMessage, getWebSocketUrl, completeStreamingMessages
  • Updated connect, disconnect, cancel, reconnect, and sendMessage to use refs instead of direct callback references
  • Reduced dependency arrays to only stable values (sessionId, setMessages, setAwaitingFirstResponse)

Test plan

  • Run kimi web, open browser — verify no rapid WebSocket reconnection cycle in terminal logs
  • Switch between sessions — verify clean disconnect/reconnect
  • Send a message — verify it's delivered and response streams correctly
  • Queue a message while AI is processing — verify it auto-sends when ready
  • Cancel a running stream — verify clean cancellation
  • Verify watchdog reconnect still works after 45s of no messages during streaming

Open with Devin

…n storms

The `connect` callback had many unstable dependencies (handleMessage,
onError, sendInitialize, etc.) that frequently changed identity due to
their own deep dependency chains. When connect changed, it cascaded to
reconnect → sendMessage → queue auto-send effects, potentially causing
rapid connect/close/reconnect cycles.

This extends the ref-based pattern from MoonshotAI#1386 to all callbacks used
inside connect(). By reading from refs (handleMessageRef, onErrorRef,
etc.) instead of capturing callbacks directly, connect() only depends on
sessionId and stable state setters, breaking the cascade.

Closes MoonshotAI#1409
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kimi cli web mode keeps refreshing and connects different port

1 participant