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] Network connections are not restored after returning from Windows sleep mode #4992

Open
choung opened this issue Mar 19, 2020 · 50 comments
Labels

Comments

@choung
Copy link

choung commented Mar 19, 2020

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
Copy link

thoni56 commented Jun 9, 2020

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
Copy link

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
Copy link

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
Copy link

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

@Spaak
Copy link

Spaak commented Aug 26, 2020

@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
Copy link

@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
Copy link

mittalsuraj18 commented Aug 28, 2020

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
Copy link

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
Copy link

xinntao commented Aug 31, 2020

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
Copy link

Did anyone manged to resolve this issue?

@Soberia
Copy link

Soberia commented Sep 3, 2020

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
Copy link

fkguo commented Sep 5, 2020

I have the same issue, insider OS build 20206.1000

@tbronchain
Copy link

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
Copy link

llacom commented Sep 8, 2020

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
Copy link

Soberia commented Sep 9, 2020

Temporal fix without system reboot

@david50407
Copy link

@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
Copy link

Still going on here as well.

@gradinarot
Copy link

For me is gone after updating to Windows Insider 20236

@wzin
Copy link

wzin commented Nov 3, 2020

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

@4integration
Copy link

4integration commented Nov 30, 2020

I have the same issue - very annoying.

  • Microsoft Windows [Version 10.0.18363.1198]
  • WSL2 with Ubuntu 20.04
  • Docker Desktop with WSL2 backend
  • Pulse Secure VPN client
  • Hyper-V isn't installed

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

@FutureGadget
Copy link

FutureGadget commented Dec 20, 2020

I have the same issue.

  • MS Windows 10 Education build 19042.685
  • WSL2 with Ubuntu 20.04
  • Docker Desktop with WSL2 backend
  • Hyper-V not installed.

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
Copy link

ati46 commented Dec 25, 2020

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
Copy link

helinwang commented Dec 30, 2020

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

@gradinarot
Copy link

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
Copy link

@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
Copy link

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

@smzht
Copy link

smzht commented Jan 21, 2021

@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
  1. Start OpenSSH Authentication Agent service on Windows.

  2. Execute the following on Windows.

> ssh-keygen.exe -t ed25519
> ssh-add.exe %USERPROFILE%\.ssh\id_ed25519
  1. 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
  1. 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
  1. Add the following to .bashrc.
export DISPLAY=:20
  1. Restart WSL2.

@bertlebee
Copy link

@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
Copy link

smzht commented Jan 21, 2021

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

@bertlebee
Copy link

@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
Copy link

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
Copy link

spicydonkey commented Jan 31, 2021

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

@vladkosarev
Copy link

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

@fberlakovich
Copy link

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
Copy link

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
Copy link

zwwi commented Feb 27, 2021

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
Copy link

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

@thoni56
Copy link

thoni56 commented Apr 27, 2021

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
Copy link

ast0815 commented Apr 27, 2021

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

@acceleratxr
Copy link

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
Copy link

Temporal fix without system reboot

Restarting LxssManager service on Windows terminated WSL distro

@Noexpert Noexpert mentioned this issue Jan 19, 2022
@md8n
Copy link

md8n commented Aug 3, 2022

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
Copy link

wzin commented Oct 12, 2022

This has been resolved since few windows versions ago.

@PrimeDominus
Copy link

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

@scruel
Copy link

scruel commented May 10, 2023

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

@bertlebee
Copy link

bertlebee commented May 10, 2023

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
Copy link

lxg208 commented Jun 11, 2023

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
Copy link

Hiieu commented Mar 31, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests