Skip to content

In v3.1.58, why pthreads don't inherit the moduleArg? #21826

Closed

Description

@DenizUgur

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?

src += 'isPthread && %s();\n' % settings.EXPORT_NAME

Activity

  1. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    Can you share more details of your use case? In particular I'm not sure how the moduleArg would be transmitted to the worker.

    How were you populating the moduleArg? Do you know how it was visible to the workers?

  2. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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:

    'urlOrBlob': Module['mainScriptUrlOrBlob']

  3. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    mainScriptUrlOrBlob should still be supported. See:

    #if expectToReceiveOnModule('mainScriptUrlOrBlob')
    // We can't use makeModuleReceiveWithVar here since we want to also
    // call URL.createObjectURL on the mainScriptUrlOrBlob.
    if (Module['mainScriptUrlOrBlob']) {
    pthreadMainJs = Module['mainScriptUrlOrBlob'];
    if (typeof pthreadMainJs != 'string') {
    pthreadMainJs = URL.createObjectURL(pthreadMainJs);
    }
    }

    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.

  4. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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.

  5. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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.

  6. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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?

  7. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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.

  8. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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:

    -O2 -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=foo -sEXPORTED_FUNCTIONS=_free,_malloc,_main -sENVIRONMENT=web,worker -sEXPORTED_RUNTIME_METHODS=FS,OPFS,getValue,setValue,UTF8ToString,lengthBytesUTF8,stringToUTF8,cwrap,addFunction,PThread -sPTHREAD_POOL_SIZE=1 -sPTHREAD_POOL_SIZE_STRICT=0 -lopfs.js -sWASMFS -sFORCE_FILESYSTEM
    

    I've also tried to set INCOMING_MODULE_JS_API to mainScriptUrlOrBlob but that didn't produced mainScriptUrlOrBlob either.

  9. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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:

    $ emcc test/hello_world.c -pthread
    $ grep mainScriptUrlOrBlob a.out.js 
            // call URL.createObjectURL on the mainScriptUrlOrBlob.
            if (Module['mainScriptUrlOrBlob']) {
              pthreadMainJs = Module['mainScriptUrlOrBlob'];
    
  10. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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?

  11. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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.

    wasmBinaryFile = new URL('{{{ WASM_BINARY_FILE }}}', import.meta.url).href;

    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.

  12. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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?

  13. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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.

  14. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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.

  15. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    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:

    function _locateFile(path, prefix) {
      const hashedWasmBinaryFile = Module["hashedWasmBinaryFile"];
      if (hashedWasmBinaryFile && path.endsWith(".wasm")) {
        return hashedWasmBinaryFile;
      }
      return prefix + path;
    }
  16. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    Thank you @sbc100 for your insight and sorry to bother you with such a simple overisight.

  17. sbc100 commented on Apr 25, 2024

    @sbc100
    Collaborator

    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.

  18. DenizUgur commented on Apr 25, 2024

    @DenizUgur
    Author

    It sounds like the fact that locateFile was being called from a pthread was causing the issue?

    That's exactly what was going on. Seems like the PR you opened would prevent the issue I've faced yesterday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions