1

I was demonstrating the working of event queue to an interviewer and suddenly he asked me what if there was a long running process on the call stack? Would that hang Node.js forever?

To the best of my knowledge, it should and that's what I told him as I had some experience in writing some bad infinite loops which froze my Chrome and all.

Then he countered me by asking if it was a desirable feature to have in a platform which is being used by thousands of companies out there for production systems? To be honest, I was stumped.

Can anyone explain?

Rajat Saxena
  • 3,834
  • 5
  • 45
  • 63
  • Its simple, any browser basically does have a "kill tab" feature which kills the specific tab. So now even if you have got the chrome hung, you can kill the specific tab. Which in turn only hampers your webpage and let other pages/tabs run just fine. – binariedMe Mar 06 '19 at 09:33
  • There are always a way to hang a server/process/connection like if you do a long/circular db query and if there is no connection timeout, you get the db hung... However you can have connection timeout/query timeout and then fix it. So its nothing to be afraid of I think. – binariedMe Mar 06 '19 at 09:35
  • But what would you do if you had a public Node.js server and someone submits a long running process like a very large file upload and all? How would one handle such cases in production? – Rajat Saxena Mar 06 '19 at 09:37
  • there's always a restriction on the size of a file to be uploaded in these cases and you can handle the same in other scenarios as well. take a look at https://stackoverflow.com/questions/21708208/express-js-response-timeout. – AZ_ Mar 06 '19 at 09:41
  • I thought you are talking from other websites's user's perspective. There are always many ways to protect your own server against such actions e.g. limiting file-size (everyone does it and not specific to node.js), request timeout (again not specific to node.js), leveraging events, promises and streams which won't block other execution as they are async. – binariedMe Mar 06 '19 at 09:42
  • I don't think that if you upload a file and it takes 5 minutes, Node gets stuck and hangs for 5 minutes. I can't imagine a file upload being thread-blocking. – Jeremy Thille Mar 06 '19 at 09:46
  • I just realized that there are two votes to close this question. Is it really below SO's standard? Is it not a valid question? Is it opinion based and not based on facts? – Rajat Saxena Mar 06 '19 at 14:27

1 Answers1

2

For your question: yes, node can be hang by putting a long running proccess on event queue.

You can read some information here: https://nodejs.org/en/docs/guides/dont-block-the-event-loop/

In default, Node have 1 main thread to run event loop and 4 worker thread to run I/O, OS, async task. So if your code take too long to run in the event loop, your app will look like be hang.

For example:

function doWork(duration) {
    const start = Date.now();
    while (Date.now() - start < duration) {
    }
}

app.get('/', (req, res) => {
    doWork(10000);
    res.send('Hello world');
})

app.get('/hi', (req, res) => {
    res.send('Hi');
});

In this case, if your request to first router and request to 2nd router after. The 2nd request will have to wait for 1st request to complete before send the response. So it will be hang on 10 second.

For worker case, if all 4 workers still haven't done their tasks and 5th tasks coming, it will have to wait until one of the worker finish. So if 4 workers take forever to run their job, the 5th job will never be done. You can increase the number of workers but it have down side too. Each maybe take longer to finish their task.

So for Node, it shouldn't work with CPU intensive task, instead it should work for something like API Gateway, handling I/O task, etc... Everything has each own used and depend on situation, we can have solution to resolve. In your upload file case, we can have some limitation for the file upload or your app Node just used for verify data, handshake for other app to upload file instead.

For thousands companies, with their own IT department, I think they already know the advantage and disadvantage to use Node in their case.

DarknessZX
  • 686
  • 3
  • 12