Skip to content

[browser] initialize runtime in a web worker - with it's own messaging #114918

Open

Description

@bcairns-google

Description

When dotnet.js is started in a web worker with an onmessage handler, the promises for asset instantiation are never resolved and therefore initialization hangs.

There appears to be a check in browser/runtime/loader/assets.ts which checks for ENVIRONMENT_IS_WORKER and deliberately avoids resolving the promises:

if (!ENVIRONMENT_IS_WORKER) {

and
if (!ENVIRONMENT_IS_WORKER) {

If ENVIRONMENT_IS_WORKER is manually set to false in the debugger, loading will complete normally and dotnet.js functions correctly in a web worker.

ENVIRONMENT_IS_WORKER is set here:

export const ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_WEB_WORKER && !ENVIRONMENT_IS_SIDECAR; // we redefine what ENVIRONMENT_IS_WORKER, we replace it in emscripten internals, so that sidecar works

The logic is a bit odd:
ENVIRONMENT_IS_WORKER is true if both of the following are true:

  • importScripts is a function
  • globalThis.onmessage is truthy

If globalThis.onmessage is undefined, globalThis.dotnetSidecar is set to true, ENVIRONMENT_IS_WORKER will be false, and loading works normally.

Reproduction Steps

 <script>
  const dotnetUrl = 'dotnet.js';
  const wasmConfig = {} // blazor.boot.json or equivalent here
  const workerSrc = `self.onmessage = async (initialMsg) => {
    const { dotnet } = await import('${dotnetUrl}');
    const { setModuleImports, getAssemblyExports } = await dotnet.withConfig(${JSON.stringify(wasmConfig)}).create();
    const assembly = await getAssemblyExports('WasmWorker');
  };`;
  const workerBlob = new Blob([workerSrc], {type: 'application/javascript'});
  const workerUrl = URL.createObjectURL(workerBlob);
  const worker = new Worker(workerUrl, { type: 'module'});
  worker.postMessage('init');
</script>

Expected behavior

dotnet.js initializes successfully in the web worker.

Actual behavior

The dotnet.create() promise is never resolved and initialization hangs.

Regression?

This worked correctly in .NET 8.

This commit introduced the logic which breaks loading in a web worker with an onmessage handler:
8698d3d

Known Workarounds

Delete any globalThis.onmessage handler before calling dotnet.create()

Configuration

.NET SDK 9.0.203
Behavior can be reproduced in Chrome, Safari, Firefox, and presumably any other browser that Blazor supports.

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Apr 22, 2025
  2. added
    untriaged
    on Apr 22, 2025
  3. self-assigned this
    on Apr 22, 2025
  4. added
    arch-wasm
    os-browser
    on Apr 22, 2025
  5. modified the milestones: Future, 10.0.0 on Apr 22, 2025
  6. removed
    needs-area-label
    untriaged
    on Apr 22, 2025
  7. dotnet-policy-service commented on Apr 22, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing
    See info in area-owners.md if you want to be subscribed.

  8. pavelsavara commented on Apr 22, 2025

    @pavelsavara
    Member

    @bcairns-google I think you can do self.dotnetSidecar=true in worker to enable this scenario without needing to kill onmessage.

    I did it here #92280

    Does that work for you ?

  9. bcairns-google commented on Apr 22, 2025

    @bcairns-google
    Author

    Setting self.dotnetSidecar=true probably would work around this issue, but it also sets ENVIRONMENT_IS_SIDECAR to true. I'm a bit concerned that there might be side effects from doing so in the future.

    Is there a particular reason to not resolve the promises in

    if (!ENVIRONMENT_IS_WORKER) {
    when ENVIRONMENT_IS_WORKER is true? You said something earlier about multi-threading, I'm just trying to understand why this would interact with the loading behavior.

  10. pavelsavara commented on Apr 22, 2025

    @pavelsavara
    Member

    You said something earlier about multi-threading, I'm just trying to understand why this would interact with the loading behavior.

    When dotnet starts in multi-threading mode it also has to load some of it's assets (think JS files). Those which are loaded into linear memory, are already loaded by main thread only.

    runtimeHelpers.deputyWorker.thread!.postMessageToWorker({

    which arrives by

    runtimeHelpers.allAssetsInMemory.promise_control.resolve();

  11. 14 remaining items

  12. VYDocuWare commented on Jul 18, 2025

    @VYDocuWare

    Interestingly enough, I can't reproduce it with self.dotnetSidecar=true on Firefox 140.0.4 (I restarted the computer in the meantime) - while it's still happening on Chrome 138.0.7204.158.

    I'm on NET 9.0.

  13. VYDocuWare commented on Jul 18, 2025

    @VYDocuWare

    setting config.maxParallelDownloads=1; seems to help with "pending" assets.

    How do I set this?

  14. pavelsavara commented on Jul 21, 2025

    @pavelsavara
    Member

    setting config.maxParallelDownloads=1; seems to help with "pending" assets.

    How do I set this?

    @VYDocuWare await dotnet.withConfig({ maxParallelDownloads: 1 }).create();

  15. VYDocuWare commented on Jul 23, 2025

    @VYDocuWare

    This seems to work around the issue in Chrome, thank you very much.

  16. pavelsavara commented on Jul 23, 2025

    @pavelsavara
    Member

    That looks like chrome race condition.
    It would be great to create repro case with simple JS, rather than whole dotnet complexity.
    And then report it to chrome.

  17. changed the title [-]dotnet.js fails to initialize when running in a web worker with onmessage handler[/-] [+][browser] initialize runtime in a web worker - with it's own messaging[/+] on Sep 29, 2025
  18. maraf commented on Nov 21, 2025

    @maraf
    Member

    An observeration

    Case 1

    1. Start dotnet on WebWorker
    2. Install my custom onmessage handler on WebWorker
      => I don't need to set dotnetSidecar=true

    Case 2

    1. Install my custom onmessage handler on WebWorker
    2. Start dotnet on WebWorker
      => I have to set dotnetSidecar=true

    I don't think changing the order should have effect. I believe in ST build we should always fallback to sidecar=true no matter if there is an onmessage handler or not.

  19. modified the milestones: 11.0.0, Future on Jun 3, 2026
  20. added a commit that references this issue on Jul 3, 2026
  21. modified the milestones: Future, 12.0.0 on Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-VM-threading-monoos-browser

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions