← Back to PRs

#15817: fix(chrome-relay): auto-reconnect, MV3 persistence, and keepalive

by derrickburns open 2026-02-13 22:50 View on GitHub →
size: M
## Problem The Chrome extension relay drops connection after page navigation, sleep/wake cycles, or MV3 service worker restarts — and never recovers. Users must manually re-click the toolbar icon after every navigation. Related: #1160 ## Root Cause Five failure modes identified via code audit of background.js and the relay server: 1. **No reconnection logic** — WebSocket drops are permanent (extension clears state and stops) 2. **MV3 state amnesia** — service worker restarts wipe all in-memory Maps 3. **No keepalive** — Chrome kills idle service worker after ~30s 4. **Navigation detaches debugger** — chrome.debugger auto-detaches on page navigation with no re-attach 5. **No pending request cleanup** — dropped messages leak memory ## Fix Drop-in replacement for background.js + one manifest permission (alarms): - **Auto-reconnect** — Exponential backoff (1s→30s cap, 10 attempts) on WS drop - **State persistence** — chrome.storage.local saves attached tabs, sessions — survives worker restarts - **Keepalive alarm** — chrome.alarms every 24s (under MV3 30s limit) checks WS health - **Navigation re-attach** — On target_closed detach, waits 500ms then re-attaches if tab exists - **Per-tab locks** — Prevents double-attach race from rapid toolbar clicks - **Tab lifecycle cleanup** — onRemoved/onUpdated listeners clean state on close/navigate - **Request timeouts** — 30s timeout on pending requests prevents memory leaks - **Child session cleanup** — Proper detach events for child sessions when parent disconnects ## Testing Tested on macOS (Chrome Profile 11) against Ancestry.com: - ✅ Snapshot through relay - ✅ Navigate to different page + snapshot (previously broke here) - ✅ Extension reload + reconnect ## Changes - assets/chrome-extension/background.js — +280 lines (reconnect, persistence, keepalive, lifecycle) - assets/chrome-extension/manifest.json — added alarms permission No changes to relay server protocol, options page, or CDP command handling. <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR rewrites the Chrome extension service worker (`assets/chrome-extension/background.js`) to make the relay connection resilient: it adds auto-reconnect with backoff, per-tab operation locks, request timeouts for pending relay RPCs, and state persistence via `chrome.storage.local`. It also introduces a keepalive alarm (`chrome.alarms`) to keep the MV3 service worker active, and tab lifecycle handling (onRemoved/onUpdated) plus navigation-triggered re-attach logic for debugger detaches. `assets/chrome-extension/manifest.json` is updated to request the new `alarms` permission required for the keepalive. <h3>Confidence Score: 3/5</h3> - This PR is close to mergeable but has reconnection/persistence logic gaps that can prevent recovery in common scenarios. - Core reconnect/keepalive/persistence changes look coherent, but the keepalive path can fail to schedule reconnect when connection attempts throw early, and restored state currently marks tabs as attached without re-attaching the debugger, which can leave the extension in an inconsistent state after MV3 restarts. - assets/chrome-extension/background.js (keepalive/reconnect failure paths, restoreState/reattach behavior) <sub>Last reviewed commit: 1891255</sub> <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment --> --- Relates to #15099

Most Similar PRs