fix: WebSocket reconnect stability — eliminate kick storms and race conditions#140
Merged
yujiawei merged 5 commits intodmwork-org:mainfrom Mar 31, 2026
Merged
Conversation
…d-disconnect detection - Bug A: Defer reconnectAttempts reset until connection stable 30s+ (prevents backoff from being defeated by short-lived connections) - Bug B: Reconnect with current credentials during token refresh cooldown (5-10s random delay to avoid tight loop on persistent kicks) - Bug C: Detect 3+ consecutive rapid disconnects (<5s each) and trigger token refresh via onError to break stale-token reconnect loops - Add clearStableTimer() to all disconnect/reconnect paths - Reset connection tracking state in disconnect() - Add bug reproduction/verification tests (5 new tests) Closes dmwork-org#139
When a bot is repeatedly kicked during cooldown, each kick created a new setTimeout for reconnect without clearing the previous one. Multiple timers firing simultaneously caused the same bot to open parallel WS connections, which WuKongIM treats as duplicate sessions for the same UID — each new connection kicks the previous one, creating a self-kick feedback loop (observed: 336 kicks/min across 15 bots). Fix: track cooldownReconnectTimer per account, clearTimeout before creating a new one, and clear on cleanup. Closes dmwork-org#139
…nup, serialized reconnect
yujiawei
approved these changes
Mar 31, 2026
Collaborator
yujiawei
left a comment
There was a problem hiding this comment.
LGTM. 9 个 WS 重连竞态修复覆盖完整,stable timer + rapid disconnect 检测设计合理,20 新测试 + 生产验证 (950 kicked/h → 0). CI ✅
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Multiple WebSocket reconnect bugs caused persistent kick storms (~950 kicked/hour across 15 bots) when connecting to WuKongIM. The root cause was a cascade of race conditions between the transport layer (socket.ts) and the application layer (channel.ts).
Fixes #135, #139
Root Cause Analysis
9 confirmed issues found via Codex + Claude Code cross-review:
Critical (direct cause of kick loops)
open/message/errorhandlers lackedif (this.ws !== ws) returnguard, allowing old WS DISCONNECT packets to corrupt new connection statews.close(), leaving TCP connections aliveonDisconnected+scheduleReconnect()but notonError, so channel layer didn't know to stop heartbeat timerRace condition fixes
ws.close()is async; new WS opened before old TCP FIN completed, causing server to see 2 connections brieflyregisterBot(forceRefresh)called while old WS still alive during awaitdisconnect()+connect()bypassed exponential backoffconsecutiveHeartbeatFailurespreserved across reconnectsChanges
socket.ts
if (this.ws !== ws) return) toopen,message,errorhandlersws.close(); this.ws = nullin CONNACK rejection and DISCONNECT handlersdisconnectAndWait(timeoutMs=2000)— waits for WS close event or terminates on timeoutstopReconnectTimer()public for cross-layer coordinationchannel.ts
onDisconnected; restart inonConnecteddisconnect()+connect()withawait disconnectAndWait()+connect()registerBot(forceRefresh)consecutiveHeartbeatFailuresinonConnectedstopReconnectTimer()beforeconnect()in cooldown/heartbeat paths[accountId]to all log messages for per-bot diagnosticsTests
reconnect-fixes.test.tsVerification
Deployed to 3 nodes with 30+ bots total: