WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL2 date incorrect after waking from sleep

Open garyo opened this issue 6 years ago • 260 comments

  • Your Windows build number (run ver at a Windows Command Prompt), your distribution (f.e. on Debian/Ubuntu run lsb_release -r), and whether the issue manifests on WSL 2 and/or WSL 1 (cat /proc/version):

Windows: version 2004, build 19041.264 Distro: Ubuntu 18.04 Issue manifests only on WSL2 (Linux version 4.19.84-microsoft-standard)

  • What you're doing and what's happening. Copy&paste the full set of specific command-line steps necessary to reproduce the behavior, and their output. Include screenshots if that helps demonstrate the problem:

I upgraded an Ubuntu 18.04 WSL1 instance to WSL2. Ever since then, the date does not advance when the machine is asleep or hibernating. To repro:

  • Install Ubuntu 18.04
  • Check that Windows system date is correct
  • Check that date is correct (use date command in WSL2)
  • Put laptop to sleep for a while
  • Resume from sleep
  • Check Windows date: correct
  • Check WSL2 date: wrong, still where it was when machine went to sleep
  • Wait a while to see if it auto-corrects
  • Notice that it doesn't

What's wrong / what should be happening instead:

  • Date does not update after sleep. It should be correct after resume, as it was in WSL1.

Note that there was a previous issue on this topic, #4245 which is now closed, I'm not sure why; it was recommended there by @therealkenc that I open a new issue.

There are temporary workarounds; if I use sudo hwclock -s or shut down the WSL2 VM with wsl --shutdown and restart, the time is correct, until the next sleep/hibernate.

As noted in various other issues on this topic (#5184, #4975, #4677 etc.), the time being incorrect is more than a minor inconvenience. It can cause trouble with apt install, validity of SSL certs, build problems, and many other issues.

garyo avatar Jun 03 '20 12:06 garyo

@garyo - Thanks for opening a new issue. We made a previous change to send clock sync packets when the system resumes from sleep, but it looks like we are not receiving those notifications in some cases. I have an ongoing conversation with the Windows Power team, it looks like they are not correctly delivering the notifications upon system resume in some cases.

benhillis avatar Jun 03 '20 17:06 benhillis

@garyo @benhillis I just tried this again on my laptop (original Surface Book) and the date/time seems to be keeping up-to-date after wake up from sleep in my setup now.

Windows: version 2004, build 19041.329 Distro: Ubuntu 20.04 Linux kernel version: 4.19.104-microsoft-standard

I put my laptop to sleep for about a minute or so, woke it back up and checked the date/time again in WSL and it seems to be updated now. Probably need to try on a longer sleep timeframe just to double-check though. I'll try this again later and report back when I have a longer period to put it to sleep.

dwaynebradley avatar Jun 11 '20 19:06 dwaynebradley

Follow up on my previous comment...

I put my laptop to sleep last night. When I opened it up this morning, the first thing I did was to check the time in WSL (Ubuntu 20.04). Low and behold...it was correct!!! :-)

For my setup at least, this appears to be fixed now.

dwaynebradley avatar Jun 12 '20 13:06 dwaynebradley

@dwaynebradley are you saying it's fixed by something Microsoft did, or did you install one of the workarounds such as https://github.com/microsoft/WSL/issues/4245#issuecomment-615327939?

garyo avatar Jun 12 '20 15:06 garyo

@garyo fixed by Microsoft apparently. No workarounds on my end.

dwaynebradley avatar Jun 12 '20 15:06 dwaynebradley

Well, I just tried it again today after leaving my laptop in sleep overnight and the date is way off again. Apparently it isn't fixed after all...

dwaynebradley avatar Jun 15 '20 13:06 dwaynebradley

Second this. Not fixed. Windows 10/2004 build 19041.329

MauriceS-gti avatar Jun 19 '20 17:06 MauriceS-gti

I'm assuming this is the same problem, but neither wsl --shutdown nor hwclock fixes it for me, so I'm not completely sure. Whenever I try to build anything (configured with cmake, built with make), I get the clock skew warning, albeit with very small times (usually below 2 seconds and quite often below 1). Interestingly it does not appear that any builds have actually failed due to this yet, things seem to be complete even after the warning appeared. I also haven't had any issues with apt yet that relate to this.

Windows: Version 2004, build 19041.329 Distro: Ubuntu 20.04 Linux Kernel: 4.19.104-microsoft-standard

Balothar12 avatar Jun 20 '20 09:06 Balothar12

