node-addon-api
node-addon-api copied to clipboard
Node throws exception in main thread (?) when trying to execute event via ThreadSafeFunction
I was trying this with Napi::ThreadSafeFunction but it keeps crashing when Node encounters the function call in its event loop supposedly. The C++ itself runs through without problems.
Napi::ThreadSafeFunction emitFuncThreadSafe;
void configIAmEvent(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object eventEmitter = info[0].As<Napi::Object>();
Napi::Value emitFuncValue = eventEmitter.Get("emit");
if (emitFuncValue.IsFunction()) {
Napi::Function emitFunction = emitFuncValue.As<Napi::Function>();
emitFuncThreadSafe = Napi::ThreadSafeFunction::New(env, emitFunction, "IAmEventEmitter", 0, 5);
}
}
void emit_iam(const char* ip) {
// emitFuncThreadSafe.BlockingCall(ip, [](Napi::Env env, Napi::Function f, const char* ip) { f.Call({Napi::String::New(env,"IAm"), Napi::String::New(env,ip)}); });
emitFuncThreadSafe.NonBlockingCall(ip, [](Napi::Env env, Napi::Function f, const char* ip) {
f.Call({Napi::String::New(env, "IAm"), Napi::String::New(env, ip)}); });
}
The result is a:
#
# Fatal error in HandleScope::HandleScope
# Entering the V8 API without proper locking in place
#
Exception: Exception 0x80000003 encountered at address 0x7ff6138bc532
Exception: Exception 0x80000003 encountered at address 0x7ff6138bbde0
Process finished with exit code 3
I can assert that the (same) event was generated two times in the native code which I think is the reason for two Exceptions.
What am I doing wrong?
Originally posted by @markpiffer in https://github.com/nodejs/node-addon-api/issues/110#issuecomment-1970839979
Hi @markpiffer ,
Is it possible for you to provide a more in-depth reproduction, preferably in a git repository? A cursory glance at your pasted code shows nothing problematic / out of the ordinary.
If you are looking for EventEmitter integrations with node-addon-api, take a look at the examples.
Thanks, Kevin
Closing due to inactivity. If you need further assistance, please let us know.