WSL: 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.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 22
  • Comments: 103 (17 by maintainers)

Most upvoted comments

Build 18975 should improve this experience greatly.

@benc-uk - A fix is on the way.

Thanks @benc-uk, my change seems to be working very well with httpd. I’m hopeful this will resolve any issues… ETA for Insider build a few weeks.

19018 should have the final fixes for this.

10.0.18945.1001 (I am using WSL 2 mode) Same here

Just updated to 19536 and this seems broken again localhost simply not working, but using the WSL IP I get a connection

I thought we had this one nailed! 😦

I also updated to 18956, but didn’t see any changes, this problem still exists.

@vasekboch - I have some great news. I installed 19002 and was able to repro the issue, I then applied my fix to the build, and the issue goes away. The fix should be in an insider build in a week or two.

I’ve checked localhost access on build 18980, and I’m not seeing any issues, still seems fixed to me 😃

Just updated to 18970 hoping it would be fixed. I spotted the WSL kernel version number got a bump to 4.19.59 so I was optimistic, however the problem is still happening 😢

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
    }
  }
}

Yes it just came back in the latest Windows update on Dev channel for me.

18990.1 is still broken for rails development 😦 will double check to make sure it’s not something I’m doing wrong. Also finding webpacker is unable to create a manifest.json on a fresh install. Have tried running rails in the wsl2 using rails server -b 0.0.0.0 -p 3000, as normal, this loads the first time when opening chrome and navigating to localhost:3000 but any changes I make to the homepage does not change on page reload. I still see the server request/response cycle but it serves the original page and not the updated page.

Note: I am also using the new Visual Studio Code Remote-WSL to edit the files.

Since did a fresh install of Ubuntu 18.04 (was 16.04) in WSL2. Installed Rails from scratch (but used my existing repo). Same problem. Might try with a fresh project and share in github as a repro of this issue.

UPDATE I have figured out my issue was that I was installing my files on an external harddrive that was mounted to windows instead of directly in the WSL2 file system. I pushed my repo to github and cloned back down into the WSL2 (using the new Terminal program 😄) ~/projects/appname directory. Spun my server back up and holy gees it is fast! localhost:3000 from the browser works, ended up getting Gaurd livereload up and running and it is soooo nice compared to WSL1. Good luck to the rest of the people in this thread. Apologies if my issue was not entirely related to yours…

I just installed 18980 today and experiencing the same issues… Starting up a simple PHP Development Server with php -S 0.0.0.0:8001. My page lodas, but the Spinner in the browser never stops. Also every request in the PHP log uses another port (not sure if this is the default behaviour though):

grafik

I was having the same issue with kubectl port-forward … sometimes it works, other times it doesn’t. Seems like it works until the port forward times out, and the next run of kubectl port forward doesn’t work. I found a workaround that will keep me going until the fix makes it’s way to an Insider build… you can run:

wsl.exe --shutdown

and that will kill off the wsl kernel (and all shell instances attached to it). When you restart your shell, everything works again. It’s annoying if you have several shell instances running and Docker Desktop WSL2 mode doesn’t recover well, but it’s workable until a fix lands.

I believe I have a fix for this. Does somebody have a test app that I could use to validate this? I’ve tried running nodejs servers and clicking refresh and that seems to be working great with my fix.

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.

@illepic, @khuongduybui: If you’re experiencing problems on 1.1.0 please see issue #9508. This issue covers 1.0.3 and before.

Just noting that this is still an issue.

PS C:\WINDOWS\system32> wsl --version
WSL version: 1.1.0.0
Kernel version: 5.15.83.1
WSLg version: 1.0.48
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.22621.1194```

The issue seems to have gone away again after upgrading to 22543

Edit: nope, still happening

I see this issue too. Started happening since the last update to 22538. Rolling back to 22533 seems to have fixed it for now.

Same here on same version, happening on PHP (73, 74, 80) vanilla built-in webserver, bound to 127.0.0.1 or 0.0.0.0 port 8000 (or anything)

  • accessed via 127.0.0.1:8000, the request will never finish, it load until it should be (I can load laravel project just fine) but the browser still show loading animation (affecting document.ready() I suppose)
  • accessed via WSL eth0 ip and it doesn’t have this problem
  • doesn’t looks like intermittent, it happen everytime

I see this issue too. Started happening since the last update to 22538. Rolling back to 22533 seems to have fixed it for now.

I am also having this issue still. I tried stopping and restarting WSL like @khuongduybui mentioned and now it works with Rails. This always seems to happen after I shut down the computer and come back to it.

Port forwarding on build 19541.1000 (slow insider channel) is also broken on first boot. When I issue wsl --shutdown and start the distro again, it works.

@benhillis No problem. Happy to help. I’m glad that this one is fixed. It was pain to fill out every time new IP adress.

Thank you for the fix. I check it a soon as it will go live. 😃

Any chance I can downgrade to 18975? 😅 @benc-uk Please consider reopening this issue

Just tested on 18975 and my Node app is back to normal, removed my workaround code and all is good. Tried a Java Spring Boot app (Tomcat) and also no problems. Also repeated my Apache 2 test

All good! 😍

Thanks for fixing!

still breaks in 18956

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

Just confirms it happens with Java Tomcat too

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

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.

Here is a short screen capture illustrating what I am seeing