Skip to content

emscripten_get_now() is not monotonic in Safari with pthreads: performance.timeOrigin changes between reads #28271

Closed

Description

@gribbet

Affects Emscripten 3.1.28 and later, any build with -pthread, running in Safari.

With PTHREADS, emscripten_get_now() (and therefore clock_gettime(CLOCK_MONOTONIC), std::chrono::steady_clock, usleep/nanosleep timing, etc.) is implemented as:

emscripten_get_now: () => performance.timeOrigin + performance.now(),

In Safari (WebKit), performance.timeOrigin is recomputed on every read and changes between reads in cross-origin isolated contexts, which pthreads require. As a result, emscripten_get_now() goes backwards by up to ~0.2 ms, many thousands of times per second. Code that assumes a monotonic clock breaks.

WebKit bug: https://bugs.webkit.org/show_bug.cgi?id=331772

This was introduced by #18267 (3.1.28). Possibly related: #18677 (Safari-only periodic glitch in a pthreads app, bisected to 3.1.27 -> 3.1.28).

Proposed fix: Read performance.timeOrigin once per thread. Per HR-Time it is constant for a global, so this matches the spec, keeps threads synchronized to the same base, and avoids the WebKit behaviour.

Activity

  1. added 4 commits that reference this issue on Nov 16, 2026
    a4c55e1
    2b18c5d
    73ab4be
    957f176
  2. added a commit that references this issue on Nov 17, 2026
    a222a9c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions