What happens if one thread crashes in a process?

Problem

I’ve two noob questions:

1) Why node.js is being run in a single thread? why not it just uses the process itself? OR What are the advantages of running it in a thread instead of process?

2) What happens if a thread in a process crashed (I am aware that, un-handled exception will cause process to exit), but what happens if its properly catch’d ? or with exceptions like Floating point exceptions. will the process also crashes?

Problem courtesy of: rin

Solution

I think you’re a bit confused about what “single threaded” means in this case. Node.js itself utilizes multiple threads in the background to handle otherwise blocking calls in an asynchronous manner.

Node.js core, the JavaScript engine, is single threaded. The lack of multi threading and the availability of an event loop makes programming a whole lot easier. In short, you don’t have to deal with all the concurrency issues that multi threading brings with it your code is a whole lot more deterministic this way.

In case a thread crashes it’s completely irrelevant whether this was the only one or one of many, if code crashes the best thing you can hope for is that the program dies with it, nothing would be more troublesome than having you program continuing to run with partly corrupted state. Imagine the thread that crashes was responsible for saving data to disk, and now you program continues to run without ever saving again.

Solution courtesy of: Ivo Wetzel

View additional discussion.