[Bug]: TimeSyncer steps the guest clock backward every 30 s, and the first sync steps it by up to ±0.5 s

Đang mở
#1,019 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@castlemilk đang làm issue này rồi.

Từ ngày 16/11/2026.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
46/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, linux, swift
Lĩnh vực
backend, operating-systems

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
  1. Save this probe as probe.py. It reads CLOCK_REALTIME between two CLOCK_MONOTONIC reads. A change of more than 0.2 ms in realtime - monotonic is 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")
  1. 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
  1. Run it again with no load. Change 16 to 0.
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.

  1. 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.
  2. 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_REALTIME read 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_REALTIME backward.
  • The guest corrects the boot offset before the container process starts, or it slews the offset.

Suggested changes:

  1. Sync once in TimeSyncer.start, before the first sleep. The first sync then corrects the boot offset before the workload reads the clock.

  2. In vminitd setTime, compute offset = 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_FREQHOLD keeps the frequency fixed, so the PLL corrects the phase only.

  3. 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
Ngôn ngữ chính
Swift
Star
9k
Fork
412
Merge trung bình
3 ngày 10 giờ
Pull request đã merge (30 ngày)
12

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apple/containerization

Tất cả issue của apple/containerization

Issue tương tự

Thêm issue về Swift

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.