Skip to content

A utility to automatically correct clock drift in WSL 2 using Windows Events to detect points where clock drift may have occurred

License

Notifications You must be signed in to change notification settings

stuartleeks/wsl-clock

Repository files navigation

wsl-clock

IMPORTANT: This is not an official solution and whilst it worked on my machine this is an unsupported workaround :-)

There is an issue with WSL where the clock in WSL isn't updated when the host resumes from sleep/hibernate. E.g. this issue

This repo has a workaround that creates a scheduled task that is triggered by Windows Events for resuming from sleep/hibernation. When the scheduled task executes it resets the clock in the WSL VM.

For the background to this repo, see this blog post. The implementation discussed in that blog post is based on PowerShell and that implementation is still available here. The implementation has now been replaced with a program written in Go, with the following advantages:

  • The application runs as a windowless app so there is no longer the flash of a powershell window when the task runs
  • The logic has been updated to test for a running WSL v2 distro before checking whether to reset the clock (rather than any running distro). This removes the potential for spinning up a WSL 2 distro when there wasn't one running (i.e. when there was no need to reset the clock)
  • The logic now uses an existing running distro for executing the time checks and reset steps, rather than the default distro. This avoids situations where an extra distro may have been started, as well as avoiding issues when the default distro was configured as WSL v1

Setup

Download the ZIP file for a prebuilt release and unzip to a local folder.

After unzipping, unblock each to the .ps1 and .exe files by right-clicking, selecting 'Properties', ticking 'Unblock' and clicking 'OK':

To set up the scheduled task, run add-wslclocktask.ps1 in the content you just unzipped. This will set up a scheduled task triggered on Hibernation Resume events to run the wsl-clock.exe to check for clock drift on resuming from hibernation.

Cleanup

To remove the scheduled task, run remove-wslclocktask.ps1

Logs/Troubleshooting

The program invoked by the scheduled task logs output to ~/.wsl-clock.log

Building from source

The simplest way to build from source is to use Visual Studio Code and open as a devcontainer. This will run the development environment with the required version of Go and allow you to run make build to build the binary.

About

A utility to automatically correct clock drift in WSL 2 using Windows Events to detect points where clock drift may have occurred

Topics

Resources

License

Stars

Watchers

Forks