microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.54k stars 823 forks source link

Some localhost ports not being forwarded to WSL2 #5942

Closed grimb300 closed 4 years ago

grimb300 commented 4 years ago

Environment

Windows build number: 10.0.19042.508
Your Distribution version: Ubuntu 18.04
Whether the issue is on WSL 2 and/or WSL 1: WSL 2

Steps to reproduce

I've tried this both using create-react-app and a simple express.js server. Any server with port number <= 3088 can not be accessed via a windows browser at URI localhost:<port>. It results in a "localhost refused to connect" error. I've tried Chrome, Firefox, Brave and Edge. I also tried the Invoke-RestMethod command in PowerShell. However, it will work within WSL2 using curl.

If I change the URI from "localhost:<port>" to "<myIP>:<port>", it works in all cases.

If I change the port to anything > 3088, it works.

I've tried turning off the default Windows firewall, closing docker, rebooting without fast reboot enabled. No change in behavior.

This feels like a port forwarding issue from Windows to WSL2.

WSL logs:

Expected behavior

I'd expect any unused port to be accessible on WSL2. I haven't used create-react-app in a long time, so I'm not sure when this behavior changed.

Actual behavior

The default create-react-app port (3000) is not being forwarded to WSL2. My non-exhaustive testing has bounded the port forwarding problem to port numbers <= 3088.

therealkenc commented 4 years ago

A simple express.js server will listen on tcp6 only, which is #4851. There is no repro to confirm "any server".

grimb300 commented 4 years ago

Sorry, got bit by markdown gobbling the less/greater than symbols I was using. I think I've fixed it now.

I am talking about different ports using http protocol. The server name localhost nor what it should resolve to 127.0.0.1 is being forwarded to WSL2 for some, but not all ports. In my original case, I used the default configuration of create-react-app to instantiate a development web server. I could not access it via the following URIs using a Windows browser:

However, I could access it via this URI:

I could also access it via curl on WSL2 using any of the above URIs. Changing the port my server was listening on to 5000 fixes everything and I can access it from all URIs and requesters listed above.

BTW, netstat reports the following:

$ netstat -nl --protocol=inet,inet6
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN

Which I think means WSL2 should be responding to both localhost:3000 (which it does not) and localhost:5000 (which is does)

benhillis commented 4 years ago

Is there something running on port 3000 on the host?

grimb300 commented 4 years ago

There wasn't anything running on the ports in question.

This morning, my computer updated to build 19042.541 and the bad behavior is no longer present. No explanation, but I guess it's best to close the issue since it seems to have been something isolated to my machine.