Search code examples
c++node.jswindowsexceptionperformance-testing

Problems with monitoring processes launched by child_process spawn on Windows


Mein purpose is simply to launch an exe using node.js. In case the application is unexpectedly terminated, the exception should be catched and logged in a protocol. An exception is thrown thru the following statement in a c++ project:

throw std::string("NOT X or x!!!");

My Javascript is:

var spawn = require('child_process').spawn;
var cp = spawn(process.env.comspec, ['/c', 'myExcepTest.exe', '', '']);

// **doesn't work**
cp.on('uncaughtException', function(err){
    console.log("Caught exception: " + err);
});
cp.stderr.on('error', function(err) {
    console.log(err.toString());
});
cp.stdin.resume();
cp.stdin.setEncoding('utf8');
cp.stdin.on('data', function(data) {
    cp.stdout.write(data);
});

// **does work**
cp.on('exit', function(code){
    console.log("Child exited with code: " + code);
});
cp.stdout.on('data', function(data) {
    console.log(data.toString());
});

My questions are:

  1. Is it a good practice to use node.js to monitor a process on windows?

  2. Is it possible, to use node.js to catch an exception on windows?

  3. How to correctly pipe the stdin to stdout in a node.js script?

Sorry for my poor English ;)


Solution

    1. Depends on why you're monitoring the process I suppose.
    2. There is no uncaughtException for child processes. The reason being is that there is no standard way for catching such things, considering the child process could be any kind of program.
    3. You can't pipe the stdin of a child process to its own stdout, because stdin is a Writable stream and is an input to the child process (not an output from it), so stdin will never have data events.