As per my understanding of Node run time environment I think If both call stack and event loop queue are empty it indicates there is no code (function) currently executing so Node will keep waiting for a process to execute some code for example if Node is being used as a http server it might be waiting for an API request to hit the TCP socket pipeline created by libuv.
Asked
Active
Viewed 912 times
0
-
If there are no more event *sources* node will exit. It doesn't matter if any particular event queue is empty or not. E.g. something reading or listening on a socket counts as a potential source of events, whether there have actually been any events or not. – Wyck Oct 16 '22 at 14:41
-
Thanks @Wyck for the response but I have a slight doubt in case when Node is acting as a HTTP server and there is no requests coming in so at that time will the call stack be empty or there is some base process which will always be present until exit ? – Navitas28 Oct 16 '22 at 14:44
-
1Has absolutely nothing to do with the call stack. The"base process" as you call it (underlying framework) is *libuv* which keeps track of any potential event sources. The act of listening for connections is an underlying pending io operation. Doesn't matter if a connection happens or not. Just the act of listening keeps the process alive. – Wyck Oct 16 '22 at 14:47
-
This is probably a duplicate of [When does Nodejs process quit?](https://stackoverflow.com/questions/19699619/when-does-nodejs-process-quit) – Wyck Oct 16 '22 at 14:57
-
1@Navitas28 When there's no more requests coming in does not mean the event queue is empty. There is still the socket listening event that is pending. That will continue to be an event waited upon until you close the socket (or until you exit the program) – slebetman Oct 16 '22 at 14:58
-
Clarification: Re-reading my first comment, it's slightly misleading, sorry. Node exits when there are no more event sources **and** no more queued events. Just the fact that there are no queued events alone does not cause it to exit. It will drain the event queues before exiting if all event sources go away while an event queue is non-empty. – Wyck Oct 16 '22 at 14:59