Skip to content

Read performance.timeOrigin once per thread in emscripten_get_now - #28272

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
gribbet:safari-timeorigin
Nov 16, 2026
Merged

sbc100 merged 1 commit into
emscripten-core:mainfrom
gribbet:safari-timeorigin

Conversation

@gribbet

@gribbet gribbet commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

WebKit recomputes performance.timeOrigin on every access and in cross-origin isolated contexts the value varies between reads, which made emscripten_get_now() (and clock_gettime(CLOCK_MONOTONIC)) go backwards in Safari with pthreads.

HR-Time specifies timeOrigin as constant for a global, so reading it once per thread is equivalent per spec and keeps the cross-thread synchronization from #18267.

Fixes: #28271
See: https://bugs.webkit.org/show_bug.cgi?id=331772

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies emscripten_get_now() in pthreads builds to read performance.timeOrigin only once per thread, addressing a WebKit bug where the value can vary between reads and cause the clock to run backwards. The review feedback highlights two critical issues: first, accessing performance.timeOrigin directly at startup can throw a ReferenceError in environments where performance is undefined; second, if performance.timeOrigin is undefined, adding it to performance.now() will result in NaN. It is recommended to use safe fallbacks to 0 for both cases.

Comment thread src/lib/libcore.js
// on every access and the value can vary between reads, which would make
// emscripten_get_now() go backwards.
// https://bugs.webkit.org/show_bug.cgi?id=331772
$timeOrigin: '=performance.timeOrigin',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Evaluating performance.timeOrigin directly at startup can throw a ReferenceError in environments where performance is not defined (such as certain JS shells or older Node.js environments). Using globalThis.performance?.timeOrigin with a fallback to 0 prevents this startup crash.

  $timeOrigin: '=(globalThis.performance?.timeOrigin || 0)',

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance is already checked on the line above (if (globalThis.performance?.now)), so this can't throw.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark this as "__internal: true" ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance is already checked on the line above (if (globalThis.performance?.now)), so this can't throw.

Did you mean to post this comment on the audio worklet usage below? Here it doesn't have any check.

That said, this code is only compiled under PTHREADS, so the check is still unnecessary - all such environments have performance defined.

Comment thread src/lib/libcore.js
if (globalThis.performance?.now) {
#if PTHREADS
_emscripten_get_now = () => performance.timeOrigin + performance.now();
var timeOrigin = performance.timeOrigin;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If performance.timeOrigin is undefined (which is the case in older browsers or environments that support performance.now() but not performance.timeOrigin), timeOrigin will be undefined. Adding it to performance.now() will result in NaN, breaking all timing functions. Defaulting to 0 avoids this issue.

      var timeOrigin = performance.timeOrigin || 0;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change in existing behavior

@sbc100

sbc100 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Do we think its likely that this will also fix #18677 ?

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

Thank you for the PR.

@sbc100
sbc100 requested a review from RReverser November 16, 2026 20:01
@gribbet

gribbet commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Do we think its likely that this will also fix #18677 ?

There's a chance, at least

@sbc100

sbc100 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Can you use the PR number in the ChangeLog entry, rather than the bug number?

WebKit recomputes performance.timeOrigin on every access and in
cross-origin isolated contexts the value varies between reads, which
made emscripten_get_now() (and clock_gettime(CLOCK_MONOTONIC)) go
backwards in Safari with pthreads.

HR-Time specifies timeOrigin as constant for a global, so reading it
once per thread is equivalent per spec and keeps the cross-thread
synchronization from emscripten-core#18267.

Fixes: emscripten-core#28271
See: https://bugs.webkit.org/show_bug.cgi?id=331772
@gribbet

gribbet commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Can you use the PR number in the ChangeLog entry, rather than the bug number?

Done

@sbc100
sbc100 merged commit a222a9c into emscripten-core:main Nov 16, 2026
43 checks passed
@sbc100

sbc100 commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

@RReverser I'd still be interested in your thoughts on this..

Comment thread src/lib/libcore.js
if (globalThis.performance?.now) {
#if PTHREADS
_emscripten_get_now = () => performance.timeOrigin + performance.now();
var timeOrigin = performance.timeOrigin;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
var timeOrigin = performance.timeOrigin;
var {timeOrigin} = performance;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh.. I like that trick :)

sbc100 added a commit that referenced this pull request Nov 17, 2026
Apply followup feedback from #28272 in `src/lib/libcore.js`. Use `const
{timeOrigin} = performance;` instead of `var` so Closure Compiler lowers
it to `let` rather than a hoisted `var` and destructuring assignment,
saving bytes both with and without Closure.

See: #28272
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 17, 2026
Followup to emscripten-core#28286

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Also update `src/jsifier.mjs` arrow-function detection so top-level JS
library arrow functions with destructured parameters (containing `}`)
are recognized and emitted properly.

Update affected `test/codesize/` expectations.

See: emscripten-core#28272
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 17, 2026
Followup to emscripten-core#28286

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Also update `src/jsifier.mjs` arrow-function detection so top-level JS
library arrow functions with destructured parameters (containing `}`)
are recognized and emitted properly.

Update affected `test/codesize/` expectations.

See: emscripten-core#28272
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 18, 2026
Followup to emscripten-core#28286

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Update affected `test/codesize/` expectations.

See: emscripten-core#28272
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 18, 2026
Followup to emscripten-core#28286

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Update affected `test/codesize/` expectations.

See: emscripten-core#28272
sbc100 added a commit to sbc100/emscripten that referenced this pull request Nov 23, 2026
Followup to emscripten-core#28286

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Update affected `test/codesize/` expectations.

See: emscripten-core#28272
sbc100 added a commit that referenced this pull request Nov 23, 2026
Followup to #28286
Depends on #28288

Use object and parameter destructuring for multi-property reads and
unused event parameters across `src/lib/` and
`src/pthread_esm_startup.mjs`, using `const` for declarations so
Closure Compiler lowers them cleanly without extra bindings.

Update affected `test/codesize/` expectations.

See: #28272
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.

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

3 participants