Skip to content

Commit 5a0b5a8

Browse files
committed
Replace setTimeout with a periodic chrome alarm for more reliable WebSocket reconnection in the background service worker
1 parent ba9f3e8 commit 5a0b5a8

3 files changed

Lines changed: 29 additions & 25 deletions

File tree

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import browser from 'webextension-polyfill'
22
import { check_and_recover_connection } from './websocket'
3+
const ALARM_NAME = 'keep-alive'
4+
const ALARM_PERIOD_MINUTES = 0.5 // fires every 30 seconds
5+
36

47
export const setup_keep_alive = () => {
58
if (!browser.browserAction) {
6-
const create_keep_alive_alarm = async () => {
7-
try {
8-
chrome.alarms.create('keep-alive', {
9-
when: Date.now() + 1000 * 30
10-
})
11-
} catch (error) {
12-
console.error('Error creating KeepAlive alarm:', error)
13-
}
9+
const ensure_alarm = () => {
10+
chrome.alarms.get(ALARM_NAME, (alarm) => {
11+
if (!alarm) {
12+
chrome.alarms.create(ALARM_NAME, {
13+
delayInMinutes: ALARM_PERIOD_MINUTES,
14+
periodInMinutes: ALARM_PERIOD_MINUTES
15+
})
16+
}
17+
})
1418
}
19+
chrome.runtime.onStartup.addListener(ensure_alarm)
20+
chrome.runtime.onInstalled.addListener(ensure_alarm)
21+
ensure_alarm()
22+
23+
chrome.alarms.onAlarm.addListener((alarm) => {
24+
if (alarm.name === ALARM_NAME) {
25+
check_and_recover_connection()
26+
}
1527

16-
chrome.runtime.onStartup.addListener(create_keep_alive_alarm)
17-
chrome.runtime.onInstalled.addListener(create_keep_alive_alarm)
18-
chrome.alarms.onAlarm.addListener(() => {
19-
create_keep_alive_alarm()
20-
check_and_recover_connection()
2128
})
2229
}
2330
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { WebSocketMessage } from '@shared/types/websocket-message'
22
import { handle_messages } from './message-handler'
3-
import { CONFIG } from './config'
43
import { DEFAULT_PORT, SECURITY_TOKENS } from '@shared/constants/websocket'
54
import browser from 'webextension-polyfill'
65
let websocket: WebSocket | null = null
@@ -23,12 +22,13 @@ export const check_and_recover_connection = () => {
2322
console.warn(
2423
'WebSocket connection is stale (no recent pings). Force reconnecting...'
2524
)
25+
is_reconnecting = false
2626
websocket.close()
2727
}
28-
} else if (
29-
!is_reconnecting &&
30-
websocket?.readyState !== WebSocket.CONNECTING
31-
) {
28+
}
29+
30+
if (!is_reconnecting && websocket?.readyState !== WebSocket.OPEN &&
31+
websocket?.readyState !== WebSocket.CONNECTING) {
3232
connect_websocket()
3333
}
3434
}
@@ -49,10 +49,8 @@ export const connect_websocket = async (): Promise<void> => {
4949
const is_healthy = await check_server_health()
5050
if (!is_healthy) {
5151
console.debug('Server health check failed, retrying in 5 seconds...')
52-
setTimeout(() => {
53-
is_reconnecting = false
54-
connect_websocket()
55-
}, CONFIG.RECONNECT_DELAY)
52+
// Let the periodic alarm drive the next retry — setTimeout is unreliable in service workers
53+
is_reconnecting = false
5654
return
5755
}
5856

@@ -84,7 +82,7 @@ export const connect_websocket = async (): Promise<void> => {
8482
console.log('Disconnected from VS Code, attempting to reconnect...')
8583
websocket = null
8684
is_reconnecting = false
87-
setTimeout(connect_websocket, CONFIG.RECONNECT_DELAY)
85+
// Reconnect attempt will be driven by the next alarm tick
8886
}
8987

9088
websocket.onerror = () => {
@@ -93,7 +91,6 @@ export const connect_websocket = async (): Promise<void> => {
9391
}
9492
} catch {
9593
is_reconnecting = false
96-
setTimeout(connect_websocket, CONFIG.RECONNECT_DELAY)
9794
}
9895
}
9996

‎apps/browser/src/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "Autofill for Code Web Chat",
55
"short_name": "CWC",
66
"description": "Got tired with copy and paste? Enable prompt autofill!",
7-
"version": "1.114.0",
7+
"version": "1.115.0",
88
"homepage_url": "https://github.com/robertpiosik/CodeWebChat",
99
"icons": {
1010
"16": "icons/icon-16.png",

0 commit comments

Comments
 (0)