Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - Child node process?

Tags:

node.js

I'm using NodeJS to run a socket server (using socket.io). When a client connects, I want am opening and running a module which does a bunch of stuff. Even though I am careful to try and catch as much as possible, when this module throws an error, it obviously takes down the entire socket server with it.

Is there a way I can separate the two so if the connected clients module script fails, it doesn't necessarily take down the entire server?

I'm assuming this is what child process is for, but the documentation doesn't mention starting other node instances.

I'd obviously need to kill the process if the client disconnected too.

like image 779
Hanpan Avatar asked Jun 01 '11 19:06

Hanpan


People also ask

What is Nodejs child process?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

How do you spawn a child process?

Spawned Child Processes. The spawn function launches a command in a new process and we can use it to pass that command any arguments. For example, here's code to spawn a new process that will execute the pwd command. const { spawn } = require('child_process'); const child = spawn('pwd');

Can we create child processes in node applications?

Node provides child_process module which provides ways to create child process.

What is the use of child process?

A child process is a process created by a parent process in operating system using a fork() system call. A child process may also be called a subprocess or a subtask. A child process is created as its parent process's copy and inherits most of its attributes.


2 Answers

I'm assuming these modules you're talking about are JS code. If so, you might want to try the vm module. This lets you run code in a separate context, and also gives you the ability to do a try / catch around execution of the specific code.

You can run node as a separate process and watch the data go by using spawn, then watch the stderr/stdout/exit events to track any progress. Then kill can be used to kill the process if the client disconnects. You're going to have to map clients and spawned processes though so their disconnect event will trigger the process close properly.

Finally the uncaughtException event can be used as a "catch-all" for any missed exceptions, making it so that the server doesn't get completely killed (signals are a bit of an exception of course).

like image 180
onteria_ Avatar answered Oct 16 '22 11:10

onteria_


As the other poster noted, you could leverage the 'vm' module, but as you might be able to tell from the rest of the response, doing so adds significant complexity.

Also, from the 'vm' doc:

Note that running untrusted code is a tricky business requiring great care. 
To prevent accidental global variable leakage, vm.runInNewContext is quite 
useful, but safely running untrusted code requires a separate process.

While I'm sure you could run a new nodejs instance in a child process, the best practice here is to understand where your application can and will fail, and then program defensively to handle all possible error conditions.

If some part of your code "take(s) down the entire ... server", then you really to understand why this occurred and solve that problem rather than rely on another process to shield you from the work required to design and build a production-quality service.

like image 1
Rob Raisch Avatar answered Oct 16 '22 11:10

Rob Raisch