WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Cannot use localhost to access a react-app running in WSL2

Open kimfucious opened this issue 6 years ago • 81 comments

Microsoft Windows [Version 10.0.19551.1005]

Steps to reproduce the problem:

  1. Execute npm start to launch a react app in WSL2.
  2. Open http://localhost:3000 in a browser in Windows.
  • What's wrong / what should be happening instead: Browser cannot open the site / the website should open.

I know this is an existing issue, and that there are other bug reports filed (with no apparent solutions), and I know that I'm running a pre-stable setup, so I'm not moaning.

I've recently switched to the slow lane.

That said, I simply can't use WSL2 for developing anything without access to localhost from a browser, and using an ip address that changes regularly is not a valid workaround. There are just too many APIs that use callbacks, redirects, scope, etc. that to code around this issue is too much effort.

At present, the only suitable, yet non-ideal, workaround is to not use WSL at all, clone my repo into Windows, and run everything in powershell.

kimfucious avatar Feb 10 '20 18:02 kimfucious

Have you tried to use the IP of your computer instead of localhost? I saw before something related.

fredyfx avatar Feb 11 '20 00:02 fredyfx

Since you're on 19551 this should work. Maybe your firewall blocks port 3000? Also check if your app is really configured to listen on port 3000?

ad-on-is avatar Feb 11 '20 13:02 ad-on-is

Thanks @fredyfx

Using the IP address does work, but it's localhost that I need/want, mainly due to how the app works.

kimfucious avatar Feb 11 '20 17:02 kimfucious

Thanks @ad-on-is

Ya know, if I weren't a complete idiot, I would have thought about setting the firewall to allow port 3000! But I am so, I didn't.

Note: I need to say that over the past few months, with various Windows Insider releases, this has worked and not worked intermittently.

So, I created the firewall rule, before checking right before doing this. And, voila, it worked.

Then, as a sanity check, I disabled the firewall rule, and tried again. And it still works.

So, I started starting apps on varying ports (3001, 8000, 9999) and accessing them w/o any firewall rules, and they worked.

I checked and no updates were applied since I posted this initial message.

The one thing that is different today, is that I have no Visual Studio Code remote sessions running, because I'm doing all the coding in windows, not in WSL.

So I launched a remote coding session in VScode and tried to access the site, and it worked.

Per the note above, in that this is an intermittent problem, I'm going to leave this open and check the behavior periodically for a week, reporting in if I re-discover the issue, and closing it it is actually gone.

kimfucious avatar Feb 11 '20 17:02 kimfucious

I have an ongoing challenge with localhost not forwarding from Windows 10 (multiple versions, but currently 19041.113) to my WSL instance.

A simple test WSL: nc -lk 9999

And, when sending: From WSL - nc localhost 9999 always works. From Windows 10, ncat localhost 9999 sometimes works - and, if it works once, it never seems to stop working as long as I don't suspend or power cycle my system. When ncat localhost 9999 fails (and an ncat -zv localhost 9999 shows the port isn't respond), then ncat wslipaddress 9999 always works - so, for whatever reason, localhost just decides not to forward to my wsl instance.

I'm only ever running a single WSL instance. It sometimes occurs after a power cycle.

