Calling a callback from a native addon asynchronously #1247
Copy link
Copy link
Closed
Labels
c++Issues and PRs that require attention from people who are familiar with C++.Issues and PRs that require attention from people who are familiar with C++.
Description
I have a native module that does some threading and calls a javascript callback asynchronously using libuv. It used to work in node but the same code in iojs now fails. Specifically Isolate::GetCurrent() is returning null.
Isolate* isolate = Isolate::GetCurrent();
Local<Context> context = Context::New(isolate); // throws
Context::Scope context_scope(context);
I'm using libuv to put the callback into the same thread as before, and again, this used to work in node. Does anyone have any suggestions about what may be different in iojs?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
c++Issues and PRs that require attention from people who are familiar with C++.Issues and PRs that require attention from people who are familiar with C++.
Activity
[-]Calling a callback from a native module asynchronously[/-][+]Calling a callback from a native addon asynchronously[/+]trevnorris commentedon Mar 24, 2015
Have a small test case we can help you with?
justinmchase commentedon Mar 25, 2015
Thanks for the reply. I have been working on getting the repro down to the simplest form and I believe it is not actually an iojs bug :)
It turns out we are actually using http://nwjs.io, which runs with a particular build of iojs. When I run my repro from the commandline iojs it works fine, from inside of nwjs it crashes in the async callback after a few milliseconds. Meaning it works for a little while but it seems like the Isolate or Context is being garbage collected out from under us.
But anyway, I do have another question. How are people actually building native modules for iojs? In node you would use
node-gyp, but that doesn't appear to actually build iojs compatible binaries. I had to fork and alter node-gyp to get it to work with iojs so it leaves me wondering how others are actually building their modules.Also, I'm wondering if I'm doing it wrong and it just happens to work from the command line version accidentally? Here is my code I would appreciate any feedback:
The Async class is very similar to this one:
https://github.com/mapbox/node-sqlite3/blob/master/src/async.h
Then to call it from iojs:
This works as expected, but the exact same module compiled with
nw-gyprunning in nwjs fails in the functionAsyncCallbackafter a few milliseconds. I will follow up with them there but I'm especially wondering about the call toisolate-GetCurrentContext(). Is this the right way to get the isolate and context when coming from an async callback?Thanks.
justinmchase commentedon Mar 25, 2015
I managed to figure it out. After adding a fatal error handler:
I managed to get this error out of it:
Which lead me to realize I needed to create a
HandleScopein my async callback and that is what fixed it.Should actually be:
Thanks.
trevnorris commentedon Mar 25, 2015
Ah yes. That'll get you every time. Can't forget those
HandleScopes :)