Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDelay in handoff from C++ to Node awaiter #123
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:
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