fix(extension): suppress ERR_CONNECTION_REFUSED and harden WebSocket lifecycle#97
Open
huangsen365 wants to merge 1 commit intojackwener:mainfrom
Open
Conversation
…ocket lifecycle Problems: - When the opencli daemon is not running, the extension's WebSocket reconnect loop produces noisy ERR_CONNECTION_REFUSED console errors that cannot be caught or suppressed via try/catch (Chrome limitation). - WebSocket constructor could throw during Service Worker teardown. - Race conditions between connect() calls from multiple alarm/event triggers could create duplicate sockets. - ws.close() in onerror handler could throw if SW context is gone. Fixes: - Probe daemon reachability via fetch(HEAD) before opening WebSocket. A failed fetch is silent; only connect WebSocket when daemon is up. - Add 'connecting' guard flag to prevent concurrent connect() calls. - Check WebSocket CLOSING state to avoid creating new socket while old one is still shutting down. - Capture socket in local variable to avoid stale reference races. - Wrap socket.close() in try/catch in onerror handler. - Only send responses if the socket is still the active one and OPEN.
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
When the opencli daemon is not running, the Chrome extension's WebSocket reconnect loop produces noisy
ERR_CONNECTION_REFUSEDconsole errors every few seconds:These errors:
Additionally, several race conditions existed in the WebSocket lifecycle.
Solution
Probe before connect: Use
fetch(HEAD)to check if the daemon is reachable before opening a WebSocket. A failed fetch is silent — no console noise. Only proceed with WebSocket when daemon is confirmed up.Harden lifecycle:
connectingguard flag to prevent concurrentconnect()calls from overlapping alarm/event triggersWebSocket.CLOSINGstate to avoid creating a new socket while the old one is still shutting downonclose/onerrorcallbackssocket.close()in try/catch in onerror (can throw during SW teardown)Testing