#15817: fix(chrome-relay): auto-reconnect, MV3 persistence, and keepalive
size: M
Cluster:
Chrome Extension Enhancements
## 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
#17588: fix(relay): survive WS disconnects and MV3 worker restarts
by Unayung · 2026-02-15
90.8%
#10390: fix(chrome-relay): sticky attach + auto-restore after disconnects
by Mokoby · 2026-02-06
89.0%
#19766: fix: Chrome relay extension auto-reattach after SPA navigation
by nishantkabra77 · 2026-02-18
84.4%
#16743: fix: auto-reattach browser relay debugger after navigation
by jg-noncelogic · 2026-02-15
80.5%
#20688: fix(browser): allow extension reconnect when stale websocket linger...
by HOYALIM · 2026-02-19
79.8%
#22385: fix: improve delivery recovery logging with entry age and deferral ...
by derrickburns · 2026-02-21
79.7%
#22571: fix(browser): complete extension relay handshake on connect.challenge
by pandego · 2026-02-21
77.5%
#21314: feat: enhance browser relay with custom naming and diagnostic tools
by kelvinCB · 2026-02-19
76.8%
#23363: Browser: fallback to managed profile and improve runtime diagnostics
by isdoho · 2026-02-22
75.8%
#13942: Fix: chrome-extension: make relay reconnect reliable with one tab
by gabepsilva · 2026-02-11
75.7%