2

In JavaScript, is it possible to use a SharedArrayBuffer in a SharedWorker?

When serving an index.html

<html>
 <body>
  <script type="text/javascript">
   console.log(
    'outer:', 'typeof SharedArrayBuffer:', typeof SharedArrayBuffer,
    'crossOriginIsolated:', crossOriginIsolated);
   new SharedWorker('worker.js');
  </script>
 </body>
</html>

and a worker.js

console.log(
 'inner:', 'typeof SharedArrayBuffer:', typeof SharedArrayBuffer,
 'crossOriginIsolated:', crossOriginIsolated);

with e.g. Emscripten's emrun (which sends

Access-Control-Allow-Origin: *
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: credentialless
Cross-Origin-Resource-Policy: cross-origin

headers among others; whichever of these may or may not be relevant here), browsing that index.html with e.g. Chrome 138.0.7204.168 on Linux, the index.html tab's console says

outer: typeof SharedArrayBuffer: function crossOriginIsolated: true

as expected, but the shared worker's console (see chrome://inspect/#workers) says

inner: typeof SharedArrayBuffer: undefined crossOriginIsolated: false

A very wild guess for a reason might be https://html.spec.whatwg.org/multipage/workers.html#run-a-worker in step 12.3.4 saying

If worker global scope's embedder policy's value is compatible with cross-origin isolation and is shared is true, then set agent's agent cluster's cross-origin isolation mode to "logical" or "concrete". The one chosen is implementation-defined.

and in step 12.3.6 saying

Set worker global scope's cross-origin isolated capability to true if agent's agent cluster's cross-origin isolation mode is "concrete".

so it might be Chrome decides to set that to "logical"---as per the note at https://html.spec.whatwg.org/multipage/document-sequences.html#cross-origin-isolation-mode:

On some platforms, it is difficult to provide the security properties required to grant safe access to the APIs gated by the [https://html.spec.whatwg.org/multipage/webappapis.html#concept-settings-object-cross-origin-isolated-capability](cross-origin isolated capability]. As a result, only "concrete" can grant access that capability. "logical" is used on platform not supporting this capability, where various restrictions imposed by cross-origin isolation will still apply, but the capability is not granted.

(And thus the security requirements for using SharedArrayBuffer would not be met.)

(SharedArrayBuffer and SharedWorker looks related, but it is from 2017 and I don't know how relevant that question and its answer are still today.)

1
  • 1
    Q&A from 2017 on this topic are no longer relevant because of Spectre. The new behavior dates to 2020. Commented Jul 30 at 15:03

1 Answer 1

-1

I tried answering you on mozilla but moderated new user there, so here again:

Currently not possible it seems. https://docs.google.com/document/d/1i3IA3TG00rpQ7MKlpNFYUF6EfLcV01_Cv3IYG_DjF7M/edit?tab=t.0

Problem is that it runs in a whole other process - you can copy buffer content ofc but you cannot access shared buffers from the main thread.

So even if you force enable usage its unlikely you get the same data in the buffer.

Update: After working with this for another ~2 weeks your best approach is using Workers. They will be their own thread and its possible to create a second tab in the same process as the first. THEN you can use both the main tab and any number of workers or secondary tabs to access the SharedArrayBuffer and actually profit from the speedup.

A SharedWorker though will always be forced its on process ID. You can see and debug this using the Task Manager in Chrome and i have to admit - so far all my use-cases are doable with multi-threading and shared memory this way.

There might be use-cases where you need a SharedWorker in its own process that will run if you open newtabs the normal way - but your browser cannot possibly know you intend to have this newtab access the same memory, thus if needed: Stay with Workers and window.open()

You still need secure Context for this ofc.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "I tried answering you on mozilla"?
Hey Bergi, he asked the same question later on discourse.mozilla.org, see discourse.mozilla.org/t/…
I kept at the problem and your best bet is using multithreading inside a single process - using a normal Worker and other tabs/windows using window.open you can pass around the SharedArrayBuffer and still have multithreading.There is no way, not even in latest Chrome unstable to get isolated = true with a SharedWorker. Im not sure whether this will change in the future, but if you want multithreading with SAB - it's a possible workaround.Demo: krei.se/vid/outputtriple.mp4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.