[Bug]: TimeSyncer steps the guest clock backward every 30 s, and the first sync steps it by up to ±0.5 s
维护者通常 2 天内回复
@castlemilk 已经在做这个了。
开始于 2026年11月16日。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 46/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Swift
- 星标
- 9k
- 派生
- 412
- 平均合并
- 3 天 10 小时
- 30 天内合并 PR
- 12
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apple/containerization 的其他 Issue
-
[Bug]: RegistryClient ignores credentials in HTTPS_PROXY, so image pulls fail with proxyAuthenticationRequired可能已有人在做 @vishalhabib99 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
apple/containerization#1012 ·
维护者通常 2 天内回复
-
[Bug]: archive ownership changes clear set-ID permission bits可能已有人在做 @maksim-romanov 于 100 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apple/containerization#819 · 1 条评论 · 1 个 reaction ·
维护者通常 2 天内回复
-
[Bug]: archive extraction strips special permission bits可能已有人在做 @maksim-romanov 于 100 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apple/containerization#818 · 2 条评论 ·
维护者通常 2 天内回复
-
LinuxContainer.create() leaks the VZ VM/XPC process when vm.start() throws — no do/catch around it可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apple/containerization#804 · 1 条评论 ·
维护者通常 2 天内回复
-
难度 3/5 1-2 天 新手友好度 65/100
apple/containerization#1017 ·
维护者通常 2 天内回复
查看 apple/containerization 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
fxios-coreexperience
难度 2/5 1-3 小时 新手友好度 68/100
mozilla-mobile/firefox-ios#36661 ·
维护者通常 1 天内回复
-
needs-triage
难度 2/5 1-3 小时 新手友好度 68/100
manaflow-ai/cmux#45713 · 1 条评论 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 67/100
alejandrobujan/tendedero#151 ·
维护者通常 2 天内回复