Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSL2 localhost access is intermittent with stuck connections #4340

Closed
benc-uk opened this issue Jul 27, 2019 · 103 comments
Closed

WSL2 localhost access is intermittent with stuck connections #4340

benc-uk opened this issue Jul 27, 2019 · 103 comments
Assignees
Labels
fixedininsiderbuilds network wsl2 Issue/feature applies to WSL 2

Comments

@benc-uk
Copy link

benc-uk commented Jul 27, 2019

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.

@benc-uk benc-uk changed the title WSL2 (18945) localhost access is intermittent and hangs WSL2 (18945) localhost access is intermittent with stuck connections Jul 27, 2019
@benc-uk
Copy link
Author

benc-uk commented Jul 27, 2019

Here is a short screen capture illustrating what I am seeing

@benc-uk
Copy link
Author

benc-uk commented Jul 27, 2019

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

@mdragosv
Copy link

10.0.18945.1001 (I am using WSL 2 mode)
Same here

@benc-uk
Copy link
Author

benc-uk commented Jul 27, 2019

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

@stefankummer
Copy link

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.

@benc-uk
Copy link
Author

benc-uk commented Jul 27, 2019

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

@craigloewen-msft craigloewen-msft added network wsl2 Issue/feature applies to WSL 2 labels Jul 28, 2019
@benhillis
Copy link
Member

Thanks for posting. I will dig into this.

@benhillis benhillis self-assigned this Jul 31, 2019
@benhillis benhillis added the bug label Jul 31, 2019
@ghost
Copy link

ghost commented Jul 31, 2019

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.

@benc-uk
Copy link
Author

benc-uk commented Jul 31, 2019

Just confirms it happens with Java Tomcat too

@karthikv
Copy link

karthikv commented Jul 31, 2019

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

@ghost
Copy link

ghost commented Jul 31, 2019

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.

@benc-uk
Copy link
Author

benc-uk commented Jul 31, 2019

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

@cvkmohan
Copy link

cvkmohan commented Aug 2, 2019

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.

@tomfakes
Copy link

tomfakes commented Aug 2, 2019

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

@ghost
Copy link

ghost commented Aug 2, 2019

@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

@villasv
Copy link

villasv commented Aug 3, 2019

@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...

@tomfakes
Copy link

tomfakes commented Aug 8, 2019

I just upgraded to 18956 and my 'localhost' access from Windows to Ubuntu seems to be much more reliable so far

@xylinq
Copy link

xylinq commented Aug 9, 2019

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

@koopmac
Copy link

koopmac commented Aug 9, 2019

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.

@tomfakes
Copy link

tomfakes commented Aug 9, 2019

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.

@montymole
Copy link

still breaks in 18956

@FlipperPA
Copy link

@benhillis I've just upgraded to 19023 and this seems to be happening again. I run Django's runserver on port 8000 on Ubuntu 18. I had the intermittent problem, then it worked fine for a while, now it isn't working at all. I've tried several Windows reboots / restarts of Ubuntu.

If I go directly to the IP of the guest, 172.20.15.250, it comes up.

A picture is worth a thousand words:

image

@benhillis
Copy link
Member

@FlipperPA - Could you give me a set of command line repro steps I could try? Also would you mind taking a trace?

https://github.com/microsoft/WSL/blob/master/CONTRIBUTING.md#8-detailed-logs

@stefankummer
Copy link

Well something weird for me on 19018.1.
Every first start of windows (after computer turned off), I am unable to access to http port (apache server started) via localhost, only via the IP of WSL2.
After restarting computer, I can access without restriction to localhost webserver.

@FlipperPA
Copy link

@benhillis @stefankummer I'll see if I can come up with a solid repro, but it is now working... which is especially weird, because I had tried restarting Ubuntu 18 and restarting all of Windows before posting above. I'll report back if I can get it to happen again!

@laem
Copy link

laem commented Nov 23, 2019

Serving files for me (express or http.server, doesn't matter) on 19030 is extremely slow on localhost (you can see images charging like with old 56k modems).
When I use the eth0 ip, it's super fast.

@benc-uk
Copy link
Author

benc-uk commented Dec 20, 2019

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! :(

@benc-uk benc-uk changed the title WSL2 (18945) localhost access is intermittent with stuck connections WSL2 localhost access is intermittent with stuck connections Dec 20, 2019
@benc-uk
Copy link
Author

benc-uk commented Dec 20, 2019

I think my latest issue is actually this #4769

If I "reboot" WSL then then first connection/service binding works, all other services after the first don't work

@benhillis
Copy link
Member

@benc-uk - fix is inbound.

@GameBurrow
Copy link

GameBurrow commented Jan 18, 2020

Seems like I have similar issue with Apache in Docker (for WSL 2) on build 19041.21 (slow insider ring)
When I had 127.0.0.1 on windows hosts only first response from server was gotten (index file, nothing else (js and css) got through).

When I replaced that with the WSL IP, everything started working again.

@khuongduybui
Copy link

khuongduybui commented Jan 19, 2020

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.

@benc-uk
Copy link
Author

benc-uk commented Jan 24, 2020

Closing for now as #4769 has been closed

@benc-uk benc-uk closed this as completed Jan 24, 2020
@Himakar-PV
Copy link

Hi,
I downloaded Microsoft Windows [Version 10.0.19592.1001] through insider (Fast)

It has vhost issue as explained below :

I have two projects

Laravel (var/www/html/laravel/..)
Pimcore (var/www/html/pimcore/..)
I have two configs under /etc/apache2/sites-available

laravel.dev.config
pimcore.dev.config
NOTE : Both are not enabled yet

In hosts file (/etc/hosts) I added below lines :
127.0.0.1 laravel.dev
127.0.0.1 pimcore.dev
::1 pimcore.dev
::1 laravel.dev

Now in browser when I type "localhost", it loads default Apache page.

Scenario 1 :
Run below commands
sudo a2ensite laravel.dev.config
sudo service apache2 restart
Type localhost in browser it loads Laravel project
Type laravel.dev in browser, loads nothing

Scenario 2 :
Run below commands
sudo a2dissite laravel.dev.config
sudo a2ensite pimcore.dev.config
sudo service apache2 restart
Type localhost in browser it loads pimcore project
Type pimcore.dev in browser, loads nothing

Scenario 3 :
Run below commands
sudo a2ensite pimcore.dev.config
sudo a2ensite laravel.dev.config
sudo service apavhe2 restart
Type localhost in browser it loads Laravel project
Type laravel.dev in browser, loads nothing
Type pimcore.dev in browser, loads nothing.
I wonder how localhost is loading one of the projects by default !!?
In scenario 3 if I enable pimcore.dev.config after laravel.dev.config then localhost is loading Laravel project instead of pimcore by default !!!

I am ready to screen share if anyone is ready to help.
Thank you.

@firrae
Copy link

firrae commented Dec 31, 2020

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.

@GiancaDIFI
Copy link

I've been having this issue since I installed docker-desktop

@vasekboch
Copy link

@Himakar-PV Its not related to this issue. You need to setup your hosts in Windows c:\Windows\System32\drivers\etc\hosts Then it will work. AFAIK Apache select one active project as default. So thats why it loads something.

@fsoikin
Copy link

fsoikin commented Jan 27, 2022

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

@fmiqbal
Copy link

fmiqbal commented Jan 31, 2022

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

@fsoikin
Copy link

fsoikin commented Jan 31, 2022

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

Edit: nope, still happening

@clovis1122
Copy link

Happening to me as well.

C:\Users\clovi> wsl --version
WSL version: 0.51.2.0
Kernel version: 5.10.81.1
WSLg version: 1.0.30
Windows version: 10.0.22543.1000

@fsoikin
Copy link

fsoikin commented Feb 9, 2022

@clovis1122 I have determined this is a bug in the latest version of WSL, the one available via the Microsoft Store.

But the good news is, an older version is available - the one bundled with Windows, - and it does not have this issue.

To install the bundled (aka "in-box") version, you need to (1) explicitly uninstall the Microsoft Store version, and then (2) wsl --install --inbox.

You'll know you got the right version if it doesn't have a --version command:

>  wsl --version
Invalid command line option: --version
Copyright (c) Microsoft Corporation. All rights reserved.

Usage: wsl.exe [Argument] [Options...] [CommandLine]
...

@illepic
Copy link

illepic commented Jan 30, 2023

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

@khuongduybui
Copy link

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

@illepic
Copy link

illepic commented Jan 30, 2023

Edit: Downgrade to 1.0.3: #9508

@pmartincic
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixedininsiderbuilds network wsl2 Issue/feature applies to WSL 2
Projects
None yet
Development

No branches or pull requests