Skip to content

Commit 8c5c5c9

Browse files
committed
Add connection timeout and socket abandonment to prevent hung WebSocket handshakes
├── apps │ ├── browser │ │ ├── README.md │ │ └── src │ │ ├── background │ │ │ ├── keep-alive.ts │ │ │ ├── main.ts │ │ │ ├── message-handler.ts │ │ │ └── websocket.ts │ │ └── manifest.json │ └── editor │ ├── src │ │ ├── extension.ts │ │ ├── i18n │ │ │ └── translations │ │ │ └── services │ │ │ └── websocket-manager.ts │ │ ├── services │ │ │ ├── websocket-manager.ts │ │ │ └── websocket-server-process.ts │ │ ├── utils │ │ │ └── shared-token.ts │ │ └── views │ │ └── prompt │ │ └── backend │ │ └── prompt-view-provider.ts │ └── webpack.config.ts ├── docs │ └── privacy.mdx └── packages └── shared └── src ├── constants │ └── websocket.ts └── types └── websocket-message.ts
1 parent dcb654c commit 8c5c5c9

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

‎apps/browser/src/background/websocket.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ports_to_check = (): number[] => {
1616
}
1717

1818
const STALE_AFTER_MS = 20000
19+
const CONNECT_TIMEOUT_MS = 500 // a handshake that hangs (e.g. dead forwarded port) must not block the port
1920
const RETRY_AFTER_DROP_MS = 1000 // we had a live connection, retry quickly
2021
const RETRY_IDLE_MS = 5000 // nothing was ever listening, poll slowly
2122

@@ -51,18 +52,28 @@ const is_busy = (c: Connection) =>
5152
c.ws?.readyState === WebSocket.OPEN ||
5253
c.ws?.readyState === WebSocket.CONNECTING
5354

55+
const abandon_socket = (c: Connection) => {
56+
const ws = c.ws
57+
c.ws = null
58+
if (!ws) return
59+
ws.onopen = null
60+
ws.onmessage = null
61+
ws.onclose = null
62+
try {
63+
ws.close()
64+
} catch {}
65+
}
66+
5467
export const check_and_recover_connection = () => {
5568
for (const port of ports_to_check()) {
5669
const c = get_connection(port)
5770

5871
if (c.ws?.readyState === WebSocket.OPEN) {
59-
if (Date.now() - c.last_ping > STALE_AFTER_MS) {
60-
console.warn(
61-
`WebSocket on port ${port} is stale (no recent pings). Force reconnecting...`
62-
)
63-
c.ws.close()
64-
}
65-
continue
72+
if (Date.now() - c.last_ping <= STALE_AFTER_MS) continue
73+
console.warn(
74+
`WebSocket on port ${port} is stale (no recent pings). Force reconnecting...`
75+
)
76+
abandon_socket(c)
6677
}
6778

6879
if (!is_busy(c) && !c.retry_timer) {
@@ -111,7 +122,15 @@ export const connect_websocket = async (port: number): Promise<void> => {
111122
c.last_ping = Date.now()
112123
let was_opened = false
113124

125+
const connect_timer = setTimeout(() => {
126+
if (c.ws === ws && ws.readyState === WebSocket.CONNECTING) {
127+
abandon_socket(c)
128+
schedule_retry(port, RETRY_IDLE_MS)
129+
}
130+
}, CONNECT_TIMEOUT_MS)
131+
114132
ws.onopen = () => {
133+
clearTimeout(connect_timer)
115134
if (c.ws !== ws) return
116135
was_opened = true
117136
c.last_ping = Date.now()
@@ -136,6 +155,7 @@ export const connect_websocket = async (port: number): Promise<void> => {
136155
}
137156

138157
ws.onclose = () => {
158+
clearTimeout(connect_timer)
139159
if (c.ws !== ws) return
140160
c.ws = null
141161
if (was_opened) {

0 commit comments

Comments
 (0)