-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Calling a callback from a native addon asynchronously #1247
Comments
Have a small test case we can help you with? |
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 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: #include <node.h>
#include "async.h"
using namespace v8;
static Persistent<Function, CopyablePersistentTraits<Function>> _cb;
Async* _async;
void AsyncCallback(void* data)
{
int x = *((int*)data);
auto isolate = Isolate::GetCurrent();
auto context = isolate->GetCurrentContext(); // crashes nwjs here
auto global = context->Global();
const int argc = 1;
Handle<Value> argv[argc];
argv[0] = Number::New(isolate, (double)x);
auto fn = Local<Function>::New(isolate, _cb);
fn->Call(global, argc, argv);
}
DWORD Worker(LPVOID lparam)
{
int x = 0;
while(true)
{
_async->send(&x);
x++;
Sleep(1000);
}
return S_OK;
}
void Start(const FunctionCallbackInfo<Value>& args)
{
auto isolate = Isolate::GetCurrent();
Handle<Function> arg0 = Handle<Function>::Cast(args[0]);
Persistent<Function> cb(isolate, arg0);
_cb = cb;
_async = new Async(&AsyncCallback);
CreateThread(nullptr, 0, Worker, nullptr, 0, 0);
}
void init(Handle<Object> exports, Handle<Object> module)
{
NODE_SET_METHOD(exports, "start", Start);
}
NODE_MODULE(evolve_overlay, init) The Async class is very similar to this one: Then to call it from iojs:
This works as expected, but the exact same module compiled with Thanks. |
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
Should actually be:
Thanks. |
Ah yes. That'll get you every time. Can't forget those |
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.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?
The text was updated successfully, but these errors were encountered: