Skip to content

fix(contexts): detach nested service worker sessions so Chrome can stop them - #666

Merged
route merged 1 commit into
mainfrom
fix/service-worker-detach
Oct 21, 2026
Merged

route merged 1 commit into
mainfrom
fix/service-worker-detach

Conversation

@route

@route route commented Oct 21, 2026

Copy link
Copy Markdown
Member

When a page registers a service worker, Chrome auto-attaches it twice: once at the browser level and once nested under the page session, because every page session re-arms Target.setAutoAttach. Ferrum detaches service workers so Chrome can terminate them, but it sent Target.detachFromTarget for the nested session on the browser client. Chrome answered "No session with given id", the error was rescued, and the worker stayed attached and alive for the browser's lifetime.

Fix

  • Detach on the session that owns the attachment (parent), like Puppeteer's #silentDetach and Playwright's CRSession#detach do.
  • Wait for the Runtime.runIfWaitingForDebugger reply before detaching. Otherwise the detach overtakes the resume and the worker stays paused forever, so navigator.serviceWorker.register() hangs.
  • Chrome only replies to the resume once every session that paused the worker has resumed it. Ferrum dispatches events on one thread, so waiting inside the browser-level handler blocks the nested handler and deadlocks. silent_detach runs resume and detach on a short-lived thread instead.

Test

spec/worker_spec.rb: "detaches from them so Chrome can terminate them" registers a service worker and waits until Target.getTargets reports it as not attached. It fails without the fix.

…op them

A page's service worker is auto-attached twice, at the browser level and under the page session. The nested
session was detached on the browser client, Chrome answered "No session with given id", so it stayed attached.

Detach on the parent session, after the resume is acknowledged. Chrome only acknowledges it once every session
that paused the worker resumed it, so this runs on its own thread to not block the other attach event.
@route
route force-pushed the fix/service-worker-detach branch from f494889 to 5bfcba1 Compare October 21, 2026 17:13
@route
route merged commit 2c8535a into main Oct 21, 2026
6 checks passed
@route
route deleted the fix/service-worker-detach branch October 21, 2026 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant