0

I have a question about how server.listen method keep node process running. Is there any setInterval method inside?

I have read answer in post How does `server.listen()` keep the node program running. But still didn't understand it.

Anyone know please explain to me. Thanks.

Quoc Van Tang
  • 1,075
  • 4
  • 15
  • 33

2 Answers2

4

Node.js internally in libuv has some sort of counter of the number of open resources that are supposed to keep the process running. It's not only timers that count here. Any type of open TCP socket or listening server counts too as would other asynchronous operations such as an in-process file I/O operations. You can see calls in the node.js source to uv_ref() and uv_unref(). That's how code internal to node.js marks resources that should keep the process running or release them when done.

Whenever the event loop is empty meaning there is no pending event to run, node.js checks this counter in libuv and if it's zero, then it exits the process. If it's not zero, then something is still open that is supposed to keep the process running.

So, let's supposed you have an idle server running with a listening server and an empty event loop. The libuv counter will be non-zero so node.js does not exit the process. Now, some client tries to connect to your server. At the lowest level, the TCP interface of the OS notifies some native code in node.js that there's a client that just connected to your server. This native code then packages that up into a node.js event and adds it to the node.js event queue. That causes the libuv to wake up and process that event. It pulls it from the event queue and calls the JS callback associated with that event, cause some JS code in node.js to run. That will end up emitting an event on that server (of the eventEmitter type) which the JS code monitoring that server will receive and then JS code can start processing that incoming request.

So, at the lowest level, there is native code built into the TCP support in node.js that uses the OS-level TCP interface to get told by the OS that an incoming connection to your server has just been received. That gets translated into an event in the node.js event queue which causes the interpreter to run the Javascript callback associated with that event.

When that callback is done, node.js will again check the counter to see if the process should exit. Assuming the server is still running and has not has .unref() called on it which removes it from the counter, then node.js will see that there are still things running and the process should not exit.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks for your clearly explanation @jfriend00. So your mean is whenever it has a server bound to a port number. The libuv counter will be a non-zero number. And even if there are no any job to do in the task queue (event loop queue), there is still an addition check on the libuv counter to determine process exit or not. – Quoc Van Tang Oct 12 '18 at 05:31
  • @QuocVanTang - Yes, that is correct. The same with timers or open TCP connections. They will all keep the process for exiting automatically. If you don't want one of them to keep the process from exiting, you can call the `.unref()` method on the socket or server and it will no longer be counted in the libuv counter. – jfriend00 Oct 12 '18 at 06:21
  • Thank you, can I ask you one more question about the event loop. I've read your response in another post about this but still have confused. Is event loop a while loop, so if event loop and the userland code stand in 1 thread, why doesn't it block the main code? when it start, it start when to process start or when the call stack is empty. Does it have a interval gap time between each cycle of the loop? – Quoc Van Tang Oct 12 '18 at 07:12
  • @QuocVanTang - The event loop is actually fairly involved with multiple queues for different types of events and different priorities for different events. It is not a simple `while` loop. In fact, when there's nothing to do, it will sleep until something is added to the queue. Because the Javascript in node.js runs as single threaded, an event is pulled from the event queue, a callback associated with that is called, that Javascript runs and no more events are processed until that Javascript returns and then the system checks for the next event in the queue and repeats. – jfriend00 Oct 12 '18 at 07:21
  • So is it periodically run to check whether call stack empty and push event from queue to the stack? I know it includes 6 phase in 1 cycle of loop, is there a meantime between 2 cycle of loop? Do you have skype, I would like to hear your explanation clearly about this :D – Quoc Van Tang Oct 12 '18 at 07:28
  • @QuocVanTang - You're asking far beyond this question. If you want to know more about the event loop, then please be specific about what you want to know, search for prior answers on the topic and if you don't find what you want to know, then post a new question. Stack overflow should not be used to continually ask more and more of the person who provided you an answer. One question. Several answers. For a new topic or more info on that topic, post a new question. – jfriend00 Oct 12 '18 at 07:33
  • @QuocVanTang - It doesn't have to poll to see when the stack is empty. It tells the JS interpreter to run this Javascript callback and the JS interpreter will return back to the event loop code when it's done running that code and that's how the event loop knows when it's done. – jfriend00 Oct 12 '18 at 07:34
0

It's running through the event loop.

Every time event loop is looking for any pending operation, the server.listen() operation come forward.

Shams Nahid
  • 6,239
  • 8
  • 28
  • 39