Repository: nodejs/node-addon-api

Issue #622: ThreadSafeFunction delay, callback is not called "immediately"
Created: 2019-12-06T00:54:42Z
Given the following code, that is almost exactly one of the examples of the doc for TSFN:
/*I am within an exported class' member function, asyncConnect and connectThread are members of that class*/

asyncConnect = Napi::ThreadSafeFunction::New(
    env, // from Napi::CallbackInfo
    fn, // from Napi::CallbackInfo
    "AsyncConnector",
    0,
    1,
    [this](auto){
        log << "release\n";
        if (connectThread.joinable())
            connectThread.join();   
    }
);

// Why? because calling this function twice is impossible without, explaination below.
if (connectThread.joinable())
    connectThread.join();

connectThread = std::thread{[this, addr, sid, channel](){
    auto callback = [this]( Napi::Env env, Napi::Function jsCallback) {
        jsCallback.Call( {Napi::String::New( env, "success" )} );
    };
    log << "blocking call\n";
    // If I comment this out, my issue vanishes.
    std::this_thread::sleep_for(2s);
    // for(int i = 0; != 100; ++i) // adding this loop just does that the callback doesnt get called,
    // but gets called 200 times the entire function is called again.
        asyncConnect.BlockingCall(callback);
    log << "blocking call over\n";
    asyncConnect.Release();
}};
The callback is never called and the "release" string is never found in the log.
Only when I call the entire function again The callback gets called, but twice!
This is what the log shows:
blocking call
blocking call over
blocking call
blocking call over
release
release

And I have no idea why.
When I add another TSFN that I call every something milliseconds, I can force the callback to be called.
Is there something I am missing about how asynchronous function calling works?
I am using electron 7.1.2 (newest) & react btw, This might be very relevant.
That should be node 12.8.X and I am using node-addon-api 2.0.0

Duplicate issue #784: Thread-safe Functions weird issue on windows, not on linux or mac
Created: 2020-08-05T17:10:16Z
Hi,
I'm trying a simple Thread-safe Function example,
https://napi.inspiredware.com/special-topics/thread-safe-functions.html
This example works as expected in mac and Linux platforms. But not in windows.
auto callback = [](Napi::Env env, Napi::Function jsCallback, int *data) {
    jsCallback.Call({Napi::Number::New(env, *data)});
  };

App config
Electron app with React and webpack.
Problem
jsCallback.Call
not gets called sometimes.
Works ok on windows when

Run with node (eg : node app.js)
Run inside Electron app without react

Does not work on windows when
Run inside a react js code in an Electron app
I'm using native-ext-loader (https://www.npmjs.com/package/native-ext-loader) to load the native node module in webpack  rules.
     rules :[ {
        test: /\.node$/,
        loader: "native-ext-loader",
        options: {
          rewritePath: path.resolve(__dirname, "../build/Release")
        }
      },

Closing comment (2020-08-08T04:29:29Z): I'm also running into this issue with an Electron (version 9.1.2) app on Windows. I have a C++ module running some async processes. My JS code registers a callback function so it can be notified when the native code state changes. On the C++ side this is done with a ThreadSafeFunction. I haven't had any problems in macOS, but the JS callback is unreliable on Windows. Incidentally I am also using React (and Redux).
The behavior I'm seeing is similar to #622. It seems that ThreadSafeFunction calls sometimes queue up and don't get processed for a long time. Eventually, and seemingly at random, a new ThreadSafeFunction call will break the logjam, and all previously queued TheadSafeFunction calls will be processed.