vscode: Webview Service Worker Error in registration due to invalid document state
Does this issue occur when all extensions are disabled?: No
- VS Code Version:
Version: 1.57.0
Commit: b4c1bd0a9b03c749ea011b06c6d2676c8091a70c
Date: 2021-06-09T17:18:42.895Z
Electron: 12.0.9
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Linux x64 5.11.0-18-generic snap
- OS Version: Ubuntu 21.04
Steps to Reproduce:
- Update to 1.57
- GitGraph extension stops working
The gitgraph extension shows this error:
Error loading webview: Error: Could not register service workers: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state..
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 101
- Comments: 126 (17 by maintainers)
For me, what worked was to delete the folder
Service Worker
under~/.config/Code
and re-opening my Visual Studio and it started working as expected. Hope it helps someone šIām the author of Git Graph, which is one of the extensions that is affected by this (as my extension leverages Visual Studio Code Webviewās for itās core user interface). This Service Worker error occurs within Visual Studio Codeās Webview implementation, so it affects all extensions that use Webviews. It prevents the Webview from loading, for all the affected users.
https://github.com/mhutchie/vscode-git-graph/issues/508 includes an image of a stack trace of this error in the first comment.
This bug first appeared in Visual Studio Code 1.56.0, and only occurred for numerous Linux users (but definitely not all). Reports of this issue lasted about a week or so, so I assumed it had been fixed in one of the 1.56.x patches. However, with the release of Visual Studio Code 1.57.0, Iāve now had reports that this issue is occurring again.
Looping in @mjbvz - Are you able to provide some assistance?
Exiting vscode manually, running
pkill code
and starting vscode again resolved the issue for meFor information: restarting the PC solved the issue for me ATM.
So, it may be related to some sort of cache that is cleared after reboot?
This issue appeared for me twice, every time after a vs code update. The first time updating from 1.55 to 1.56 and now updating from 1.56 to 1.57. I donāt know if the fix on 1.56 happened after a reboot as well.
Duplicate of #122951
(Experimental duplicate detection) Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:
I found that the problem ca be fixed without rebooting by removing all the cache folders used by vscode:
rm -rf ~/.config/Code/*Cache*
I also have this issue on Linux, be good to get a fix soon.
Update: Rebooting fixed the issue, thanks @tritemio
pkill code
works for me. ThanksVersion: 1.62.1 Commit: f4af3cbf5a99787542e2a30fe1fd37cd644cc31f Date: 2021-11-05T10:56:50.217Z Electron: 13.5.2 Chrome: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Linux x64 5.11.0-40-generic snap
Worked for me.Thanks
this is still an issue in 1.70 on ubuntu 20.4
Same here! Thanks!
pkill code
from @lubomir-brindza combined withrm -rf ~/.config/Code/*Cache*
from @tritemio worked for me.Since yesterday, I encountered the same issue for all webviews, tried with: the one in Walkthrough, Quarkus welcome page, Kaoto UI.
Error loading webview: Error: Could not register service workers: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid state..
I am on Fedora 36 VS Code 1.73.1
I tried restating VS Code, restarting OS. Still same behavior. I tried with another fresh instance of VS Code.
Workaround for me (based on previous comments): deleting the
~/.config/Code/Service Worker
then restart VS Codenote it doesnāt work for all the webviews when developing an extension in the
Extension development host
, there is surely another cache to clean somewhere else (not found yet)worked like a charm.
@eduncan911 The problem persists in version 1.60.
Was hit by this issue again, on a completely different machine and setup but was able to resolve without rebooting.
There where stale ācodeā processes running in the background even though I explicitly clicked āexitā from menu. I just killed them from
htop
one by one, and the problem was gone.I suspect this happened due to a previous crash (-es?) - I periodically open my laptop and see no vscode windows open, so I just click the icon again and the window re-appears. Looks like the old processes get orphaned.
This isnāt a linux/snap issue, Iāve seen it plenty of times on Windows.
This worked like a charm for me in Ubuntu, remember to refresh VSCode! thanks a lot!
I just hit this issue with the latest release trying to display the Release Notes. I deleted
~/.config/Code/Service Worker
(+ all directories withCache
in the name) and now itās working again.https://github.com/microsoft/vscode/issues/125993#issuecomment-999715355
It works for me! Iām under Ubuntu 20.04.4 LTS ā¦thanks!
I have anecdote to support this. I accidentally left my computer on last night with VSCode running. I log in to find the icon showing there are no windows open. This has happened to a different computer along with the webview error. So I left the windows open just in case. Opening a new window causes the webview error to pop up. Here is a screenshot showing the webview markdown preview working on the old process:
Clicking on the icon to opens a new window that has the webview error:
Using
alt + tab
to see all open VSCode windows (even the ones that are no longer associated with a tray icon), and closing them normally worked for me. This might be better than usingpkill
to shutdown vscode ungracefully. I have not needed to clear any cache files when shutting down VSCode this way!I can confirm that an update to VSCode was partially the cause. The old window had the following version info:
The new window has this version info:
@deepak1556 I have been able to reproduce this bug with the following:
sudo snap revert code
(this causes the tray icon to show no windows open)code <path>
)alt + tab
then the issue goes away)sudo snap revert code --revision <number>
to force the upgrade again with an existing code instance running.Hope this helps!
You can kill all running vscode processes with
pkill code
.Ran into this just now; the cause was probably that I was still running āoldā code instances alongside new ones (updating a snap will cause the Ubuntu launcher icon to indicate no instances are running, since the path to the executable has changed). I have no idea whether the āoldā code instance was responsible for a stray headless process, or I just didnāt notice an open window in one of my workspaces; but killing them made the issue go away.
If you are using Ubuntu, there is probably another (maybe hidden) vscode process, which is causing the problem.
Close the vs code first and in terminal try:
killall code
Issue still persist on latest version
I had the same issue with Mac OS 13.0.1 running VS Code
1.73.1
. Putting my resolution out here for anyone how needs it.sudo pkill code
and then force close any VS Code instances. Thanks @milnomadaI also have this issue on Linux. (Version VScode: 1.66.0) Rebooting of computer fixed the issue, thanks!
pkill code
works for meCopying my comment from the āWindowsā threadā¦
Running
vscode 1.58.1
on Linux.Multiple extensions that use WebView all error with:
code --no-sandbox
does not help, same errors.However, changing my
--user-data-dir
(that forced a re-install of all extensions) did make it work!Obviously, that is not preferred due to many years of customizing - that I do not want to start all over with.
(itās installed via Snap)
Developer Tools before opening a file that uses webview has this error:
Developer Tools after I open a file that fails webview:
Additional info:
Hereās my
settings.json
that has been scrubbed from sensitive information:I tried to downgrade to VSCode 1.55; but, when opening a file now for webview, it doesnāt seem to trigger the extensions any longer (something broke).
Rebooting is not an option for me, and I love the Git Graph extension. As well as ability to view images and other neats within vscode tabs. Please consider re-opening and fixing. Thanks
rm -rf .config/Code/Service\ Worker/{CacheStorage,ScriptCache}
followed bypkill code
worked for me without restarting my whole machine.I am using Fedora and I had the same problem, and believe or not, the problem was because of Brave, do not ask me why. I tried get copilot working and I got this error.
What I did was that:
~/.config/Code
Done. VSCode working again as well as copilot.
I have a known solution on Ubuntu 22.04
I deleted my ā~/.config/Codeā folder and was able to restore functionality. Its going to be a pain installing all my extensions again, but Iāve narrowed down the error ( at least in my system )
You can probably fix the issue by clearing the Code folder, or by clearing your āsettingsā. What I suspect is that VS code has some older config-settings which break when updating the version, or become corrupted by some other process.
Iām facing the same error for the internal Markdown preview as well as the Markdown Enhanced extension and the Code Whisperer Reference Log on Windows using 1.74.2.
Using just pure
pkill code
fixed this on ubuntu for me just now. Clearly there is a defect here.` ~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION=āUbuntu 20.04.4 LTSā `
this has worked for me on manjaro linux
This is still a problem with 1.64 just released, CentOS8/RHEL8, and moving from 1.61 to 1.64 but the ~/.config/Code folder is much older.
This solution worked for me
Only pkill works for me.
Version: 1.62.3 Commit: ccbaa2d27e38e5afa3e5c21c1c7bef4657064247 Date: 2021-11-17T08:00:36.721Z Electron: 13.5.2 Chrome: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Linux x64 5.11.0-38-generic snap
I just found out that killing all vscode jobs through the activity monitor also solves the problem. In this way, one does not need to restart the machine like I was doing before.
Ubuntu 18.04.6 LTS
I experience the same issue on Ubuntu 21.04., with the latest official VSCode version.
If anyone finds a reliable way to repro the issue it would help with further debugging, thanks!
Iām on Windows and can repro this error in a new blank Jupyter notebook.
pkill code rm -rf .config/Code/Service\ Worker/{CacheStorage,ScriptCache}
Obrigado, essa solução funcionou para mim, estou usando a versão 23.x do ubuntu
On windows thatās %AppData%\Code\Cache
Iām not sure why #178834 was closed, because this issue is happening when updating to 1.77, and 1.76.2 worked perfectly. I have been checking and this change should have caused the issue:
https://github.com/microsoft/vscode/pull/177716/commits/f71995e0b16ec2429cbbd221714984197ff5259d#diff-2f29d5590142a69ba7b8500a64b19f6bffb00b3b6bace7329976f6e5fea88649
My extension is totally broken because the webview cannot show my web app and itās kind of difficult to send you a reproducible test case.
I am seeing this when trying to view the latest Release Notes:
Error loading webview: Error: Could not register service workers: InvalidStateError: Failed to register a ServiceWorker: The document is in an invalid stateā¦
If anyone is using Brave, please turn off the brave shields as well.
Restarting worked for me, I also re-installed jupyter notebook extension!
I also get this on windows easily, using the VSCode Lean4 extension which also has a webview - by simply running ācode .ā too quickly. If I let the previous VS code fully shutdown then the next ācode .ā works fine. But I have to give it 10 seconds or so. It would be nice if vscode would fix this as it is very annoying.
I just run into this issue while working on a new extension after updating vscode to the most recent version. So far I havenāt experienced this issue and the code was more or less unmodified since yesterday where I extensively tested all changes.
Iāve updated vs-code today via the
.deb
package provided on the official site. Before I had it installed via snap but I found multiple comments that mentioned to use the official.deb
package in favor of the snap one on Ubuntu systems.What I figured out is that when I run the extension via the
Run Extension
debug option, every second restart of vscode (not the child environment started via theRun Extension
command but the actual development editor!) all Webviews work as expected within the child editor, but every odd restart they do not. To me this sounds like something in regards to Webviews is cached here and not fully restorable on a reload which is then removed and hence works on every 2nd restartStill having this issue: v1.63.2
Thanks @mattangus, those steps are really helpful. I can confirm the issue on my end now.
Anecdote: This appeared after vscode update to 1.16.0 x64 for me today, but after trying reboot as suggested above, which didnāt help, Chrome Google Chrome 94.0.4606.81 update came along through auto updates. I took it, and without reboot and only restart of vscode, the problem is gone. This is on Ubuntu 20.04 with snap vscode.
As a reminder from my earlier debugging, when using
--user-data-dir
for a new empty directory (and vscode re-downloads all extensions), it works fine. Removing the--user-data-dir
and going back to my original setup, itās broken again.However, with so many reports out there, this is not an isolated case. So we appreciate the additional debugging.
Thanks for the logs, @mjbvz here is what we know from it. The following is the loading trace comparison of draw-io extension from my machine and from the issue reporter. I will skip to the part which starts loading the webview container.
vscode-webview://{{uuid}}/index.html?id=3d61c755-1203-4a27-8874-30a8c77cc9c2&swVersion=2&extensionId=hediet.vscode-drawio&platform=electron&vscode-resource-base-authority=vscode-resource.vscode-webview.net
gets committed on both these traces, which means there is successful navigation to the container startpoint and the render process is locked to this origin.Working device:
Error device:
Next we get a resource request for
"vscode-webview://{{uuid}}/host.js"
which also gets completed for both scenariosAfter which get resource request for
"vscode-webview://{{uuid}}/main.js"
, this is the file which tries to register the service worker https://github.com/microsoft/vscode/blob/c1b4cf00b93a83f810055b52ba2ceb08be1432a1/src/vs/workbench/contrib/webview/browser/pre/main.js#L207-L240The resource request to
main.js
completes for both scenarios, but the difference comes in thenavigator.serviceWorker.register
call. For the successful scenario we pass the security checks in blink and continue with service worker loading as you can see belowBut for the failure case we are hitting this condition https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/service_worker/service_worker_container.cc;l=312-319 which is the DOM exception the user receives in the end.
Based on some early debugging the provider can be empty under the following conditions:
From the trace we are definitely not hitting 1) and based on https://github.com/microsoft/vscode/blob/c1b4cf00b93a83f810055b52ba2ceb08be1432a1/src/vs/workbench/contrib/webview/browser/webviewElement.ts#L357-L376 we only edit the sandbox properties during the webview container creation and not after that, and this steps happens way before the
main.js
file gets requested. Are there other places where we edit the sandbox properties ?So we are hitting some other edge case that is not covered above, will ping the chromium team to understand further or if we can get better debug logs.
Looks like some warnings for
--enable-tracing
and--trace-startup-file
as valid options.However, there does seem to be something in the log file (see attached).
chrometrace.log