I have studied about the event loop in Node.Js, it work in asynchronous and non-blocking manner to process the request. Is there any way so that we can block the execution of event loop?
-
Yes - have a synchronous code that doesn't exit. That will block it. – VLAZ Apr 22 '20 at 06:05
-
Why would you ever want to? – Lasse V. Karlsen Apr 22 '20 at 06:33
-
3@LasseV.Karlsen - It's more likely one wants to know what might block the event loop so you can avoid doing so. – jfriend00 Apr 22 '20 at 06:37
2 Answers
There are lots of ways to block the event loop. Some ways block it just for awhile (like using synchronous file I/O) and some ways block it forever.
For example, this blocks it forever:
let flag = false;
setTimeout(() => {
// this callback never gets called
// because event loop is blocked
flag = true;
}, 1000);
while (!flag) {
console.log("still waiting")
}
// never get here
The issue is that the while()
loop runs until the flag
changes value. As long as that while loop is running, the event loop is blocked. There's a setTimeout()
that wants to fire in 1 second, but it can't actually call its callback until the interpreter gets back to the event loop. But, it won't get back to the event loop until the while()
loop is done. It's a deadlock which results in an infinite loop and the event loop is permanently blocked.
The setTimeout()
can't call its callback until the while
loop is done and the while
loop won't finish until the setTimeout()
runs its callback. Deadlock, infinite loop.
This blocks it for awhile while all the file operations are going on and all the processing of the files:
setTimeout(() => {
// this doesn't get to run until all the synchronous file I/O
// finishes in the code below, even though the timer is set
// for only 10ms
console.log("finally got to run the timer callback");
}, 10);
let files = some array of files;
for (let file of files) {
let data = fs.readFileSync(file);
let lines = data.split("\n");
for (let line of lines) {
// do something
}
}

- 683,504
- 96
- 985
- 979
Event loop runs on a thread so any call that blocks the thread, like an infinite loop, or a sync file operation, or a blocking network call, will block the event loop.
You can imagine the event loop as first in first out callback queue, if one function call is stuck, subsequent calls will wait for it to finish.
You can also block the event loop by infinitely queuing a new task to the end using setTimeout or setInterval functions.

- 10,486
- 4
- 39
- 44
-
http call doesn't block the thread in node js, at least "from the box" – Alexander Danilov Nov 18 '21 at 16:27
-
@AlexanderDanilov I wasn't talking about node's http module but a network call. – snnsnn Nov 18 '21 at 17:24
-
-
@AlexanderDanilov What do you mean by crappy? The warning is for not because the code is crappy, it is because blocking a server is a serious issue with certain ramifications. But, that is not the point, the point is you can block on a network call. Again, I am not talking about Node's net module but in general. Just for assurance please look for the statement "then read calls will block indefinitely" on this page https://doc.rust-lang.org/stable/std/net/struct.TcpStream.html. Sorry I did not get the part 'I wrote "from the box"'. – snnsnn Nov 19 '21 at 13:28
-
So now after your edit it seems to be a duplicate of @jfriend00 answer – Alexander Danilov Nov 19 '21 at 17:24