You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In v3.1.58, why pthreads don't inherit the moduleArg? #21826
I'm not sure if this was an oversight but with the latest version, pthreads don't use the same moduleArg that was given to the main module. I'm relying on this argument to supply hashed version of my wasm file. Is there any other suggested way to achieve this?
I was utilizing the "hack" described on ffmpeg.wasm to pass my custom wasm URL. Since that file is suffixed with an hash during application bundling, this seems to be necessary. On the previous version mainScriptUrlOrBlob was sent to the worker thread so that it can be used import the main module again using the value that was received. See here:
What would be the suggested way of setting wasm binary name during runtime? Also how do you enable mainScriptUrlOrBlob because I don't see it in my build output.
I think I'll use a combination of sessionStorage and locateFile. Not sure what would be the most elegant way to do this natively with Emscripten but if you believe this is a valid use-case then I think it's worth looking into.
Edit: Apperantly I missed the fact that sessionStorage is not available in workers.
Pthreads don't need to locate the wasm file because they all get the wasm module delivered via postMessage on startup. i.e. the module is shared from the main thread.
mainScriptUrlOrBlob tells the system is used to the control the name/content for the main JavaScript script itself.
If mainScriptUrlOrBlob is not showing up in your output it could be you are overriding INCOMING_MODULE_JS_API? Or maybe you are using -sSTRICT with makes INCOMING_MODULE_JS_API empty by default?
Well that may be the case, but for me I can confirm that it tries to locate the file twice (verified via good old console.log). Second one returns empty because the variable I've set in the main module is lost on the second instance.
Oh wait.. I think its because you are using EXPORT_ES6. In that scenario it should not necessary to use mainScriptUrlOrBlob since the main script can always be located via import.meta.url. I wonder why that is not working in your case?
Can you elaborate on "it tries to locate the file twice"? Which file and what is function are where is the it trying to be located from?
I've put a console.trace("load") before this line and the second trace shows that the pthread is accessing this line. In this instance, I haven't supplied a locateFile function in my --pre-js.
Also when you say "the variable I've set in the main module", which variable exactly are you talking about and how exactly do you set it?
Similiar to how ffmpeg.wasm was supplying mainScriptUrlOrBlob, I've defined a new variable in my --pre-js called hashedWasmBinaryFile. When I call my emscripten module load function, I supplied this variable to where moduleArg supposed to be.
It works okay for the main module but whenever pthread creates the instance again that variable is lost because it didn't supplied that variable. That's expected because it doesn't know what to set that variable to. The ffmpeg.wasm solution uses the hash portion of mainScriptUrlOrBlob to create an artifical state across threads to mitigate this.
When you say "it didn't produced mainScriptUrlOrBlob" is that string not seen at all in your output JS file?
Yes, it's not there. I've used -sINCOMING_MODULE_JS_API=mainScriptUrlOrBlob
the main script can always be located via import.meta.url
Correct, but wasm file is hardcoded during build so there is no way to infer the wasm file name if it changes during bundling. That wouldn't be an issue with the main script as it's loaded from the application script anyway.
So IIUC mainScriptUrlOrBlob has always been about locating the main JavaScript file, and never about locating the wasm file (which can be controled via adding a custom locateFile). So I'm a little confused about why your use case (or the ffmpeg one) even use mainScriptUrlOrBlob.
Also IIUC locateFile should only be used to find the wasm file exactly once, one the main thread, and never on worker threads (I will double check that now since it seems you are seeing otherwise).
Is there any chance you should share your completely setup? i.e. is your project open source?
I've put a console.trace("load") before this line and the second trace shows that the pthread is accessing this line.
While its true that wasmBinaryFile might be calculated on by worker when it loads the main script, that value of it should never be used since pthreads override the nomral module loading process by overriding instantiateWasm. By using this override they bypass all of the module creation code and instead use the module they receive via postMessage from the main thread.
The project itself is not open-source but I'm using https://github.com/gpac/gpac as the wasm binary. Use the same link flags I gave above (edit here) and configure using ./configure --emscripten. Please note that the upstream requires some patches to support EXPORT_ES6 so demo page you see here will not work.
While its true that wasmBinaryFile might be calculated on by worker when it loads the main script, that value of it should never be used since pthreads override the nomral module loading process by overriding instantiateWasm. By using this override they bypass all of the module creation code and instead use the module they receive via postMessage from the main thread.
Oh wait... that's where I went wrong. I assumed since locateFile is being called it should be properly populated. I'll add a check with ENVIRONMENT_IS_PTHREAD to fix this.
I tried it and it works like a charm. If I only haven't been so defensive in my implementation I would've probably gotten away :)
For future readers, to override the default wasm file path you can use the following locateFile override:
Can you explain the fix a little more? It sounds like the fact that locateFile was being called from a pthread was causing the issue? Perhaps we should try to avoid that completely.
Activity
sbc100 commentedon Apr 25, 2024
Can you share more details of your use case? In particular I'm not sure how the
moduleArgwould be transmitted to the worker.How were you populating the
moduleArg? Do you know how it was visible to the workers?DenizUgur commentedon Apr 25, 2024
I was utilizing the "hack" described on ffmpeg.wasm to pass my custom wasm URL. Since that file is suffixed with an hash during application bundling, this seems to be necessary. On the previous version
mainScriptUrlOrBlobwas sent to the worker thread so that it can be used import the main module again using the value that was received. See here:emscripten/src/library_pthread.js
Line 381 in cf90417
sbc100 commentedon Apr 25, 2024
mainScriptUrlOrBlobshould still be supported. See:emscripten/src/library_pthread.js
Lines 458 to 466 in 7c31c12
So there must be some way this usage is different and not working for your use case. Hopefully we can figure it and add a test for that technique.
DenizUgur commentedon Apr 25, 2024
What would be the suggested way of setting wasm binary name during runtime? Also how do you enable
mainScriptUrlOrBlobbecause I don't see it in my build output.DenizUgur commentedon Apr 25, 2024
I think I'll use a combination of
sessionStorageandlocateFile. Not sure what would be the most elegant way to do this natively with Emscripten but if you believe this is a valid use-case then I think it's worth looking into.sbc100 commentedon Apr 25, 2024
Pthreads don't need to locate the wasm file because they all get the wasm module delivered via postMessage on startup. i.e. the module is shared from the main thread.
mainScriptUrlOrBlobtells the system is used to the control the name/content for the main JavaScript script itself.If
mainScriptUrlOrBlobis not showing up in your output it could be you are overridingINCOMING_MODULE_JS_API? Or maybe you are using-sSTRICTwith makesINCOMING_MODULE_JS_APIempty by default?sbc100 commentedon Apr 25, 2024
I don't understand this comment: https://github.com/ffmpegwasm/ffmpeg.wasm/blob/ae1cdac7db79c5315f9a1b716fcbd9bcfe27c902/src/bind/ffmpeg/bind.js#L93-L96, It seems to suggest that each worker needs to locate the wasm file.. but IIUC workers have always recieved the wasm module via postMessage so have never needed to know its location.
DenizUgur commentedon Apr 25, 2024
Well that may be the case, but for me I can confirm that it tries to locate the file twice (verified via good old
console.log). Second one returns empty because the variable I've set in the main module is lost on the second instance.For reference here is my link flags:
I've also tried to set
INCOMING_MODULE_JS_APItomainScriptUrlOrBlobbut that didn't producedmainScriptUrlOrBlobeither.sbc100 commentedon Apr 25, 2024
Can you elaborate on "it tries to locate the file twice"? Which file and what is function are where is the it trying to be located from?
Also when you say "the variable I've set in the main module", which variable exactly are you talking about and how exactly do you set it?
When you say "it didn't produced mainScriptUrlOrBlob" is that string not seen at all in your output JS file?
I certainly see it when I build a simple moulde:
sbc100 commentedon Apr 25, 2024
Oh wait.. I think its because you are using
EXPORT_ES6. In that scenario it should not necessary to use mainScriptUrlOrBlob since the main script can always be located viaimport.meta.url. I wonder why that is not working in your case?DenizUgur commentedon Apr 25, 2024
I've put a
console.trace("load")before this line and the second trace shows that the pthread is accessing this line. In this instance, I haven't supplied alocateFilefunction in my--pre-js.emscripten/src/preamble.js
Line 617 in 7c31c12
Similiar to how ffmpeg.wasm was supplying
mainScriptUrlOrBlob, I've defined a new variable in my--pre-jscalledhashedWasmBinaryFile. When I call my emscripten module load function, I supplied this variable to wheremoduleArgsupposed to be.It works okay for the main module but whenever pthread creates the instance again that variable is lost because it didn't supplied that variable. That's expected because it doesn't know what to set that variable to. The ffmpeg.wasm solution uses the hash portion of
mainScriptUrlOrBlobto create an artifical state across threads to mitigate this.Yes, it's not there. I've used
-sINCOMING_MODULE_JS_API=mainScriptUrlOrBlobCorrect, but wasm file is hardcoded during build so there is no way to infer the wasm file name if it changes during bundling. That wouldn't be an issue with the main script as it's loaded from the application script anyway.
sbc100 commentedon Apr 25, 2024
So IIUC
mainScriptUrlOrBlobhas always been about locating the main JavaScript file, and never about locating the wasm file (which can be controled via adding a customlocateFile). So I'm a little confused about why your use case (or the ffmpeg one) even usemainScriptUrlOrBlob.Also IIUC
locateFileshould only be used to find the wasm file exactly once, one the main thread, and never on worker threads (I will double check that now since it seems you are seeing otherwise).Is there any chance you should share your completely setup? i.e. is your project open source?
sbc100 commentedon Apr 25, 2024
While its true that
wasmBinaryFilemight be calculated on by worker when it loads the main script, that value of it should never be used since pthreads override the nomral module loading process by overridinginstantiateWasm. By using this override they bypass all of the module creation code and instead use the module they receive via postMessage from the main thread.DenizUgur commentedon Apr 25, 2024
The project itself is not open-source but I'm using https://github.com/gpac/gpac as the wasm binary. Use the same link flags I gave above (edit here) and configure using
./configure --emscripten. Please note that the upstream requires some patches to supportEXPORT_ES6so demo page you see here will not work.DenizUgur commentedon Apr 25, 2024
Oh wait... that's where I went wrong. I assumed since
locateFileis being called it should be properly populated. I'll add a check withENVIRONMENT_IS_PTHREADto fix this.I tried it and it works like a charm. If I only haven't been so defensive in my implementation I would've probably gotten away :)
For future readers, to override the default wasm file path you can use the following
locateFileoverride:DenizUgur commentedon Apr 25, 2024
Thank you @sbc100 for your insight and sorry to bother you with such a simple overisight.
sbc100 commentedon Apr 25, 2024
Thats great to hear!
Can you explain the fix a little more? It sounds like the fact that locateFile was being called from a pthread was causing the issue? Perhaps we should try to avoid that completely.
Skip wasmBinaryFile initialization on pthread workers
Skip wasmBinaryFile initialization on pthread workers
DenizUgur commentedon Apr 25, 2024
That's exactly what was going on. Seems like the PR you opened would prevent the issue I've faced yesterday.
Skip wasmBinaryFile initialization on pthread workers
Skip wasmBinaryFile initialization on pthread workers
Skip wasmBinaryFile initialization on pthread workers
Skip wasmBinaryFile initialization on pthread workers (#21829)