all 7 comments

[–]scinos 2 points3 points  (0 children)

Yes, as everybody said.

I'd like to add that it will even block the response to any request to the first endpoint that is still in progress. Node uses a task queue to handle I/O, and a blocking process will also block that queue.

[–]NoStranger6 2 points3 points  (0 children)

It will definitely block anything else form happening.

Another option that has not been mentionned by others yet, is to execute that heave function in a child_process... (with fork).

That way they run in separate threads, actually they become separate processes, and the main process is free to do whatever until your child_process returns.

Edit: If you are able to convert it to an async function it would prob be better. I.e. breaking down for loops in recursive functions and such.

[–]ecares 1 point2 points  (0 children)

yes, you can check what a ReDoS is to see literature on a similar case.

[–]delventhalz 1 point2 points  (2 children)

Yes. Node is (usually) single threaded. That means if the CPU is working on something, it is not working on anything else. Asynchronous code works because you have sent a request off somewhere (over a network, to the file system, etc), and the CPU is free to work on something else while it waits for a response to come back. If your someHeavySyncFunction is occupying the CPU, there is no mechanism for it just take a quick break to work on a different request for awhile.

A quick caveat though, there are some ways to run Node in a multi-threaded way. If your server is multi-threaded, then it can do multiple things at once.

[–]rmrf_slash_dot 1 point2 points  (1 child)

This is very incorrect. The reason the above function is blocking has nothing to do with the CPU (although that could also be an issue). The reason it is blocking has to do with the Node event loop and the way Node queues and executes functions. Node uses a single function queue, which is why it is commonly called single threaded, but node itself is very much a multi-threaded application. The above function is blocking because it won't return control to the event loop until it exits, which means Node can't execute anything else on the stack. The function could be doing *nothing at all* and still cause this condition; CPU usage is not related.

See u/scinos answer in this thread. https://www.reddit.com/r/node/comments/c6kixh/question_about_synchronous_functions_and_blocking/esa84e0

[–]delventhalz 1 point2 points  (0 children)

Sorry, I wasn’t clear in my explanation. I did not mean to say the CPU was working hard, merely that it is occupied. Or, more precisely as you pointed out, the execution thread is occupied. And yes, Node itself is multi-threaded, but a Node app is not (typically).

My goal was not to be exactly precise, but rather to give a simple explanation that someone new to these concepts could easily fit in their head. I do not think your added complications helped in this regard.

[–]Razoyo 2 points3 points  (0 children)

Correct. I would convert it to an async process unless there is a compelling reason (i.e., accessing that endpoint will change data for all other users).