Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node throws exception in main thread (?) when trying to execute event via ThreadSafeFunction #1457

Open
markpiffer opened this issue Feb 29, 2024 · 1 comment

Comments

@markpiffer
Copy link

markpiffer commented Feb 29, 2024

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 #110 (comment)

@markpiffer markpiffer changed the title event execution crashes in Node main thread (?) when trying to execute event via ThreadSafeFunction Node throws exception in main thread (?) when trying to execute event via ThreadSafeFunction Feb 29, 2024
@KevinEady
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants