microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.44k stars 822 forks source link

[WSL2] Network connections are not restored after returning from Windows sleep mode #4992

Open choung opened 4 years ago

choung commented 4 years ago

Your Windows build number:

10.0.19582.1001

What you're doing and what's happening:

Originally found this problem while using X410 (X Server for Windows 10) with WSL2. I was able to confirm the same problem with much simpler program, netcat (https://en.wikipedia.org/wiki/Netcat).

The following problem only happens when TCP connections are made from WSL2 to Windows. When connections are made from Windows to WSL2, they are restored after sleep mode as expected.

  1. Start netcat in Windows 10
.\ncat.exe -l -p 9999 -v

netcat for Windows is available as an executable in Nmap package (https://nmap.org/download.html) You don't need to install it. You just need to download its command-line ZIP file, unzip, go to that unzipped folder in PowerShell or Windows Command Prompt and execute the ncat.exe as shown above.

The above command puts ncat.exe into a server mode (-l) waiting for data at the port 9999 (-p).

If you have a WSL1, you can also use its 'nc' command. For example, if you have Debian in WSL1 and Ubuntu in WSL2, you can run "nc -l -p 9999 -v" in Debian and get the same result.

  1. Open WSL2 console

I used the "Ubuntu 18.04" installed from Microsoft Store.

2.1 Get the IP address for Windows host

echo $(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}')

2.2 Connect to the IP address found in Step 2.1

Assuming the IP address is 172.18.16.1:

nc -v 172.18.16.1 9999

2.3 You should get connection succeeded messages for both ncat.exe in Windows and nc in WSL2.

  1. Type some text and press [ENTER] in Windows (ncat.exe) and WSL2 (nc). You should be able to confirm that the connection is active and working in both directions as expected.

  2. Put your computer to Sleep (Windows Start -> Power -> Sleep)

  3. After confirming that your computer is fully in sleep mode, wake it up.

ncat.exe in Windows shows that the connection is lost, but nc in WSL2 thinks the connection is still active.

What's wrong / what should be happening instead:

The network connection between WSL2 and Windows is completely restored after waking up from Windows sleep mode.

The same problem can be observed with Hyper-V virtual machines (TCP connections made from VM to Windows). However, I can work around it by using its VSOCK feature. But, as far as I can tell, there seems to be no plan for VSOCK or AF_UNIX in WSL2.

All in all, such problem can be fatal for developers who create server programs for Windows that can be used by Linux apps running in WSL2.

thoni56 commented 4 years ago

I suppose this is also the reason why my WSL 2 Emacs over Xming X11 windows server have aborted with a traceback every time the computer returns from sleep.

klesniewski commented 4 years ago

Similarly here, I suppose this is the cause of issue I experience with IntelliJ IDEA run from WSL 2, connecting to X Server on Windows. After coming back from sleep, IntelliJ window is gone and I have to start it again. Unfortunately IntelliJ IDEA doesn't (yet?) support WSL for Java development, that's why I run it straight from WSL (https://intellij-support.jetbrains.com/hc/en-us/community/posts/360004275400-Developing-in-Windows-Subsystem-for-Linux and https://youtrack.jetbrains.com/issue/IDEA-223986)

chiefjester commented 4 years ago

I'm getting this as well, but I use hibernate. I'm on Version 10.0.20197.1000

It's annoying, things I did to no avail:

  1. add nameservers
  2. disable swap

restarting via WSL --shutdown does nothing (same as restarting Lxssmanager).

I have to reboot just to get connectivity back. Is there a way to initialize WSL's network stack since IP is

david50407 commented 4 years ago

Same on insider 20180.1000 (wsl kernel: 4.19.121), must reboot windows to get network back

Spaak commented 4 years ago

@thisguychris :

I have to reboot just to get connectivity back.

and @david50407 :

must reboot windows to get network back

This seems to be a different issue than the one reported by @klesniewski :

IntelliJ IDEA run from WSL 2, connecting to X Server on Windows. After coming back from sleep, IntelliJ window is gone and I have to start it again.

which is what I am experiencing as well: I'm running an X server on Windows (VcXsrv) to use graphical Linux applications. This works perfectly, except that when Windows goes into standby and wakes up again, all X windows are gone. The Linux applications don't report any error (in the console) and appear to be running fine; it's only the X output that is gone (thus rendering the apps unuseable). I can simply kill the apps and restart them, and the X windows reappear fine. No reboot required, which is why I think there are separate issues here.

chiefjester commented 4 years ago

@Spaak I was speaking at the context of the OP, which was @choung.

If this is a new issue, should I file a new one? To add, I also use an X Server. But on my end, I really need to restart Windows. Not even signout works. I really need to restart the OS to connectivity back.

cc @therealkenc

mittalsuraj18 commented 4 years ago

same problem here. for me changing the network or disconnecting and connecting apart from the above also causes the same issue. reboot is the only fix

changing nameservers and using wsl --shutdown does not work

OS: Windows Insider Dev Build - 20197.1000

bltavares commented 4 years ago

My laptop used to work fine when closing and opening the lid. Now, it requires a reboot to reconnect. wsl --shutdown didn't work either.

OS: Windows Insider Dev Build - Version 2004 - Build 20201.1000

xinntao commented 4 years ago

Same problem. After sleeping mode, I lost the internet connection for wsl2. Only reboot can solve the problem, wsl --shutdown cannot solve. Still looking for / waiting better solution!

OS: Win10 pro insider preview: build 20201.re_prerelease.200822-1922

wsl cat /proc/version: Linux version 4.19.128-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Tue Jun 23 12:58:10 UTC 2020

gradinarot commented 4 years ago

Did anyone manged to resolve this issue?

Soberia commented 4 years ago

Screenshot 2020-09-03 233239

I realized WSL Connection type in Hyper-V Virtual Switch Manager change from Internal network to Private network after this. By restarting the system, this sets back to Internal network automatically.

fkguo commented 4 years ago

I have the same issue, insider OS build 20206.1000

tbronchain commented 4 years ago

Similar issue here. It seems heavily linked with Hyper-V and stops obeying to any sort of logic once the issue starts to happen, reboot solves it. The problem occurs since the update to Windows 10 2004. Latest KB doesn't seem to resolve the issue.

llacom commented 4 years ago

Also the same issue here. I use GUI apps through an X server and this issue is highly impacting.

I do not require a reboot for things to work again, restarting the app suffices. (emacs through X410 X server).

Soberia commented 4 years ago

Temporal fix without system reboot

david50407 commented 4 years ago

@Soberia this solution is only works once. I use this fix to get my network back and sleep again then the fix won't work anymore until I reboot...

pelletierjy commented 4 years ago

Still going on here as well.

gradinarot commented 4 years ago

For me is gone after updating to Windows Insider 20236

wzin commented 4 years ago

OK so on build 20231 the wsl --shutdown works for me.

4integration commented 3 years ago

I have the same issue - very annoying.

Only reboot makes WSL2 working again. I have not seen the issue when being in the office (no VPN involved)

FutureGadget commented 3 years ago

I have the same issue.

I can't connect to my WSL2 ubuntu after returning from sleep mode. VS Code connection is lost after waking up and can't reconnect unless I reboot my pc. Also, I can't connect with Windows Terminal application.

ati46 commented 3 years ago

I used x11-over-vsock to solve the problem that the link between wsl2 and x410 is broken after the network change, but there is a problem with this library that the cpu will soar when the window is moved. I hope wsl2 can support vsock as soon as possible

helinwang commented 3 years ago

+1 it's annoying, my emacs x11 window keep closing. I just turned off sleep :(

apos commented 3 years ago

@gradinarot

For me is gone after updating to Windows Insider 20236

Really? For WSL2 - not WSL1! Could you really reproduce this?

Which channel of windows insider program are you using?

gradinarot commented 3 years ago

Yes, im using wsl2. But now I have a problem with some connections. For example doing a git pull will fail 20% of the time. This thing happenz for all ssl connections

troglotit commented 3 years ago

@gradinarot

For me is gone after updating to Windows Insider 20236

Really? For WSL2 - not WSL1! Could you really reproduce this?

Which channel of windows insider program are you using?

Folks at WSL are working on native GUI - https://devblogs.microsoft.com/commandline/whats-new-in-the-windows-subsystem-for-linux-september-2020/#gui-apps and you can already check it out in Insiders build (not sure which version though).

david50407 commented 3 years ago

@troglotit not available yet in dev channel (21277), so I think that doesn't released into all insiders yet.

smzht commented 3 years ago

@choung

The following problem only happens when TCP connections are made from WSL2 to Windows. When connections are made from Windows to WSL2, they are restored after sleep mode as expected.

How about the following method? The point is the R option of ssh.

  1. Execute the following on WSL2.

    $ sudo apt install openssh-server # if necessary
    $ sudo ssh-keygen -A # if necessary
    $ sudo service ssh start
  2. Start OpenSSH Authentication Agent service on Windows.

  3. Execute the following on Windows.

    > ssh-keygen.exe -t ed25519
    > ssh-add.exe %USERPROFILE%\.ssh\id_ed25519
  4. Execute the following on WSL2.

    $ cd
    $ mkdir .ssh  # if necessary
    $ chmod 700 .ssh # if necessary
    $ cd .ssh
    $ cat /mnt/c/Users/<Windows user>/.ssh/id_ed25519.pub >> authorized_keys
    $ chmod 600 authorized_keys
  5. Add the following to .bash_profile.

    
    if ! service ssh status > /dev/null 2>&1; then
    wsl.exe -d "$WSL_DISTRO_NAME" -u root service ssh start
    fi

if ! ss -lt4 | grep -q '127.0.0.1:6020'; then setsid ssh.exe -f -N -R 6020:127.0.0.1:6000 $USER@localhost & fi

[ -r ~/.bashrc ] && source ~/.bashrc


6. Add the following to .bashrc.

export DISPLAY=:20



7. Restart WSL2.
bertlebee commented 3 years ago

@smzht that's an interesting idea. Not really liking the idea of sending x traffic through ssh though! It's a good idea for other use cases perhaps.

smzht commented 3 years ago

@robmwalsh Ssh has an X option. It ’s similar to that.

bertlebee commented 3 years ago

@smzht worth a try I suppose. My computer would certainly appreciate being able to go to sleep again, even if it does have to encrypt display traffic that's not even leaving my computer!

Related: Is anybody from Microsoft actually working on this?

FutureGadget commented 3 years ago

This problem has gone away after I updated my Ubuntu by doing the following: sudo apt update && sudo apt upgrade.

I wrote a short article on how I solve this problem: WSL2 slowing down over time issue: Medium

spicydonkey commented 3 years ago

Same problem here. Watching to see if this problem gets solved...

vladkosarev commented 3 years ago

Same here. Upgraded to WSL2 and now things are not coming back after sleep. Using Void Linux and X410

fberlakovich commented 3 years ago

I recently discovered https://github.com/nbdd0121/wsld which provides a workaround based on vsocks. So far it works really well for my X forwarding use case.

vladkosarev commented 3 years ago

https://github.com/nbdd0121/wsld does seem to work! I had to download pre-built binary for windows because custom built executable would just insta-crash. I am running x410/Void/i3 so I had to put the snippet from wsld into .profile and set up windows daemon in task scheduler.

zwwi commented 3 years ago

I recently discovered https://github.com/nbdd0121/wsld which provides a workaround based on vsocks. So far it works really well for my X forwarding use case.

I also confirm that the wsld indeed works perfectly. The gui app has persisted for several days

Kwaadpepper commented 3 years ago

Could not use wsld (os error 10013) in any way sadly.

thoni56 commented 3 years ago

Hmm, today my computer with Windows 19042.928 came back from sleep with my X11 Emacs still running. Good! What changed?

Or maybe I should wait a bit and see if it wasn't just a one-time thing...

ast0815 commented 3 years ago

Same here on Version 1909 18363.1500. It just seems to work now. 🤷

acceleratxr commented 3 years ago

I'm experiencing this same issue with Windows 11 22000.282 and WSL2 Ubuntu 20.04. Network works fine on fresh boot, but after resuming from sleep or hibernate, WSL network connection is broken.

Dawoodshah100 commented 2 years ago

Temporal fix without system reboot

Restarting LxssManager service on Windows terminated WSL distro

md8n commented 2 years ago

Check in your /etc folder for resolv.conf and wsl.conf. If resolv.conf is missing you will get DNS resolution issues.

So check what is in your wsl.conf, assuming that you have one ...

If it contains:

[network]
generateResolvConf = false

Then WSL is not going to regenerate the resolv.conf for you and you have to create/edit your own (e.g. sudo nano /etc/resolv.conf and make sure it is pointing at some nameservers.

The default for generateResolvConf is supposed to be true (from what I understand), but sometimes that doesn't seem to be correct ...

So you can explicitly set generateResolvConf = true in the /etc/wsl.conf, and then reboot (yep), to get it to generate resolv.conf for you.

wzin commented 2 years ago

This has been resolved since few windows versions ago.

PrimeDominus commented 1 year ago

This problem still exists on Windows 11 build 22621.1265 on a fairly fresh install.

scruel commented 1 year ago

To prevent rebooting the whole system, you can try: wsl-restart.bat

bertlebee commented 1 year ago

To prevent rebooting the whole system, you can try: wsl-restart.bat

Perhaps Microsoft can just fix their shit. This has been open since (early) 2020 ffs

lxg208 commented 1 year ago

wsl --shutdown makes my system back, but this is not really a solution. All unsaved work are lost and very inconvenient. I really wish Microsoft can solve this issue.

Hiieu commented 7 months ago

Not sure why, but after 6 months of constantly looking for a solution. This command fixed the issue (see: link):

sudo rm -rf /etc/acpi/events
chanpreetdhanjal commented 6 months ago

Hi. Can you please collect networking logs by following the instructions below? https://github.com/microsoft/WSL/blob/master/CONTRIBUTING.md#collect-wsl-logs-for-networking-issues