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

process.nextTick() never calls its callback #14425

Closed
losfair opened this issue Jul 22, 2017 · 7 comments
Closed

process.nextTick() never calls its callback #14425

losfair opened this issue Jul 22, 2017 · 7 comments
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. process Issues and PRs related to the process subsystem.

Comments

@losfair
Copy link

losfair commented Jul 22, 2017

  • Version: v8.2.1
  • Platform: Ubuntu 16.10

When a function is invoked in the uv_async_send handler context and calls process.nextTick(), the callback provided to process.nextTick() is never called. setImmediate() works fine.

native.cc:

#include <node.h>
#include <uv.h>
#include <thread>

using namespace v8;

Persistent<Function> *fn = NULL;
uv_async_t uv;

static void fire(const FunctionCallbackInfo<Value>& args) {
    Isolate *isolate = args.GetIsolate();
    
    Local<Function> _cb = Local<Function>::Cast(args[0]);
    fn = new Persistent<Function>(isolate, _cb);

    uv_async_init(uv_default_loop(), &uv, [](uv_async_t *_) {
        Isolate *isolate = Isolate::GetCurrent();
        HandleScope currentScope(isolate);

        Local<Function> cb = Local<Function>::New(isolate, *fn);
        Local<Value> argv[] = {};

        cb -> Call(Null(isolate), 0, argv);
    });

    std::thread([]() {
        uv_async_send(&uv);
    }).detach();
}

static void init(Local<Object> exports) {
    NODE_SET_METHOD(exports, "fire", fire);
}

NODE_MODULE(native, init);

test.js:

const native = require("./build/Release/native");

let fn = id => {
    console.log(`Into ${id}`)
    process.nextTick(() => {
        console.log(`Next tick: ${id}`);
    });
};

let a = () => fn(1);
let b = () => fn(2);

a();
console.log("Called: a");
native.fire(b);
console.log("Called: b");
setTimeout(() => process.exit(0), 5000);

node test.js output:

Into 1
Called: a
Called: b
Next tick: 1
Into 2
@mscdex mscdex added process Issues and PRs related to the process subsystem. v8.x labels Jul 22, 2017
@bnoordhuis
Copy link
Member

What happens when you use node::MakeCallback() instead of v8::Function::Call()?

@Fishrock123 Fishrock123 added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jul 22, 2017
@losfair
Copy link
Author

losfair commented Jul 23, 2017

It seems to work with node::MakeCallback().

@losfair losfair closed this as completed Jul 23, 2017
@c4milo
Copy link

c4milo commented Sep 5, 2017

@bnoordhuis would you mind elaborating about what's going on internally? I just got a similar situation reported at c4milo/node-inotify#67 and would like to understand better.

@addaleax
Copy link
Member

addaleax commented Sep 5, 2017

@c4milo When you use Function::Call, that’s really just a plain function call, and Node has no way to know that it should call the functions attached using process.nextTick(). MakeCallback(fn) does the same things as fn->Call(), but it additionally takes care of resolving promises and running the nextTick queue.

@c4milo
Copy link

c4milo commented Sep 5, 2017

I see, that makes sense @addaleax! Thanks!

So, since I'm using NAN, should I use Nan::Call instead? nodejs/nan#284 (comment)

@addaleax
Copy link
Member

addaleax commented Sep 5, 2017

@c4milo I am pretty sure those problems have long been resolved – if you call into Node asynchronously, MakeCallback (or Nan::MakeCallback) are the right things to do :)

@c4milo
Copy link

c4milo commented Sep 5, 2017

@addaleax thanks again, I've been out of the loop regarding node/v8 changes for a while. This is very helpful <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. process Issues and PRs related to the process subsystem.
Projects
None yet
Development

No branches or pull requests

6 participants