What this thread does
The problem
An extension (often agent-written, often ported from Manifest V2) works right after you reload it, then later the popup shows empty or stale data, a polling setInterval never fires, or the background script throws that localStorage is not defined.
What Chrome's docs say
On the service worker lifecycle page (read 18 October 2026):
- The worker is terminated after about 30 seconds of inactivity, when one event or API call runs past 5 minutes, or when a
fetch()takes over 30 seconds to answer. Receiving an event or calling an extension API resets the idle timer. WebSocket message traffic (Chrome 116), active debugger sessions (118), native messaging connections (105) and long-lived messaging (114, where sending a message keeps it alive but merely opening a port does not) extend its lifetime. - Global variables are lost when it shuts down; save state to storage.
- Use
chrome.storage.sessionorlocalfor JSON state, IndexedDB for structured data and blobs.localStorageis not available in extension service workers. - Use alarms for scheduled work; timers die with the worker. Alarms can have a minimum period of 30 seconds since Chrome 120.
- Events wake a stopped worker, so register your listeners when the script starts, not inside a later callback.
How to ask your agent
Reviewbackground.jsfor state kept in module variables,setTimeoutorsetInterval, andlocalStorage. Move state intochrome.storage.session(orlocalif it must survive restart), replace timers withchrome.alarms, and register all listeners at the top level. Then add a test that stops the service worker and checks the popup still shows the data.
Pitfalls
- Reading a variable right after the worker restarts gives
undefined, which looks like a logic bug. - Keeping the worker alive with a ping loop treats the symptom; the docs list what legitimately extends its life.
- Values in
localstorage are exposed to content scripts by default, and access can be restricted withsetAccessLevel(); keep secrets out. See Chrome's storage API docs.
Limits
From Chrome's documentation, not from a reproduced bug. Version numbers are as the docs state them.
Sources
Primary source: Chrome's extension service worker lifecycle page, read on 18 October 2026.
- Confirms: the 30-second idle shutdown, the 5-minute limit for one event or call, the 30-second
fetch()limit, events and extension API calls resetting the timer, lost globals,chrome.storageand IndexedDB for state, nolocalStorage, and the 30-second minimum alarm period since Chrome 120. - Corrected above: the keep-alive versions differ by feature. Native messaging is Chrome 105, long-lived messaging 114 (sending a message keeps the worker alive, merely opening a port does not), WebSocket message traffic 116 and debugger sessions 118. This page does not discuss registering listeners at the top level, so that advice is not confirmed here.
Started from a post by @Chrome for Developers, packaged by the editor. Original post