beginner emscripten issue:emscripten_fetch on a background thread is failing

359 views
Skip to first unread message

Scott Watson

unread,
Aug 9, 2017, 2:37:54 PM8/9/17
to emscripten-discuss
I am having trouble porting our application to asm.js.  We have a class that handles HTTP GETs for us, that I am trying to port using the emscripten_fetch api.

I can only get the asynchronous fetch to work, and only when called on the main thread.  I can't get synchronous to work at all.  Using the sample code from: https://kripken.github.io/emscripten-site/docs/api_reference/fetch.html, I built this.

    // main.cpp

    #include <thread>
    #include <iostream>
    
    #include <emscripten/fetch.h>
    
    void downloadSucceeded(emscripten_fetch_t *fetch) {
      printf("Finished asynchronously downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
      // The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
      emscripten_fetch_close(fetch); // Free data associated with the fetch.
    }
    
    void downloadFailed(emscripten_fetch_t *fetch) {
      printf("Downloading %s asynchronously failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
      //emscripten_fetch_close(fetch); // Also free data on failure.
    }
    
    void worker()
    {
      emscripten_fetch_attr_t attr;
    
      emscripten_fetch_attr_init(&attr);
    
      strcpy(attr.requestMethod, "GET");
    
      attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
      attr.onsuccess = downloadSucceeded;
      attr.onerror = downloadFailed;
    
      emscripten_fetch(&attr, "http://localhost/VOD/nangu/00000041.ts"); // Blocks here until the operation is complete.
    }
    
    int main()
    {
        // worker();
        std::thread test1(worker);
    }

I am building it using:

    emcc main.cpp -o $(OUTPUT_FOLDER)/thread_test.html -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=5 -s FETCH=1

My downloadFailed method is being called with a 'fetch->status' of 0.  If I call the 'worker()' method directly I get the expected behavior.

I am tearing my hair out here.  I have googled everything I can think of to no avail.

Jukka Jylänki

unread,
Aug 9, 2017, 3:35:20 PM8/9/17
to emscripte...@googlegroups.com
First thing is to remember to also compile with -s NO_EXIT_RUNTIME=1,
since once main() exits, it will tear down the application runtime,
along with all threads, so the linker flag will keep it running after
exiting main. I wonder if that already will solve the issue?

Another note is that C++11 threading is known to have some bugs, the
pthread_create() API is robust and unit tested. (If someone knows of a
good open source unit test suite for C++11 threading, I'm all ears)
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-disc...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Scott Watson

unread,
Aug 9, 2017, 3:45:22 PM8/9/17
to emscripten-discuss
First, thank you for the assistance!

-s NO_EXIT_RUNTIME=1 had no effect.  Actually, I think you get that automatically with -s USE_PTHREADS=1 don't you?

I switched to using pthreads directly with no effect either.

Jukka Jylänki

unread,
Aug 9, 2017, 4:06:28 PM8/9/17
to emscripte...@googlegroups.com
Hmm, can you try specifying the following attributes for the download?

attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY |
EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE;

Also there is a linker flag -s FETCH_DEBUG=1 that can show more information.
>> > email to emscripten-disc...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to emscripten-disc...@googlegroups.com.

Scott Watson

unread,
Aug 9, 2017, 4:22:05 PM8/9/17
to emscripte...@googlegroups.com
Ok, that’s interesting. The synchronous download worked on the background thread. This works now with -s NO_EXIT_RUNTIME=1. It had no effect on the asynchronous download on a background thread. I still get a call to downloadFailed with a status of 0.
> You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/wBCcSKPZsx8/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages