[Bug]: TimeSyncer steps the guest clock backward every 30 s, and the first sync steps it by up to ±0.5 s
I maintainer di solito rispondono entro 2 giorni
@castlemilk ci sta già lavorando.
Dal 16/11/2026.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 46/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- backend, operating-systems
Direzione di ricerca
Start with Sources/Containerization/TimeSyncer.swift and vminitd/Sources/VminitdCore/Server+GRPC.swift, then trace the host time sample through the setTime RPC to the guest clock update. The report describes a PLL slew for small offsets and a clock step for larger ones, with an optional delayed sample check. Done means syncs avoid backward clock steps for small offsets and correct the boot offset before workloads read the clock.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
I have done the following
- I have searched the existing issues
- If possible, I've reproduced the issue using the 'main' branch of this project
I did not build main. Sources/Containerization/TimeSyncer.swift and setTime in vminitd/Sources/VminitdCore/Server+GRPC.swift are unchanged on main at 706cb512c.
Steps to reproduce
- Save this probe as
probe.py. It readsCLOCK_REALTIMEbetween twoCLOCK_MONOTONICreads. A change of more than 0.2 ms inrealtime - monotonicis a clock step. A slew moves both clocks together, so the probe does not count it as a step.
import os, signal, time, sys
N = int(sys.argv[1]) if len(sys.argv) > 1 else 16
DUR = int(sys.argv[2]) if len(sys.argv) > 2 else 240
kids = []
for _ in range(N):
pid = os.fork()
if pid == 0:
while True:
pass
kids.append(pid)
R, M, RAW = time.CLOCK_REALTIME, time.CLOCK_MONOTONIC, time.CLOCK_MONOTONIC_RAW
g = time.clock_gettime_ns
def sample():
while True:
m1 = g(M); r = g(R); m2 = g(M)
if m2 - m1 <= 50_000:
return r, r - (m1 + m2) // 2
prev_r, prev_off = sample()
rr0 = g(R) - g(RAW)
steps, backs, worst_back = [], 0, 0
end = time.monotonic() + DUR
next_report = time.monotonic() + 15
while time.monotonic() < end:
r, off = sample()
if r < prev_r:
backs += 1; worst_back = max(worst_back, prev_r - r)
d = off - prev_off
if abs(d) > 200_000:
steps.append(d)
print(time.strftime('%H:%M:%S', time.gmtime(r / 1e9)), f"step {d/1e6:+.3f} ms", flush=True)
if time.monotonic() >= next_report:
print(time.strftime('%H:%M:%S', time.gmtime(r / 1e9)), f"realtime-raw {(g(R) - g(RAW) - rr0)/1e6:+.3f} ms", flush=True)
next_report += 15
prev_r, prev_off = r, off
time.sleep(0.001)
for pid in kids:
os.kill(pid, signal.SIGKILL)
neg = [s for s in steps if s < 0]
print(f"done: load={N} steps={len(steps)} backward_steps={len(neg)} worst_backward={min(neg)/1e6 if neg else 0:.3f} ms realtime_went_back={backs} worst={worst_back/1e6:.3f} ms")
- Run the probe in a VM with 16 busy processes on 2 vCPUs for 240 s:
container run --rm --cpus 2 --memory 256m -v "$PWD":/probe:ro \
docker.io/library/python:3.13-alpine python3 -u /probe/probe.py 16 240
- Run it again with no load. Change
16to0.
Current behavior
The host TimeSyncer sleeps 30 s, reads the host time with gettimeofday, and calls setTime on the guest agent. vminitd calls settimeofday with that value. musl implements settimeofday as clock_settime(CLOCK_REALTIME). I confirmed the syscall with a seccomp user-notification filter in the guest.
This path has no latency compensation, no threshold and no slew. Each sync steps the guest clock to the host time, which is already old when the guest applies it.
- The first sync steps the clock by up to ±0.5 s. The guest boots with a time from a 1-second RTC. The first sync comes about 32 s after boot and removes the fraction with one step. Four boots stepped +259, +284, −311 and −366 ms. About half the boots step backward.
- Later syncs step the clock backward under host load. The RPC delay makes the host time stale. With 16 busy processes on 2 vCPUs, one 240 s run had 6 steps. The worst step was −19.6 ms, and one
CLOCK_REALTIMEread went backward by 18.5 ms. With no load, the steps were −0.3 ms to −2.6 ms.
A workload that uses wall-clock time for order sees these steps. Examples are a database in the guest that orders rows by clock_timestamp(), and a test that expects a later event to have a later timestamp. In our CI, five such tests failed only in container VMs.
#698 makes the sync interval configurable and changes the default to 20 s. It does not change how the guest applies the time. #490 kept the host as the time source. This report agrees with that design. The request is only about how each sync changes the guest clock.
Expected behavior
- A sync with a small offset does not move
CLOCK_REALTIMEbackward. - The guest corrects the boot offset before the container process starts, or it slews the offset.
Suggested changes:
-
Sync once in
TimeSyncer.start, before the first sleep. The first sync then corrects the boot offset before the workload reads the clock. -
In vminitd
setTime, computeoffset = host time - guest time. If the offset is within ±0.5 s, give it to the kernel PLL. Step the clock only for a larger offset.struct timex tx = { .modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST | ADJ_NANO, .offset = offset_ns, .status = STA_PLL | STA_FREQHOLD | STA_UNSYNC, .constant = 2, }; adjtimex(&tx);With time constant 2 and
ADJ_NANO, the kernel removes 1/16 of the remaining offset each second. A 0.5 s offset changes the clock rate by 3.1% at most, and less than 2 ms of it remains after 90 s. The kernel limits a PLL offset to 0.5 s (MAXPHASE).STA_FREQHOLDkeeps the frequency fixed, so the PLL corrects the phase only. -
Optional: subtract half the RPC round trip, or skip one sample that is far behind the expected offset.
I tested change 2 and the sample skip with a wrapper around an unmodified vminitd 0.47.0. The wrapper catches the clock_settime call from vminitd with a seccomp listener. It slews offsets from −2 s to +0.5 s through the PLL and steps larger offsets. It skips one sample that is more than 20 ms behind the expected offset. Two VMs ran the same probe side by side, with 16 busy processes on 2 vCPUs for 240 s:
| Measure | vminit 0.47.0 | With slew |
|---|---|---|
| Steps over 0.2 ms | 6 | 0 |
| Worst backward step | −19.6 ms | none |
Backward CLOCK_REALTIME reads |
1 (18.5 ms) | 0 |
| First sync after boot | +259 ms step | −366 ms, slewed in about 90 s |
| Delayed sample | −19.6 ms step, then +17.0 ms step | one −115 ms sample skipped |
Environment
- OS: macOS 26.5.1 (25F80)
- Xcode: Version 26.6 (17F113)
- Swift: Apple Swift version 6.3.3
- container: 1.5.0 (d265d66)
- vminit: ghcr.io/apple/containerization/vminit:0.47.0
- Kernel: vmlinux-6.18.35-197-debug from kata-static 3.32.0 (arm64)
Relevant log output
# probe.py 16 240, vminit 0.47.0
23:16:27 realtime-raw -0.000 ms
23:16:42 realtime-raw -0.000 ms
23:16:44 step +258.964 ms
23:16:58 realtime-raw +258.963 ms
23:17:13 realtime-raw +258.963 ms
23:17:16 step +0.263 ms
23:17:28 realtime-raw +259.226 ms
23:17:43 realtime-raw +259.226 ms
23:17:58 realtime-raw +259.075 ms
23:18:13 realtime-raw +259.075 ms
23:18:20 step -0.443 ms
23:18:28 realtime-raw +258.631 ms
23:18:43 realtime-raw +258.631 ms
23:18:52 step -19.597 ms
23:18:58 realtime-raw +239.034 ms
23:19:13 realtime-raw +239.034 ms
23:19:24 step +16.991 ms
23:19:28 realtime-raw +256.024 ms
23:19:43 realtime-raw +256.024 ms
23:19:55 step +1.118 ms
23:19:58 realtime-raw +257.142 ms
done: load=16 steps=6 backward_steps=2 worst_backward=-19.597 ms realtime_went_back=1 worst=18.530 ms
Code of Conduct
- I agree to follow this project's Code of Conduct
- Lingua principale
- Swift
- Stelle
- 9k
- Fork
- 412
- Merge medio
- 3g 10h
- PR unite (30g)
- 12
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di apple/containerization
-
[Bug]: RegistryClient ignores credentials in HTTPS_PROXY, so image pulls fail with proxyAuthenticationRequiredForse già presa @vishalhabib99 l’ha presa 4 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
apple/containerization#1012 ·
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: archive ownership changes clear set-ID permission bitsForse già presa @maksim-romanov l’ha presa 100 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
apple/containerization#819 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: archive extraction strips special permission bitsForse già presa @maksim-romanov l’ha presa 100 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
apple/containerization#818 · 2 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
LinuxContainer.create() leaks the VZ VM/XPC process when vm.start() throws — no do/catch around itForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
apple/containerization#804 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
apple/containerization#1017 ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di apple/containerization
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Switch to new string APIApertafxios-coreexperience
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
mozilla-mobile/firefox-ios#36661 ·
I maintainer di solito rispondono entro 1 giorno
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
manaflow-ai/cmux#45713 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Save the clothespinsApertaenhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
alejandrobujan/tendedero#151 ·
I maintainer di solito rispondono entro 2 giorni