Repository navigation
Read performance.timeOrigin once per thread in emscripten_get_now - #28272
Conversation
There was a problem hiding this comment.
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.
| // 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', |
There was a problem hiding this comment.
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)',There was a problem hiding this comment.
performance is already checked on the line above (if (globalThis.performance?.now)), so this can't throw.
There was a problem hiding this comment.
Can you mark this as "__internal: true" ?
There was a problem hiding this comment.
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.
| if (globalThis.performance?.now) { | ||
| #if PTHREADS | ||
| _emscripten_get_now = () => performance.timeOrigin + performance.now(); | ||
| var timeOrigin = performance.timeOrigin; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
No change in existing behavior
d756644 to
a4c55e1
Compare
|
Do we think its likely that this will also fix #18677 ? |
sbc100
left a comment
There was a problem hiding this comment.
Nice find!
Thank you for the PR.
a4c55e1 to
2b18c5d
Compare
There's a chance, at least |
2b18c5d to
73ab4be
Compare
|
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
73ab4be to
957f176
Compare
Done |
|
@RReverser I'd still be interested in your thoughts on this.. |
| if (globalThis.performance?.now) { | ||
| #if PTHREADS | ||
| _emscripten_get_now = () => performance.timeOrigin + performance.now(); | ||
| var timeOrigin = performance.timeOrigin; |
There was a problem hiding this comment.
Nit
| var timeOrigin = performance.timeOrigin; | |
| var {timeOrigin} = performance; |
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
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
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
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
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
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
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