Skip to content

fix: count the SDK clock on CLOCK_MONOTONIC_RAW on Linux - #415

Merged
chrisuthe merged 1 commit into
mainfrom
fix/use-raw-monotonic-clock-on-linux
Oct 16, 2026
Merged

chrisuthe merged 1 commit into
mainfrom
fix/use-raw-monotonic-clock-on-linux

Conversation

@chrisuthe

Copy link
Copy Markdown
Member

What

HighPrecisionTimer now counts elapsed time on clock_gettime(CLOCK_MONOTONIC_RAW) on Linux. Windows and macOS keep Stopwatch, which already reads an unslewed clock there (QueryPerformanceCounter and CLOCK_UPTIME_RAW).

Why

On Linux, Stopwatch reads CLOCK_MONOTONIC, which NTP slews. aiosendspin timestamps on CLOCK_MONOTONIC_RAW (aiosendspin#211, "to avoid NTP slewing"), and so do sendspin-rs and SendspinKit. So whenever the player host's time daemon is correcting, the offset the clock synchroniser measures moves at the slew rate. That rate is tens of ppm, it keeps changing, and an initial sync can move the clock several milliseconds within seconds. The Kalman filter models drift as near-constant, so it lags behind. The mapping error grows past the sync threshold, and playback hard-corrects audibly.

Measured on a Home Assistant OS box, with the server and a player on the same host (sendspin-cpp, which reads the same slewed clock):

  • CLOCK_MONOTONIC ran +24.5 → +11.3 ppm against CLOCK_MONOTONIC_RAW while systemd-timesyncd was correcting. The player's measured server offset tracked that exactly, burst by burst.
  • systemd-timesyncd's "Initial clock synchronization" moved the two clocks 11.5 ms apart within one 10 s time-sync burst, and the player lost sync 6 s later.

The .NET runtime reads CLOCK_MONOTONIC for Stopwatch on Linux (minipal_hires_ticks), so the SDK is exposed in the same way.

How

  • HighPrecisionTimer picks its counter once per process: on Linux, the raw clock if it can be read, otherwise Stopwatch. The UTC anchor, MonotonicTimer and every consumer are unchanged. Only the elapsed part changes clock.
  • The binding is a blittable [DllImport("libc", EntryPoint = "clock_gettime")]. Both runtime loaders map "libc" to the platform C library (CoreCLR's PAL FixLibCName, and System.Native's pal_dynamicload.c for NativeAOT), so it resolves on glibc and musl.
  • Players that supply IAudioPlayer.GetAudioClockMicroseconds() are unaffected: that value replaces the timer for local time.

Testing

  • New HighPrecisionTimerTests: the raw clock is used on Linux and only there, elapsed time tracks real time (pins the tick-to-µs conversion), and the timer reports Unix-epoch microseconds and never decreases.
  • Full suite on Windows, net8.0 and net10.0: 1631 passed.
  • Ubuntu (glibc 2.39) in dotnet/sdk:10.0: the full net10.0 suite matches main. Both have the same 4 failures, which are file-permission tests that cannot fail as root in a container and are unrelated to this change.
  • Alpine (musl) in dotnet/sdk:10.0-alpine: the synchronization tests pass (39/39), and the raw clock is in use.
  • NativeAOT linux-x64 publish: no IL or AOT warnings. The smoke test passes, including a new check that reads the shared clock.

Stopwatch reads CLOCK_MONOTONIC on Linux, which NTP slews. aiosendspin
timestamps on CLOCK_MONOTONIC_RAW (aiosendspin#211), so while the host's
time daemon is correcting, the offset the clock synchroniser measures
moves at the slew rate: tens of ppm that keep changing, and several
milliseconds within seconds when the daemon does an initial sync. The
Kalman filter models drift as near-constant, so it lags that, the error
grows past the sync threshold, and playback hard-corrects audibly. On one
Home Assistant OS host, systemd-timesyncd's initial sync moved the two
clocks 11.5 ms apart in a single 10 s time-sync burst.

HighPrecisionTimer now counts elapsed time on clock_gettime(
CLOCK_MONOTONIC_RAW) on Linux, through a blittable libc P/Invoke that the
runtime resolves on glibc and musl under JIT and NativeAOT. It falls back
to Stopwatch if the raw clock cannot be read. Windows (QPC) and macOS
(CLOCK_UPTIME_RAW) already count on an unslewed clock and are unchanged,
as are the UTC anchor and every consumer of the timer.

The AOT smoke test now reads the shared clock, so a published binary
binds the P/Invoke the way a consumer's does.
@chrisuthe
chrisuthe merged commit 3e594cf into main Oct 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant