I am trying to use Emscripten to fetch and process a set of images. The approach below works well with a single image file, but it fails when fetching multiple images simultaneously, resulting in various errors. I believe the asynchronous fetch operations are sharing resources, which leads to race conditions, data inconsistencies, and other issues. I am using Emscripten’s Fetch API to retrieve the images.
How can I solve this problem?
function read_object(object, url) {
return new Promise((resolve, reject) => {
try {
Module.onFetchClosed = (succeeded) => {
if (!succeeded) {
reject(new Error("Fetch failed"));
return;
};
resolve();
};
object.read_url(url);
} catch(e) {
reject(e);
};
});
};
var Module = { onRuntimeInitialized: function() {
async function async_call(n) {
try {
var object = new Module.cpp_object();
let url = await get_url(n);
await read_object(object, url);
...
} catch(e) {
object.delete();
console.error("Error:", e);
};
};
let promises = [];
for (let i = 0; i < NUM; i++) {
let promise = async_call(i);
promises.push(promise)
console.log(i);
};
Promise.all(promises).then(() => {
...;
}).catch((err) => {
console.error("Something went wrong:", err);
});
}};
I get these errors:
Uncaught RuntimeError: index out of bounds
Error: Object { name: "ExitStatus", message: "Program terminated with exit(1)", status: 1 }
I have tried another approach by modularizing the Emscripten code and giving a Module to each async_call. It works, therefore the C++ code should be okay. However, I don’t believe that giving a Module to each async_call is correct.