Skip to content
Advertisement

Precedence of setImmediate() and setTimeout() callbacks vs I/O ones

The Node js event loop implemented by libuv consists of the certain phases. The poll phase is meant for blocking waiting for I/O tasks with subsequent running its callbacks. The duration of the waiting depends on timers timeouts and the presence of callbacks registered through setImmediate(). For example, if we have a setImmediate() callback the duration is set to 0.

What if at the moment we are entereing inside the poll phase there are already some I/O events? Should their callbacks execute immediately without a view of callbacks registered with setTimeout() or setImmediate()?

The docs:

When the event loop enters the poll phase and there are no timers scheduled, one of two things will happen:

  • If the poll queue is not empty, the event loop will iterate through its queue of callbacks executing them synchronously until either the queue has been exhausted, or the system-dependent hard limit is reached.

  • If the poll queue is empty, one of two more things will happen:

    a. If scripts have been scheduled by setImmediate(), the event loop will end the poll phase and continue to the check phase to execute those scheduled scripts.

    b. If scripts have not been scheduled by setImmediate(), the event loop will wait for callbacks to be added to the queue, then execute them immediately.

It seems setImmmediate() callbacks would run after I/O (correct me if I’m wrong, please) but what does “there are no timers scheduled” mean? What happens otherwise?

Advertisement

Answer

What if at the moment we are entereing inside the poll phase there are already some I/O events? Should their callbacks execute immediately without a view of callbacks registered with setTimeout() or setImmediate()?

As I understand it, yes. If the event loop reaches the poll phase it will give priority to “I/O callbacks” over any kind of timer.

It’s covered in the documentation for the timers phase:

Timers callbacks will run as early as they can be scheduled after the specified amount of time has passed; however, Operating System scheduling or the running of other callbacks may delay them.

As for:

but what does “there are no timers scheduled” mean? What happens otherwise?

I believe what the documentation is trying to convey here is that the poll phase will wait for events to happen if there are no timers ready to be called.

No timers ready, empty poll queue:

  • If scripts have not been scheduled by setImmediate(), the event loop will wait for callbacks to be added to the queue, then execute them immediately.

Timers ready, empty poll queue:

  • Once the poll queue is empty the event loop will check for timers whose time thresholds have been reached. If one or more timers are ready, the event loop will wrap back to the timers phase to execute those timers’ callbacks.

But the distinction does feel kind of redundant or at least I didn’t feel that distinction necessary.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement