Fix Cloud Hypervisor TSC synchronization causing clock skew in VMs
Author: skybrian2 Channel: #feedback Link: https://discord.com/channels/1405685085923049482/1405686161791516873/1530767831233204245
Issue
Playwright tests are failing randomly on skybrian-links VM due to Chromium's network process crashing. Root cause analysis using Opus 5 determined that clock_gettime(CLOCK_MONOTONIC, &ts) is returning non-monotonic values.
Root Cause
The VM is using the TSC (Time Stamp Counter) as its time source. Cloud Hypervisor advertised CLOCKSOURCE_STABLE_BIT and invariant TSC, but did not actually synchronize the guest TSCs across vCPUs. When processes switch vCPUs under load, the clock jumps backwards by up to ~1.197 ms.
The kernel has a check to detect unstable TSC, but it appears to be disabled.
Workaround
Switching to kvm-clock resolves the issue, but only until the next reboot.
Additional Details
- Affected VM:
skybrian-linksinpdx - VM uptime: 132 days at time of report
- The clock skew may have developed over time as TSCs drifted apart
- A minidump from the Chromium crash was obtained
Context
Confirmed by @josharian as a Cloud Hypervisor issue that should be fixed on our end.
2 Comments
FWIW, an LLM suggestion as a workaround in the interim:
If you try this out, please let me know how it goes.
cc @skybrian
Thanks, I'm good at this point. BTW, here's a test written in TypeScript (for Deno, but probably easy to port) that will fail if clock skew shows up again:
https://gist.github.com/skybrian/221ffb8eef2ffc4100771bb0a7ad2816