recursive/nested ticks

108 views
Skip to first unread message

eghteen...@gmail.com

unread,
May 3, 2014, 7:02:32 PM5/3/14
to nod...@googlegroups.com
Can recursive/nested ticks happen in node? I mean functions registered with process.nextTick()? Is this possible in user land or node-core only?

i'm trying to make sense of the below code from node.cc:

if (tick_info->in_tick()) {

    return ret;

  }

 

  …

 

  tick_info->set_in_tick(true);

 

  env->tick_callback_function()->Call(process, 0, NULL);

 

  tick_info->set_in_tick(false);

 

thanks!

Forrest Norvell

unread,
May 3, 2014, 11:44:40 PM5/3/14
to nod...@googlegroups.com
Replies inline:

On Sat, May 3, 2014 at 4:02 PM, <eghteen...@gmail.com> wrote:
Can recursive/nested ticks happen in node? I mean functions registered with process.nextTick()? Is this possible in user land or node-core only?

When you're writing application code, the vast majority of the time you want to use setImmediate instead of process.nextTick, because setImmediate does a better job of yielding V8 time to pending I/O operations. That said, there's no reason you can't call either of setImmediate or process.nextTick from within the callback you provide to setImmediate or process.nextTick. In both cases, the provided callback will be added to a queue and handled as soon as possible, given what "as soon as possible" means, given a particular operation. For process.nextTick, that means that all of the pending process.nextTick callbacks (included those added "recursively") will be run before the next turn of the event loop.

Because this can starve I/O or other low-level tasks waiting to be run by V8, in older versions of Node you will get an error or warning when the number of callbacks scheduled exceeds a certain depth, which is controlled by process.maxTickDepth (which defaults to 1000). Note that both process.nextTick and setImmediate are defined entirely in the JavaScript side of Node, which leads me to believe that your question below is about something different from process.nextTick entirely. Also note that how this is handled varies between stable minor versions of Node, and in 0.11 / 0.12 Trevor Norris modified it to get rid of the maximum tick depth altogether, which makes this possibly kind of a footgun if you're not very careful about how you use it. Again: process.nextTick is rarely what you want unless you're doing fancy, low-level things. setImmediate is the preferred operation most of the time.

i'm trying to make sense of the below code from node.cc:

if (tick_info->in_tick()) {

    return ret;

  }

 

  …

 

  tick_info->set_in_tick(true);

 

  env->tick_callback_function()->Call(process, 0, NULL);

 

  tick_info->set_in_tick(false);


This guard exists to make sure that all of the queued callbacks are handled properly within the JavaScript side of Node (as there are additional layers of core functionality, e.g. domains or asyncListeners, that need to be wrapped around callback execution). If you're trying to do something fancy with event loop manipulation in an add-on, you probably need to figure out how timers and process.nextTick are implemented in src/node.js, which I'll admit is pretty hairy. Either way, this should only affect you in certain very limited circumstances. What exactly are you trying to do?

F

eghteen...@gmail.com

unread,
May 7, 2014, 12:09:43 PM5/7/14
to nod...@googlegroups.com
thank you Forrest for reply; it did cleared up a few things for me. 
i hope to continue this conversation on https://github.com/joyent/node/issues/7555 
thx
Reply all
Reply to author
Forward
0 new messages