Search code examples
jupyter-notebookwindows-subsystem-for-linux

Jupyter lab NetworkError running on WSL2


I am running Jupyter lab on my WSL2 Ubuntu and accessing it in Firefox on my Win10 PC. They are running on the same machine, so there should be no "Network" involved. I am running it as jupyter lab --no-browser. The Server launches just fine in the terminal:

[I 11:17:40.843 LabApp] [jupyter_nbextensions_configurator] enabled 0.4.1
[I 11:17:40.885 LabApp] JupyterLab extension loaded from /home/muthur/anaconda3/lib/python3.8/site-packages/jupyterlab  
[I 11:17:40.886 LabApp] JupyterLab application directory is /home/muthur/anaconda3/share/jupyter/lab                    
[I 11:17:40.888 LabApp] Serving notebooks from local directory: /home/muthur                                            
[I 11:17:40.888 LabApp] Jupyter Notebook 6.2.0 is running at:                                                           
[I 11:17:40.888 LabApp] http://localhost:8888/?token=...                                                                
[I 11:17:40.888 LabApp]  or http://127.0.0.1:8888/?token=...                                                            
[I 11:17:40.888 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

But when I start it in my browser, I first get "Server Connection Error: A connection to the Jupyter server could not be established. JupyterLab will continue trying to reconnect. Check your network connection or Jupyter server configuration.", which in the F12 console is due to Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource. Reloading the tab or opening it in a new one gives me the Firefox "Connection failed" error page, saying that it cannot establish a connection to 127.0.0.1:8888

I have been using this setup for the past 2-3 months without issue. Any idea why this crops up now, and how to solve it?


Solution

  • They are running on the same machine, so there should be no "Network" involved.

    Believe it or not, there's definitely a network involved when communicating between Windows and a WSL2 session. While WSL1 ran "on" the host Windows network (pseudo-bridged), WSL2 runs in a separate VM that has its own virtual NIC. That NIC is NAT'd behind the Windows interface by default.

    This means that port-forwarding is needed, so that when you access port 8888 on localhost (127.0.0.1) in the Windows browser, it is forwarded to the virtual NIC for the WSL2 instance.

    By default, this happens automatically (see the localhostForwarding setting on that page). However, sometimes it doesn't. Typically, it "breaks" when Windows is hibernated or shut down with "Fast Restart" enabled (which also does a "mini-hibernate"). Note that Fast Restart is the default in Windows, so it's easy to run into this problem.

    The solution is fairly straightforward, assuming that this is the problem that you are seeing:

    • Exit out of any running WSL instances
    • wsl --shutdown at the PowerShell or CMD prompt
    • Restart your WSL instance

    Things will hopefully hum along just fine until your next hibernate.

    More info and other suggestions to try if that doesn't work at this answer.