@Balothar12 Are your projects on /mnt/* folders? See #4975

onomatopellan avatar Jun 20 '20 10:06 onomatopellan

@onomatopellan Indeed they are. Didn't realize that at first since I symlinked the respective directory from my home directory and kinda forgot abut that, but yeah, the files are technically in the mounted folders. And as mentioned in #4975 the issue does not occur when the same files are actually on the Linux filesystem.

Balothar12 avatar Jun 20 '20 11:06 Balothar12

Everyone's saying that with WSL2 you want to move your project folders into the Linux side -- opposite of WSL1. Filesystem performance accessing Windows files from WSL2 is abysmal. (But this is getting off topic.)

garyo avatar Jun 20 '20 12:06 garyo

I'm not sure if I have the same issue but I have the same problem...

My workstation NEVER sleeps, but still, somehow my WSL2 Ubuntu host became 20 seconds behind...

image

sudo hwclock -s didn't do anything, but wsl --shutdown did cause the clocks to re-sync.

Update: Checked the time again, and it's drifted once again. It's now 2 seconds behind. No reboots or sleep since the screenshot above

MysticRyuujin avatar Jul 18 '20 21:07 MysticRyuujin

@Balothar12 and @onomatopellan I found the same issue with files on /mnt/* and using make. sudo hwclock -s does not help. After executing this command I still get warnings like make[2]: Warning: File 'CMakeFiles/yaml-cpp.dir/depend.make' has modification time 0.6 s in the future and make[2]: warning: Clock skew detected. Your build may be incomplete.

The warnings are not in vain. Later I found that in fact the library I had build was incomplete and I got issues using it when trying to link it to my own code. When I moved all to the Linux side and rebuild the library it was complete. I also find that i/o of files located in /mnt/* is very slow. Probably this clock warning due to sub-second lags and slow /mnt/* file handling are related.

eajfpeters avatar Jul 23 '20 21:07 eajfpeters

@garyo - Thanks for opening a new issue. We made a previous change to send clock sync packets when the system resumes from sleep, but it looks like we are not receiving those notifications in some cases. I have an ongoing conversation with the Windows Power team, it looks like they are not correctly delivering the notifications upon system resume in some cases.

Hi @benhillis , I'd like to remind you of another scenario about clock sync packet and Windows Power state. That is NESTED VIRTUALIZATION. I got a physical computer running Hyper-V. I created a virtual machine with nested virtualization enabled and I installed Windows 10 2004 on that virtual machine. Inside Windows 10, I enabled Hyper-V and enabled WSL2. Now my Ubuntu 20.04 is 50 minutes behind my Windows 10 clock. This is possibly because the virtual machine got paused when I restarted my host machine. I guess in this scenario, Windows 10 would never have received any event notification because there's nothing a guest OS needed to do. Thus Windows 10 would never have notified WSL2 kernel of something that's "never happened".

wsy avatar Jul 28 '20 07:07 wsy

@Balothar12 and @onomatopellan I found the same issue with files on /mnt/* and using make. sudo hwclock -s does not help. After executing this command I still get warnings like make[2]: Warning: File 'CMakeFiles/yaml-cpp.dir/depend.make' has modification time 0.6 s in the future and make[2]: warning: Clock skew detected. Your build may be incomplete.

The warnings are not in vain. Later I found that in fact the library I had build was incomplete and I got issues using it when trying to link it to my own code. When I moved all to the Linux side and rebuild the library it was complete. I also find that i/o of files located in /mnt/* is very slow. Probably this clock warning due to sub-second lags and slow /mnt/* file handling are related.

Yes, you are totally right the error only happens inside /mnt/* folder, I tried with hwclock and shutdown etc.. but none works that is the reason why the difference is only by seconds or milliseconds. Thanks.

realtica avatar Jul 29 '20 01:07 realtica

Seeing the same issue here

Microsoft Windows [Version 10.0.20197.1000]
% lsb_release -r
Release:        9.5
at /proc/version
Linux version 4.19.121-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Fri Jun 19 21:06:10 UTC 2020

Surface laptop usually sleeps, I have seen the Linux clock more than a day behind the windows clock

relston avatar Aug 27 '20 08:08 relston

I posted this on another issue, but adding here in case it helps anyone.

I created a workaround for this issue using Windows Events to trigger the clock sync via hwclock on resume from sleep. Originally a PowerShell script, I've recently updated it to a go binary (partly because that avoids the flash of a PowerShell window). Since implementing this workaround, I've not had any issues: https://github.com/stuartleeks/wsl-clock

stuartleeks avatar Sep 02 '20 12:09 stuartleeks

Still present in W10 2004 (Build 19041.508). This was a fun rabbit-hole to dive down.

nate2014jatc avatar Sep 15 '20 05:09 nate2014jatc

~~I like the PowerShell/Go workaround above, but I wanted something running natively in the WSL2 VM. This is what I cooked up. It polls the hardware clock every minute, and if the clock has drifted more than 5 seconds, it syncs the VM to the hardware clock: https://github.com/muchtall/muchtall-scripts/blob/master/wsl2-time-sync.txt~~

~~Not as quick as triggering on wake from hibernate, but it's close enough for my needs.~~

EDIT: Nevermind. I got into work today, and noticed that it's not working. Not sure why it worked when I was developing the solution, but it's not working for me now. 🤷‍♂️

muchtall avatar Sep 17 '20 19:09 muchtall

Can confirm fully patched 2004 still has this issue. This is particularly annoying if you're using Docker with WSL2 backend, as this drift is inherited by all containers.

oising avatar Sep 30 '20 14:09 oising