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

Delay in handoff from C++ to Node awaiter #123

Open
hiteshlala opened this issue Nov 22, 2018 · 0 comments
Open

Delay in handoff from C++ to Node awaiter #123

hiteshlala opened this issue Nov 22, 2018 · 0 comments

Comments

@hiteshlala
Copy link

The addon I am building will be used on the server ( Koa ) side of a Node application.

This is an asynchronous library where the addon functions receive some arguments and a callback function.

In the server I wrap these addon functions in a promise and then use them in async awaits.

I discovered that when called this way in a server setting there is a delay between when the callback is called and the server picks up on the completion. The delay is significant - seconds.

This behavior does not show up outside of a server - i.e. if I just run a script calling the same functions.

I narrowed down where the delay is happening by logging times at different points in the functions ( both C++ side and Node side ).

In the following code the delay is happening after the resolve is being called.

  function asyncAddOnFuncCall( time ) {
    return new Promise(( resolve, reject ) => {
      addon.longRunFunction( time, ( err, res ) => {
        if( err ) {
          reject( err );
        }
        else {
          resolve( res );
        }
      });
    });
  }

I then created similar addons using Nan and also just the regular V8 way.

I found that this behavior was not present in Nan but was in V8.

I found the same behavior using Node version 8 and 10.

Next I tested using a callback method in the server code itself ( no promises or async awaits ) using Express.

In this configuration I found that there were no delays in any of the three addons that were built.

My colleague then discovered that by wrapping the callback body in the promise form inside a setTimeout() removes all delays.

Like this:

  function asyncAddOnFuncCall( time ) {
    return new Promise(( resolve, reject ) => {
      addon.longRunFunction( time, ( err, res ) => {
        setTimeout( () => {
          if( err ) {
            reject( err );
          }
          else {
            resolve( res );
          }
        }, 0 );
      });
    });
  }

So here is my question, is this a bug in the way I wrote the addon or in the manner in which I am using them?

I have created a repo which has simplified async addons in it along with a server that can be run that demonstrates this issue.

There are three addons in this repo each doing the same thing but written for Nan, Nbind, and V8 ways.

The Koa server uses promises and the Express server callbacks.

node-addon-example

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

1 participant