Skip to content

[Bug]: serviceworker event never emitted when a Manifest V3 extension's service worker stops and restarts #39475

Closed

Description

@shairoth12

Version

1.58.0

Steps to reproduce

Reproduction repository: https://github.com/shairoth12/playwright-sw-repro

  1. Clone the repo and follow the README to install and run
  2. The test launches a persistent context with a simple MV3 extension
  3. It grabs the initial service worker and records a startTime global from the SW's scope
  4. It forces a stop → start cycle via the ServiceWorker CDP domain
  5. It waits for a new serviceworker event (registered before the stop/start)
  6. It evaluates startTime in the new worker to confirm a fresh context

Expected behavior

context.waitForEvent('serviceworker') resolves with a new Worker object after the stop/start cycle, and evaluating in it returns a startTime greater than the original.

Actual behavior

waitForEvent('serviceworker') times out — the event is never emitted. The original Worker reference is still in context.serviceWorkers() but its execution context is stale; any evaluate() call throws.

Additional context

Chrome reuses the same CDP target ID when a Manifest V3 service worker restarts, so no Target.attachedToTarget event is fired and Playwright never learns a new worker exists. This is reproducible with an explicit CDP stop/start cycle as well as with chrome.runtime.reload().

This is a reopening of a previous issue: #27015

A fix is introduced in #39476

Environment

- OS: macOS 15.3.1, Apple M1 Max
- Browser: Chromium (bundled with Playwright)
- Language: Node.js

Activity

  1. dgozman commented on Mar 2, 2026

    @dgozman
    Collaborator

    Shai Roth (@shairoth12) Thank you for the issue and the PR!

    What's the actual usecase for this? I don't think we expect anyone to restart a service worker through custom CDP commands. I'd assume you have some other usecase that does not involve it?

    About the fix: why would you prefer waitForEvent('serviceworker') to trigger, and resolve with a new Worker instance? I think it would be easier to preserve the Worker object and just make sure evaluate() is stalling while the service worker is being reloaded. WDYT?

  2. shairoth12 commented on Mar 2, 2026

    @shairoth12
    ContributorAuthor

    Dmitry Gozman (@dgozman) Thank you for the quick response 🙂

    The actual use case is MV3's natural lifecycle - Chrome suspends the background service worker after ~30s of inactivity and restarts it when an event comes in (navigation, message, alarm, etc.). After a restart, the old worker handle is stale and no new 'serviceworker' event arrives, so there's no way to get a live one. This was reported a couple of times before (#26958, #27015).

    The CDP commands in the test are just to reproduce it fast - there's no Chromium flag to shorten the 30s timeout. I used ServiceWorker.stopWorker specifically (not Target.closeTarget) because it keeps the same target alive, which is what Chrome does during natural idle suspension. Target.closeTarget closes the target entirely and Chrome issues a new Target.attachedToTarget on restart, which Playwright already handles fine.

    On the fix - you're right, stalling evaluate() and keeping the same Worker object makes much more sense. Should I go ahead and update the PR to make it work that way?

  3. dgozman commented on Mar 3, 2026

    @dgozman
    Collaborator

    Shai Roth (@shairoth12) Thank you for the explanation, that makes sense.

    Should I go ahead and update the PR to make it work that way?

    Yep, that would be great.

  4. shairoth12 commented on Mar 5, 2026

    @shairoth12
    ContributorAuthor

    Dmitry Gozman (@dgozman) I've updated the PR with same-Worker / stalling approach (also updated the PR description).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions