Skip to content

Commit 069e772

Browse files
committed
fix(ws-hook): guard against stale socket callbacks after cleanup/reconnect
- Add 'if (ws !== wsRef.current) return' guard to onopen, onmessage, onerror, onclose - Prevents old socket events from mutating state after a new connection is created - cleanup() clears wsRef.current so guard works correctly
1 parent a53f526 commit 069e772

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

frontend/src/hooks/useRepoIndexingWebSocket.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,31 @@ export function useRepoIndexingWebSocket(
166166
wsRef.current = ws
167167

168168
ws.onopen = () => {
169+
// Guard against stale socket callbacks after cleanup/reconnect
170+
if (ws !== wsRef.current) return
169171
console.log('[WS] Connected')
170172
reconnectAttempts.current = 0
171173
setConnectionState('connected')
172174
}
173175

174-
ws.onmessage = handleMessage
176+
ws.onmessage = (event) => {
177+
// Guard against stale socket callbacks
178+
if (ws !== wsRef.current) return
179+
handleMessage(event)
180+
}
175181

176182
ws.onerror = () => {
183+
// Guard against stale socket callbacks
184+
if (ws !== wsRef.current) return
177185
setConnectionState('error')
178186
setPhase('error')
179187
setError('WebSocket connection error')
180188
}
181189

182190
ws.onclose = (event) => {
191+
// Guard against stale socket callbacks after cleanup/reconnect
192+
if (ws !== wsRef.current) return
193+
183194
console.log('[WS] Closed:', event.code, event.reason)
184195

185196
// Normal closure or auth failure - don't reconnect

0 commit comments

Comments
 (0)