Skip to content

fix: webview - retry service worker registration and reload webview - #333833

Open
dnch13 wants to merge 2 commits into
microsoft:mainfrom
dnch13:webview-service-worker-retry
Open

fix: webview - retry service worker registration and reload webview#333833
dnch13 wants to merge 2 commits into
microsoft:mainfrom
dnch13:webview-service-worker-retry

Conversation

@dnch13

@dnch13 dnch13 commented Sep 1, 2026

Copy link
Copy Markdown

Bugfix is related to SUPER hot issue - 125993 (Webview crash)

Service worker registration in webviews can transiently fail, e.g. with "InvalidStateError: The document is in an invalid state" when the document is not fully active during registration, which previously surfaced a fatal "Error loading webview" notification that could only be resolved by reloading the whole window.

  • pre/index.html: retry registration with backoff (1s/1s/2s/3s/5s) and clean up possibly corrupted service worker state (caches / registrations on desktop) between attempts. Fail fast on permanent conditions (user denied permission, InvalidStateError) since retrying cannot recover the current document.

  • webviewElement: on fatal service worker registration errors, reload the webview into a fresh document (up to 5 times with backoff) before surfacing the error to the user, and offer a Reload Webview action with the final error.

…nsient failures

Service worker registration in webviews can transiently fail, e.g. with "InvalidStateError: The document is in an invalid state" when the document is not fully active during registration, which previously surfaced a fatal "Error loading webview" notification that could only be resolved by reloading the whole window.

- pre/index.html: retry registration with backoff (1s/1s/2s/3s/5s) and clean up possibly corrupted service worker state (caches / registrations on desktop) between attempts. Fail fast on permanent conditions (user denied permission, InvalidStateError) since retrying cannot recover the current document.

- webviewElement: on fatal service worker registration errors, reload the webview into a fresh document (up to 5 times with backoff) before surfacing the error to the user, and offer a Reload Webview action with the final error.
Copilot AI balanced review requested due to automatic review settings September 1, 2026 18:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds layered recovery for transient webview service-worker registration failures.

Changes:

  • Retries registration with backoff and state cleanup.
  • Reloads failed webviews before reporting fatal errors.
  • Adds a final “Reload Webview” notification action.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
webviewElement.ts Adds host-side reload retries and recovery notification.
pre/index.html Adds registration retries and cleanup logic.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/vs/workbench/contrib/webview/browser/pre/index.html
Comment thread src/vs/workbench/contrib/webview/browser/webviewElement.ts
Comment thread src/vs/workbench/contrib/webview/browser/webviewElement.ts
Comment thread src/vs/workbench/contrib/webview/browser/webviewElement.ts
@dnch13

dnch13 commented Sep 2, 2026

Copy link
Copy Markdown
Author
@microsoft-github-policy-service agree

- Clear webview caches only on desktop, where each webview has an
  isolated origin. In the browser, webviews share an origin, service
  worker, and cache storage, so clearing them would evict resources for
  every other open webview. Update the inline script CSP hash.
- Ignore duplicate registration failure reports while a reload is
  already scheduled, so repeated content events from a failed document
  cannot exhaust the retry budget before any reload happens.
- Track mount-time drag listeners in a per-mount disposable store that
  is cleared on each mount, so repeated remounts no longer accumulate
  duplicate listeners.
- Guard the Reload Webview notification action against disposed or
  detached webviews.
@dnch13

dnch13 commented Sep 2, 2026

Copy link
Copy Markdown
Author

paul-cheung0607 ayo, since this fix is solving really a ridiculous problem, bet would be better if getting steamlined ASAP (just stating that process is not as important as people's time).

Thank you.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants