async error
emcc: 3.1.0
async error
During the execution of an asynchronous function, if other tasks are inserted, the state of the Promise function will always be pending
【code】
#include <emscripten.h>
#include<emscripten/bind.h>
#include <emscripten/val.h>
#include <iostream>
#include <string.h>
using namespace emscripten;
class Test {
public:
void printf() {
for (int i = 0; i < 1000; i++) {
std::cout<<"i: "<<i<<std::endl;
}
}
emscripten::val GetBase64Sha256Normal(const std::string &text)
{
using namespace emscripten;
val encoder = val::global()[u8"TextEncoder"].new_();
val data = encoder.call<val>(u8"encode", val(text));
val digestValue = val::global()[u8"crypto"][u8"subtle"].call<val>(u8"digest", val(u8"SHA-256"), data).await();
val base64 = val::global().call<val>(u8"btoa", val::global()[u8"String"][u8"fromCharCode"].call<val>(u8"apply", val::null(), val::global()[u8"Uint8Array"].new_(digestValue)));
return base64;
}
};
EMSCRIPTEN_BINDINGS(main) {
class_<Test>("Test")
.smart_ptr_constructor("Test", &std::make_shared<Test>)
.function("printf", &Test::printf)
.function(u8"GetBase64Sha256Normal", &Test::GetBase64Sha256Normal);
}
js:
console.log("before ");
const res = await Promise.all([test.getBase64Sha256Normal("helloworld"), test.printf()]);
console.log("after: ", res);
would not print the last : "after ………"
Can you share your full link command? (Are you building with ASYNCIFY?)
@RReverser is most familiar with embind + async I think.
Oh yeah, you absolutely cannot use async tasks in parallel via Emscripten exports.
Is the limitation that there can only be a single asyncify task/stack pending at a given time? Or is that you can't call into a wasm module at all if it has a suspended task/stack?
Is the limitation that there can only be a single asyncify task/stack pending at a given time?
Rather this.
I can't find the issue right now (maybe it was just a chat), but I remember suggesting to @kripken to add checks for this to Asyncify, as it's an easy trap to fall into. Sometimes it will just get the app stuck like in this issue report, while in other cases it can corrupt memory.
The way I'm thinking about Asyncify is essentially pthreads without all the pthread synchronization primitives, that is, you can't ensure that you're the only one accessing any shared data in the program at any given time. It's possible that a struct has been partially filled, the stack got suspended to retrieve some external value, and meanwhile another call comes in and tries to either read or also modify the same struct. I've seen this leading to corruptions pretty deep & far away from the original call, including some dlmalloc structures.
I think whatever the overhead of such added checks would be, it would be negligible compared to dangers from such inadvertent race conditions.
We do support multiple stacks in the Fibers API (which can switch between paused fibers). Probably we could do the same for general async operations. But it would take some work. An error in assertions builds for now sgtm.