WSL2 localhost access is intermittent with stuck connections
Please fill out the below information:
-
Your Windows build number: 10.0.18945.1001 (I am using WSL 2 mode)
-
What you're doing and what's happening:
Opening a URL served via a Node.js app using localhost, from a Windows browser results in page/URL never loading. It will spin indefinitely trying to load, rather than getting an error that site/page can not be found
Hitting stop and then refresh in browser will result in page loading OK Page will continue to load OK, if you send requests to it quickly, if you wait a few seconds the problem will return and the URL and site will never load
I have verified the following:
- It only happens with Node.js, I've tried Python, .NET Core and Golang web apps with out problem
- Problem only happens with localhost, if I use the IP of eth0 and access the app that way, I get no issues
- It affects Node.js 8.x and 10.x, presumably other versions too
- I've tried with Debian 9.7 and Ubuntu 18.04 distros and both have the same problem
- It makes no difference what port I use
- It makes no difference what browser is used (Edge, Chrome, Firefox)
- It makes no difference if I use Node Express or the base http server package
- Issue doesn't happen inside WSL when accessing the app with localhost using curl or wget
This seems to be a TCP socket issue with the way WSL 2 handles the new localhost bridging out to Windows
It is trivial to reproduce. Install Node.js and run the following simple server
const http = require('http')
const requestHandler = (request, response) => {
console.log(`### Request for ${request.url}`)
response.end('<h1>Hello Node.js Server!</h1>')
}
const server = http.createServer(requestHandler)
server.listen(3000, '0.0.0.0', (err) => {
if (err) return console.log('### Something bad happened', err)
console.log(`### Server is listening on 3000`)
})
-
What's wrong / what should be happening instead: HTTP request /page should instantly load
-
Strace of the failing command, if applicable:
NA -
For WSL launch issues, please [collect detailed logs] NA
See our contributing instructions for assistance.
Here is a short screen capture illustrating what I am seeing

Confirmation this is a TCP keep alive issue Setting the following
server.keepAliveTimeout = 0
Resolves the issue
The setting keepAliveTimeout is described in more detail here but it changes how TCP keepalives are handled, setting to zero disables keepalives
But this change shouldn't be necessary and has other implications
10.0.18945.1001 (I am using WSL 2 mode) Same here
Phew thought I was going crazy! It's such a specific narrow problem...
I'm sure it's WSL 2 at fault here, as accessing over the WSL eth0 IP the problem goes away, so it's somehow related to whatever network trickery they use to get localhost working
Facing the same issue with apache.
Windows build : 10.0.18945.1001 with Ubuntu on WSL2 fresh install.
Accessing a website on localhost works only once in two. Like benc-uk with nodejs.
Turning off KeepAlive in apache conf resolve the issue.
Thanks
I can also reproduce this, got the same problem with Apache/httpd. Good to know it's not just Node.js at fault here
Seeing the same "one request in every two" weird behavior
Thanks for posting. I will dig into this.
I'm facing a similar issue, using NodeJs (v12.4.0) it only works once after that I get ERR_CONNECTION_REFUSED, the same in my Elixir(1.9.1)-Phoenix project. Direct connection using the ip works just fine.
Just confirms it happens with Java Tomcat too
I've been experiencing this issue with node.js servers as well. Thanks @benc-uk for mentioning the stopgap solution with keepAliveTimeout.
For those using webpack-dev-server who may find this issue, you can set the keepAliveTimeout in your webpack.config.js using the devServer.before hook, as shown below. Then your assets should be served without issue.
devServer: {
before: function(app, webpackServer) {
// We override the listen() function to set keepAliveTimeout.
// See: https://github.com/microsoft/WSL/issues/4340
// Original listen(): https://github.com/webpack/webpack-dev-server/blob/f80e2ae101e25985f0d7e3e9af36c307bfc163d2/lib/Server.js#L744
const { listen } = webpackServer
webpackServer.listen = function(...args) {
const server = listen.call(this, ...args)
server.keepAliveTimeout = 0
return server
}
}
}
I'm not sure, but my problem has been softened with this: sudo -i && echo 200000 > /proc/sys/net/ipv4/tcp_keepalive_time && echo 100 > /proc/sys/net/ipv4/tcp_keepalive_intvl && echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes In Elixir everything is working until react-apolo crash the connection somehow.
Thanks @karthikv I've used a similar workaround when serving directly in Express
var server = require('http').createServer(app);
server.keepAliveTimeout = 0; // This is a workaround for WSL v2 issues
server.listen(port);
Which I'm using rather than the more normal app.listen(port);
I'd rather not have to workaround this in all my code, and I'm sure it has performance implications
I am facing the same problem with rails application. Reloading problem. This keepaliveTimeOut is node specific. Anything that needs to be done for a rails app? I did try persistent_timeout 0 for puma configuration but not working.
I am facing the same problem with rails application. Reloading problem. This keepaliveTimeOut is node specific. Anything that needs to be done for a rails app? I did try persistent_timeout 0 for puma configuration but not working.
For Rails with Puma, I'm using the eth0 IP address without problem. Localhost works for a few requests and then stops. The IP address works pretty well so far.
Edit: Actually, IP address almost works great. I have a subdomain I need to access in my app, `foo.localhost:3000' works nicely in Chrome (except that localhost is now completely broken). Now I'm manually changing the Windows HOSTS file (\Windows\System32\drivers\etc\hosts for reference) to make this work
@tomfakes, try this: https://github.com/shayne/wsl2-hacks This one, install a service to update hosts file automatically: https://github.com/shayne/go-wsl2-host
@benc-uk and @karthiv suggestion did not work for me with restify. I was able to get my things done by setting that on Firefox itself...
I just upgraded to 18956 and my 'localhost' access from Windows to Ubuntu seems to be much more reliable so far
I also updated to 18956, but didn't see any changes, this problem still exists.
Confirming the same issue using flask and docker-compose. Sporadic connection from localhost and interestingly no connection at all when I try 127.0.0.1. Both domains work fine on docker desktop.
I've mostly switched to using named hosts in the Windows hosts file. This week I'm working on multiple cross-domain apps, so having a broken localhost actually works to my advantage.
But for short tests, localhost is more reliable this version than it was in 18950.