[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時間
- マージ済み PR(30日)
- 12
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 件 ·
メンテナーはふだん 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対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 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 日以内に返信
-
Save the clothespinsオープンenhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
alejandrobujan/tendedero#151 ·
メンテナーはふだん 2 日以内に返信