WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Only one port can be accessed by the host through localhost at the same time

Open zed76r opened this issue 6 years ago • 84 comments

Windows build number: 10.0.19536.1000

Steps to reproduce:

  1. Listen on port A vianc -l <PortA>.
  2. The browser accesses http://localhost:PortA, and the console can output the browser request body.
  3. Listen on port B via nc -l <PortB>.
  4. When the browser accesses http://localhost:PortB, the error ERR_CONNECTION_REFUSED directly appears.
  5. Shut down wsl2 through the wsl.exe --shutdown command, repeat the test in the order of 3, 4, 1, 2, and the result is that PortB can receive the packet, and PortA has an error ERR_CONNECTION_REFUSED.

Another reproduction method:

Using vscode's wsl-remote plug-in, it can be started correctly through the code . command for the first time. After closing vscode and using the command to open it again, the file under wsl cannot be displayed and an error message is displayed.

zed76r avatar Dec 17 '19 12:12 zed76r

I think I'm running into this issue as well. I've just upgraded to 19536 and can't use vscode and my localhost:3000 app at the same time.

BabyDino avatar Dec 17 '19 12:12 BabyDino

I have exactly the same issue:

  1. Opening a Remote WSL instance in VSCode (works)
  2. Run vue ui
  3. localhost:8000 gives error ERR_CONNECTION_REFUSED

After wsl --shutdown

  1. Run vue ui
  2. localhost:8000 works
  3. Open a Remote WSL instance in VSCode gives error image

ad-on-is avatar Dec 17 '19 15:12 ad-on-is

Similar issue to previous comments, but slightly different error path.

Windows 10 Pro Insider Preview Build 19536.rs_prerelease.191211-1446 (WSL 2 Enabled)

  1. Open a Remote WSL instance.
  2. Start node server on WSL (running in a Docker container for an additional complication 🙂 )
  3. Cannot access the server at localhost:3000, which was working prior to Windows update.

wlbrough avatar Dec 17 '19 17:12 wlbrough

Same issue, multiple connections to localhost doesn't work after the update 19536.1000

Issue Identical to @ad-on-is 's comment.

Don't know much about WSL's network arch to debug further.

prsnnami avatar Dec 18 '19 15:12 prsnnami

I have noticed that this is infinitely more flakey than just one port being available.

  • I have Docker on my machine setup with WSL2 as its backend. I can run as many containers as I like on Docker and all the ports there get correctly exposed to the host.
  • With Ubuntu, however, only the first opened port ever gets exposed to the host, and even that never works if the process dies and then I try to re-bind that port (a good example being VS Code -- if I have to re-open the window, the port won't bind).
  • Lastly, with CentOS (which I installed to test a theory about the port issue), I can't bind any ports whatsoever, they all fail.

Not sure what steps I can/should take to debug WSL2 networking, but I can confirm the update to 19536 is what broke me.

berwyn avatar Dec 18 '19 18:12 berwyn

Thanks for reporting, there is an issue in Insider Fast, and a fix is coming in a future build (hopefully next week).

benhillis avatar Dec 18 '19 18:12 benhillis

I have a LAMP setup on Ubuntu and I have the same issue. I cannot access my sites when using localhost.

Until the fix is coming, I found a workaround. Do a ifconfig and take the inet value of the eth0 connection and use it instead of localhost (for example in your host file).

The only problem is this address changes when the VM restart.

Windows 10.0.19041

alexisferat avatar Dec 19 '19 08:12 alexisferat

Is there a way to downgrade to previous build?

GMory avatar Dec 19 '19 16:12 GMory

Settings -> Update & Security -> Recovery -> Go back to a previous version of Windows 10

This should allow you to go back to the build you were running prior to this one. Make sure to pause updates from there, else your system will attempt to upgrade again to the problematic build.

image

berwyn avatar Dec 19 '19 16:12 berwyn

I witness that after 19536 update, it started to give me this same error.

OctavioBR avatar Dec 20 '19 19:12 OctavioBR

I ran into this problem as well. I experimented a little and found out that the problem is better described as "one port per ip address".

For example, I start server A on port 3000 and server B on 4000. If I access server A first on 127.0.01:3000 on the windows side, but I cannot access server B on 127.0.0.1:4000. However, I can access server B using wsl's ip address (let's say 172.23.134.64:4000). If I start another server C on port 5000, I cannot access it from either ip.

So if you are like me, that are only running at most 2 servers in WSL, you are fine for now, and can wait for the update to drop.

alechstong avatar Dec 24 '19 15:12 alechstong

@benhillis would you share an update on the fix? Any vague ETA?

hristo-banchev avatar Jan 02 '20 13:01 hristo-banchev

Any news on the fix?

puco avatar Jan 06 '20 11:01 puco

Any news on the fix?

mituso89 avatar Jan 08 '20 13:01 mituso89

Fix was delayed by holiday break but still inbound.

benhillis avatar Jan 08 '20 14:01 benhillis

Just installed 19541. Works for me :)

BabyDino avatar Jan 08 '20 22:01 BabyDino

19541 fixed it for me as well. Thanks @benhillis & WSL team!

wlbrough avatar Jan 09 '20 01:01 wlbrough

19541 Seems to have fixed vscode not being able to "fetch remote environment",

However it does NOT seem to have fixed the issue of not being able to access more than one port at a time.

I have a React project which runs on localhost:3000, and a Gatsby project which runs on localhost:8000, previous to this issue, I could run both of them simultaneously and access them from windows on Chrome.

Follow these steps to recreate the behavior:

  • Open WSL2
  • Go to the React project root directory and run npm start to start the server running onlocalhost:3000
  • On a new tab, go to the root directory of the Gatsby project and run npm start, this starts the server on localhost:8000
  • Open Google Chrome and go to localhost:3000, you should see the react project.
  • Now try to go to localhost:8000, you will not be able to see the Gatsby project, and you should see an error like the following:

image

Fun thing is you can stop both the servers, ONLY start the gatsby project on localhost:8000, and you will get the same error, but if you start another react project, it'll be fine on localhost:3000.

It seems like the first port you use while WSL2 is active sticks and becomes the only one you can see from Windows, running wsl --shutdown on a command prompt window and starting WSL2 again seems to reset the slate, so say now you start the Gatsby project on localhost:8000 first then stop it right away, you won't be able to see any apps running on localhost:3000 if you start them after starting the localhost:8000 app, you will only have port 8000 visible on windows.

penavincent avatar Jan 09 '20 03:01 penavincent

@Joe-Pena i observed the same behavior before updating to 19541. :( was feeling hopeful about this version. Does VSCode work while your server is occupying a port? I've had to resort to just using VIM while leaving my react dev server up..

rabbitXIII avatar Jan 09 '20 03:01 rabbitXIII

19541 fixed it for me as well. I can able to access port. Thank @benhillis

mituso89 avatar Jan 09 '20 03:01 mituso89