#22064·Avalonia

Wayland: every DispatcherTimer fires late by a constant startup offset (Dispatcher._timeProvider keeps the pre-InitializeUIThreadDispatcher clock)

Author: electro-logicCreated Aug 25, 2026Updated Sep 28, 2026

Avalonia version: 12.1.1 (also present on current master, see code references below) OS: Linux, GNOME on Wayland, .NET 10 — Wayland backend on its default dispatcher path (ManagedDispatcherImpl, not UseGLibMainLoop)

Symptom

Every DispatcherTimer in the process — any priority — fires late by a constant per-launch offset (measured 137–261 ms across launches, rock-stable within one launch). Because the timer re-arms after each late fire, the effective period becomes offset + interval: a 10 ms timer beats at ~150–270 ms, a 125 ms UI tick at ~260–390 ms. An application whose UI is purely timer-driven visibly runs at 4–7 Hz.

Measured with a 10 ms DispatcherTimer recording actual inter-tick intervals (identical results for Background and Normal priority):

scenario 10 ms timer actually fires every
Wayland backend, idle window 137–261 ms (per-launch constant)
Wayland backend, while pointer events flow ~16 ms
X11 backend, same code 10.0 ms
Wayland + workaround below 10.0 ms

Root cause

Dispatcher captures its clock once, in the constructor, into a readonly field (Dispatcher.cs — _timeProvider assignment):

  • impl == null → _timeProvider = () => st.ElapsedMilliseconds (a Stopwatch started at dispatcher construction),
  • impl != null → _timeProvider = () => impl.Now.

The Wayland backend installs its impl after construction, via Dispatcher.InitializeUIThreadDispatcher(new ManagedDispatcherImpl(null)) (WaylandPlatform.Initialize). InitializeUIThreadDispatcher first touches Dispatcher.UIThread (UIThread.VerifyAccess()), which lazily constructs the dispatcher through the impl == null branch, then calls ReplaceImplementation(impl) — which swaps _impl and rewires events but cannot touch the readonly _timeProvider. So on this init path the mismatch is structural, not a race:

  • timer due-times (DispatcherTimer.DueTimeInMs = _dispatcher.Now + interval) are computed on the dispatcher's stopwatch, started early in process setup;
  • ManagedDispatcherImpl.RunLoop compares the armed due-time against its own _clock (_nextTimer < _clock.Elapsed, and timeout = nextTimer - _clock.Elapsed for the wait), a Stopwatch started only when the platform finished connecting to the compositor.

Every armed wait therefore oversleeps by the difference of the two stopwatch epochs — the 137–261 ms above is simply how long Wayland platform init took in that particular launch.

Why it usually goes unnoticed: Dispatcher.ExecuteJob runs PromoteTimers() after every executed job, and that comparison uses Dispatcher.Now on both sides — consistent. So any cross-thread Post (input, render, app traffic) both wakes the loop and promotes due timers accurately, masking the defect. It surfaces only when the UI is driven by timers alone — which also explains the ~16 ms row above (each pointer event executes a job and rescues the pending timers).

Follow-up source inspection and measurements show that X11 and Win32 can also lazily construct the dispatcher without the implementation. Normal X11 startup produces only a small negative skew and therefore appears cadence-correct, but an early dispatcher touch produces the same large positive offset. The follow-up comment below has the full Linux matrix.

Repro

csharp
public override void OnFrameworkInitializationCompleted()
{
    var sw = Stopwatch.StartNew();
    double last = 0;
    new DispatcherTimer(TimeSpan.FromMilliseconds(10), DispatcherPriority.Normal, (_, _) =>
    {
        double now = sw.Elapsed.TotalMilliseconds;
        if (last > 0) Console.WriteLine($"{now - last:F1} ms");
        last = now;
    }).Start();
    // show any window; do not touch it. Expected ~10 ms; observed 137-261 ms on Wayland.
    base.OnFrameworkInitializationCompleted();
}

Suggested fix

Make the clock follow the implementation: either refresh the time provider in ReplaceImplementation (e.g. make Now read through _impl, falling back to the private stopwatch only while _impl is a default ManagedDispatcherImpl), or construct the UI-thread dispatcher with the impl inside InitializeUIThreadDispatcher instead of replacing it afterwards.

Workaround

Re-pointing the private _timeProvider at the installed impl's Now via reflection at startup fixes timer cadence completely (10.0 ms measured); this remains a production workaround until the defect is fixed upstream.