The change in state between forward/not forward isn't associated with any firewall rules (I've disabled all of them) or config changes on WSL (I haven't touched them).

I'm kind of stuck on creating an thoroughly repeatable case, but I'd be happy to dig in and provide further diagnostics if someone can think of something for me to try when it occurs, as it appears to do on a semi-periodic basis (I run Jupyter-Labs against my WSL instance, so I get lots of opportunity to see this happen.)

ghshephard avatar Mar 14 '20 02:03 ghshephard

Update - system was just updated to 19041.153. I accepted the restart, and upon starting WSL got the request to update through https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel which I did.

Came into WSL, and tried nc -lk 9999 and in windows 10 ncat localhost 9999 - this failed. Immediately tried .\ncat.exe 172.31.141.125 9999 (the IP of the WSL instance) - and this worked immediately - tried localhost again, and that failed.

Tried a wsl --shutdown - that did not work. So, my localhost forwarding is now wedged again. I'll see what sequence of activity I take that results in it working again.

ghshephard avatar Mar 14 '20 16:03 ghshephard

I've had several reboots - and so far Build 19041.172 hasn't stopped forwarding. Fingers crossed that something was changed that resolved this. I'll keep rebooting and banging on it and see if it happens again.

ghshephard avatar Mar 30 '20 02:03 ghshephard

I have a same problem.

default via 172.29.64.1 dev eth0
172.29.64.0/20 dev eth0 proto kernel scope link src 172.29.71.101

I'm using the nodejs API gateway on port 6565 If I use 192.29.71.101, then I can access it, but it doesn’t work through the localhost Build 19041.172

vadimjoy avatar Apr 08 '20 09:04 vadimjoy

I am using WSL2 too, I tried to install postgresql in my WSL, I cannot access it using TablePlus if I am using localhost, only works if I use ip address.

OS Build: 19041.207

rained23 avatar Apr 17 '20 03:04 rained23

From this issue #4636

  1. Try to enable Windows Sandbox & Windows Hypervisor on Windows features
  2. Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞

Step 2 must be performed after each computer shutdown. Then the localhost becomes available again. I'm not shure about first step, but turned on the sandbox just in case. Build 19041.207

vadimjoy avatar Apr 17 '20 10:04 vadimjoy

From this issue #4636

  1. Try to enable Windows Sandbox & Windows Hypervisor on Windows features
  2. Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞

Step 2 must be performed after each computer shutdown. Then the localhost becomes available again. I'm not shure about first step, but turned on the sandbox just in case. Build 19041.207

I'm having the same experience as vadimjoy, Intermittent problem. Restarting lxmssmanager works - but in my opinion not a long term solution.

timbophillips avatar Apr 17 '20 10:04 timbophillips

I'm having the same experience as vadimjoy, Intermittent problem. Surely we aren't expected to continually restart LxssManager for the rest of our lives?

I really hope that the guys will fix it, because I have to use different operating systems to work.

vadimjoy avatar Apr 17 '20 10:04 vadimjoy

My temp solution using powershell and Task Scheduler:

  1. Allow scripting using powershell (for example: Set-ExecutionPolicy RemoteSigned)
  2. Create a file RestartLxssManager.ps1 with the contents: Restart-Service -DisplayName LxssManager
  3. Add script execution at system startup trigger in the Task Scheduler with highest available (command: powershell.exe, arguments: -File C:\RestartLxssManager.ps1).

Exported task (replace with your YOUR_DESKTOP_NAME\user, YOUR_USER_ID):

<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2020-04-17T16:31:09.8645471</Date>
    <Author>YOUR_DESKTOP_NAME\user</Author>
    <URI>\RestartLxssManager</URI>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Enabled>true</Enabled>
      <UserId>YOUR_DESKTOP_NAME\user</UserId>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>YOUR_USER_ID</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>-File C:\RestartLxssManager.ps1</Arguments>
    </Exec>
  </Actions>
</Task>

vadimjoy avatar Apr 17 '20 12:04 vadimjoy

This should be resolved, you should be able to access it using localhost. We haven't been able to repro this on the team.

Can you provide us with a project that we can repro this on? Or can you take logs when you have this problem and submit them here?

craigloewen-msft avatar Apr 17 '20 22:04 craigloewen-msft

This should be resolved, you should be able to access it using localhost. We haven't been able to repro this on the team.

Can you provide us with a project that we can repro this on? Or can you take logs when you have this problem and submit them here?

Hi Craig,

Thanks very much for taking the time to look at this. I appreciate you are a busy gu.

I'm on build 19608.1000 and have just fresh re-installed Ubuntu-18.04 WSL2 distro.

Still happening intermittently - seemingly with any lite server I'm using for dev... angular ng serve etc,

I just now freshly installed lite-server in the WSL2 18.04 distro and it is also affected. I can't see localhost:3000 but can see 172.28.137.63:3000 in my windows browsers.

The docker WSL2 containers aren't affected - I can still see my Hasura container (for example) at localhost:8080.

wsl -t <DistroName> doesn't fix the problem Restart-Service LxssManager DOES fixt the problem

Happy to provide logs - which logs would you like?

Tim.

timbophillips avatar Apr 18 '20 02:04 timbophillips

Hi Craig,

Here are logs from Saturday (couldn't reproduce error) and this morning (error is happening right now). Screenshot also attached.

Any help from anyone greatly appreciated.

Tim

Annotation 2020-04-21 002131

logs.zip

timbophillips avatar Apr 20 '20 16:04 timbophillips

I've just upgraded to build 19613.rs_prerelease.200417-1557.... issue persists

Also now upgraded to ubuntu 20.04 from the microsoft store.... issue persists.

timbophillips avatar Apr 24 '20 12:04 timbophillips

I'm also having this issue, 19613.1000 and running Pengwin. Here are my logs wsl_logs.zip

SlowPokeInTexas avatar Apr 24 '20 18:04 SlowPokeInTexas

Interesting additional finding. When it occurs it is ports <= 4518 that are affected. WSL2 Ubuntu services listening on ports >= 4519 are not affected.

screen shot below Annotation 2020-04-26 171411

timbophillips avatar Apr 26 '20 09:04 timbophillips

Does anyone having this issue have a network set up on the host windows 10 machine that is as straightforward as a laptop with one wifi card?

I'm looking for reasons why this happens to me on both my desktop and xps 13... Even with fresh install on win 10.

Both machines have a sonicwall VPN app on them (issue doesn't seen to correlate with the VPN being connected).

The desktop has both a wired and wireless NIC both connected to the same network but with different dhcp allocated IP addresses.

timbophillips avatar May 02 '20 08:05 timbophillips