Skip to content

Synchronous Fetch API Bug with Fix #8749

@elliott-wen

Description

@elliott-wen

Hi All

I somehow found that the synchronous API is not working properly.

I used the following example source code from the API document.

#include <stdio.h>
#include <string.h>
#include <emscripten/fetch.h>
int main() {
  emscripten_fetch_attr_t attr;
  emscripten_fetch_attr_init(&attr);
  strcpy(attr.requestMethod, "GET");
  attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
  emscripten_fetch_t *fetch = emscripten_fetch(&attr, "test.txt"); // Blocks here until the operation is complete.
  if (fetch->status == 200) {
    printf("Finished 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];
  } else {
    printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
  }
  emscripten_fetch_close(fetch);
}

I then compiled the source code using
emcc example.cc -s FETCH=1 --proxy-to-worker -o example.html

Then I started the emrun http server, which allowed me to load the example.html in my firefox browser (Ubuntu 19.04) through http://localhost:6931/example.html

Somehow the console outputted

running code in a webworker
Downloading test.txt failed, HTTP failure status code: 0.

I double checked that http://localhost:6901/test.txt exists and the link flag contains --proxy-to-worker.

My emcc version is 1.38.31. (Also tried the upstream one 1.38.34)

Anyone could enlighten me?

Cheers
Elliott

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