Repository navigation
Description
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
dotnet-policy-service commentedon Apr 22, 2025
Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.
pavelsavara commentedon Apr 22, 2025
@bcairns-google I think you can do
self.dotnetSidecar=truein worker to enable this scenario without needing to killonmessage.I did it here #92280
Does that work for you ?
bcairns-google commentedon Apr 22, 2025
Setting
self.dotnetSidecar=trueprobably would work around this issue, but it also setsENVIRONMENT_IS_SIDECARtotrue. 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
runtime/src/mono/browser/runtime/loader/assets.ts
Line 253 in 7dcd892
ENVIRONMENT_IS_WORKERistrue? You said something earlier about multi-threading, I'm just trying to understand why this would interact with the loading behavior.pavelsavara commentedon Apr 22, 2025
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.
runtime/src/mono/browser/runtime/startup.ts
Line 330 in cc2406f
which arrives by
runtime/src/mono/browser/runtime/pthreads/deputy-thread.ts
Line 38 in cc2406f
14 remaining items
VYDocuWare commentedon Jul 18, 2025
Interestingly enough, I can't reproduce it with
self.dotnetSidecar=trueon 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.
VYDocuWare commentedon Jul 18, 2025
How do I set this?
pavelsavara commentedon Jul 21, 2025
@VYDocuWare
await dotnet.withConfig({ maxParallelDownloads: 1 }).create();VYDocuWare commentedon Jul 23, 2025
This seems to work around the issue in Chrome, thank you very much.
pavelsavara commentedon Jul 23, 2025
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.
[-]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[/+]maraf commentedon Nov 21, 2025
An observeration
Case 1
onmessagehandler on WebWorker=> I don't need to set dotnetSidecar=true
Case 2
onmessagehandler 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
onmessagehandler or not.feat(zil): compile in the Web Worker — fix the dotnet.create() hang (d…
Upgrade to .NET 10.0 (from .NET 8.0) (#249)
feat(i6): compile in a Web Worker with a wall-clock timeout
Run the extractor in a worker, loaded on demand (Phase 6, step 3)