fix(desktop): guard reconnect sockets and keep branch search precise

Avoid stale WebSocket events from an old reconnect attempt flipping the gateway state after a newer socket opens. Also limit session-search dedupe to compression edges so branch-specific hits still open the branch instead of collapsing to the parent.
This commit is contained in:
Brooklyn Nicholson
2026-06-03 13:13:21 -05:00
parent 93228d5299
commit 1b89715e15
3 changed files with 83 additions and 21 deletions
+19 -8
View File
@@ -103,10 +103,19 @@ export class JsonRpcGatewayClient {
this.socket = socket
socket.addEventListener('message', message => {
if (this.socket !== socket) {
return
}
this.handleMessage(message.data)
})
socket.addEventListener('close', () => {
if (this.socket !== socket) {
return
}
this.socket = null
this.setState('closed')
this.rejectAllPending(new Error(this.options.closedErrorMessage))
})
@@ -125,7 +134,7 @@ export class JsonRpcGatewayClient {
}
const onOpen = () => {
if (settled) {
if (settled || this.socket !== socket) {
return
}
@@ -136,7 +145,7 @@ export class JsonRpcGatewayClient {
}
const onError = () => {
if (settled) {
if (settled || this.socket !== socket) {
return
}
@@ -159,13 +168,15 @@ export class JsonRpcGatewayClient {
cleanup()
// Drop the half-open socket so the next connect() starts clean
// instead of short-circuiting on a zombie 'connecting' state.
try {
this.socket?.close()
} catch {
// ignore
}
if (this.socket === socket) {
try {
socket.close()
} catch {
// ignore
}
this.socket = null
this.socket = null
}
this.setState('error')
reject(new Error(this.options.connectErrorMessage))
}, this.options.connectTimeoutMs)