WSL: Localhost relay does not support dual-mode sockets

Your Windows build number: (Type ver at a Windows Command Prompt)

Microsoft Windows [Version 10.0.19551.1005]

What you’re doing and what’s happening:

I am trying to start-up a webserver and have it listen on a specific port. Then when I try to reach this website using a service like localtest.me it says it’s unreachable, this is specifically happening when I try to run shadow-cljs on WSL2. If I run it on a WSL distro it works fine. After some talking with the author of that package I was able to boil down a reproducible issue to:

  1. In a fresh instance of Ubuntu with WSL2 enabled run nc :: 8080 -l
  2. Try to connect to nc, on Windows with any browser, at the address 127.0.0.1:8080.

What’s wrong / what should be happening instead:

When opening up a server and telling it to listen on ‘::’ and port 8080, the server becomes unreachable on the Windows side when trying to access it through a browser on 127.0.0.1:8080. However, the server is reachable through the browser when accessing it through localhost:8080. This workflow used to work on WSL1. I have tested this on a multitude of ports and it never works.

Additionally, this works fine if I use nc 0.0.0.0 8080 -l instead. It is reachable by browser on 127.0.0.1 and localhost.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 19
  • Comments: 44

Most upvoted comments

Ran into this same issue trying to run an express server in wsl2. If you’re using hostnames from your hosts file, a quick workaround is to just change them to point to your local IPv6 address instead of the v4 one, so 127.0.0.1 mysite.dev becomes ::1 mysite.dev.

I don’t know why the default WSL kernel has IPv6 enabled if WSL can’t route IPv6.

This isn’t really true. It routes IPv6 fine, it just doesn’t also bind to IPv4 when you bind only to an IPv6 address. If I start a server using IPv6, like python3 -m http.server --bind ::, then I can access it fine on the Windows side at http://[::1]:8000/, I just can’t access it at http://127.0.0.1:8000/. The same is true the other way around, binding to 0.0.0.0 is only accessible via IPv4 on the Windows side.

This behavior isn’t really wrong, it’s just not the default behavior you see in most OSs. The only weird thing is that on the Linux side, it does operate in dual stack mode, so you’ll run into issues if you try to explicitly bind to both IPv4 and IPv6 in your service. You can get around this by disabling dual-stack mode when you bind to IPv6. For a node.js server, that could look something like this:

const http = require("http");
const reqHandler = (req, res) => res.end("Hello World");
http.createServer(reqHandler).listen({ port: 8000, host: "::", ipv6Only: true });
http.createServer(reqHandler).listen({ port: 8000, host: "0.0.0.0" });

With a setup like that, you will get a response on the Windows side using both http://127.0.0.1:8000/ and http://[::1]:8000/.

Obviously this isn’t ideal, it would be nice if WSL forwarded both IPv4 & IPv6 when bound to an IPv6 address, especially since it works properly from the Linux side. But I wouldn’t go as far as to say “WSL can’t route IPv6”.

I had a problem with Golang HTTP server listening IPv6 :::3010 within WSL2 Ubuntu machine.

I can’t reach it as 127.0.0.1:3010 from my Windows 10 and I’ve solved the issue redirecting IPv4 address to IPv6:

netsh interface portproxy add v4tov6 listenaddress=127.0.0.1 listenport=3010 connectaddress=::1 connectport=3010

CurrPorts software was really helpful to look into things on Windows.

And that command helped me to understand the Ubuntu situation:

sudo netstat -tulpn | grep LISTEN

Workaround, run in your wsl terminal: socat tcp-l:8080,fork,reuseaddr tcp:127.0.0.1:9090 Where 8080 is the port you are trying to access via your browser, localhost:8080 and 9090 the service that is listening in your wsl on port 9090

This continues to be an issue, and the only consistent solution I’ve found so far is this: https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/

It’s a little involved and requires you to use a differently named host than localhost, but it works. Essentially this sets up a task to be triggered when wsl starts which gets the dynamic IP address of the wsl2 instance and then adds it to your windows host file under the name wsl.

So let’s say you have an application you normally access in wsl2 by going to localhost:8080 in your browser. After setting up this script you would go to wsl:8080.

You’d need to tweak any services that try to hit the thing running in the wsl2 instance to use this new wsl endpoint instead of localhost.

We got hit by this on https://github.com/firebase/firebase-tools-ui/issues/332 – it turns all our Java programs (Firebase Emulators) experiences this issue since Java prefers to open up an IPv6 socket by default, under the assumption that it will serve both IPv4 and IPv6. (It does that even if asked to listen on 127.0.0.1, an IPv4 address.)

java -Djava.net.preferIPv4Stack=true mitigates this problem (and netstat -nl shows tcp instead of tcp6), but that prevents listening to both IPv4 and IPv6 at the same time.

@therealkenc Would it be reasonable for WSL2 to route ipv4 127.0.0.1 to ipv6 ::1 automatically like Linux? There may or may not be an IETF RFC, but a lot of programs like Java is already built under the assumptions and bridging that gap would help compatibility.

Try to connect to nc, on Windows with any browser, at the address 127.0.0.1:8080.

Right; you aren’t listening on ipv4 127.0.0.1 in your repro.

image

If you try to connect to 127.0.0.1:8080 on the linux side, the Linux kernel routes those (different) protocols automatically. The Windows->WSL ipv4 127.0.0.1 tunnel, notsomuch.

on Windows with any browser

No; MS Edge and Chrome treat the string localhost differently (Edge favors resolving localhost to ipv4 over ipv6 AFAICT). The client (a browser or otherwise) matters. Your browser tried connecting on ipv6, because that’s how localhost resolved.

Ref ongoing #4353 et al. Work-around for the time being: Use the real ipv4 subnet to listen. That can be ever-popular 0.0.0.0, or an ipv4 subnet that is routable from Windows. For example, listen on 127.0.0.1 or (for me atm) 192.168.181.47 or 192.168.181.0. Connect using an ip4 address (contrast an ipv6 address).

I’ve got a similar problem:

WSL 2 running a node server listening on [::]:3000 (node listens only to ipv6) Trying to connect via chrome to http://127.0.0.1:3000 wasn’t working.

Then following the article shared by @stevenobird I tried: netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=<WSL IP>

And http://127.0.0.1:3000 worked

https://docs.microsoft.com/en-us/windows/wsl/compare-versions#accessing-a-wsl-2-distribution-from-your-local-area-network-lan

As far as I know, IPv6 is explicitly not supported in WSL 2, see https://docs.microsoft.com/de-de/windows/wsl/compare-versions#ipv6-access

Has anyone tested disabling ipv6 as I posted above? Basically:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1

I don’t know why the default WSL kernel has IPv6 enabled if WSL can’t route IPv6.

I wasn’t sure if it was the same root cause as #4353

Your OP is fair game. #4353 was actually deemed addressed in August. I just don’t have the heart to close it. There is more than one “root cause” in that ongoing thread (contrast the original post). Appreciate the submission.

@timriker IMHO the best would be if the host could request a prefix via DHCPv6-PD and then route this to the WSL2 instance.

@timriker I tried your sysctl method but it seems not working.

I tried to start a node server without specifying the host, which makes node.js bind it to :: because it finds ipv6 is enabled.

This server therefore cannot be accessed via host.docker.internal within a docker container started by Docker Desktop. The host.docker.internal works only if the server listens on ipv4.

Disabling ipv6 on Ethernet adapters (from windows) worked for me.