Lost control of child_process when child process outputs long strings

4 views
Skip to first unread message

Aaron Lewis

unread,
Dec 6, 2016, 12:05:33 PM12/6/16
to nodejs
I have a simple program that reads from child output and pass it through websocket

However, when the child output very long string, nodejs quits immediately, no errors, sometimes I can see the 'clean up' message, sometimes I couldn't.

Anyone know why?

var peers = [];

child
= require('child_process').execFile('bash', [
   
'xxxx.sh'
], {
    detached
: true,
    stdio
: [ 'ignore', 1, 2 ],
});


child
.unref();
child
.stdout.on('data', function(data) {
  data
= data.toString();
  peers
.forEach(function(peer) {
    peer
.sendText(data);
 
})
  console
.log('[STDOUT]', data);
});
child
.stderr.on('data', function(data) {
  data
= data.toString();
  console
.log('[STDERR]', data);
});
child
.on('error', function(err) {
  console
.log('error in child process', err);
  process
.exit(1);
});
child
.on('close', function(code) {
  process
.exit(code);
});


function cleanup() {
    console
.log('Cleaning up');
    child
.kill('SIGINT');
}


process
.on('exit',   cleanup);
process
.on('SIGINT', cleanup);


var ws = require("nodejs-websocket")
var server = ws.createServer(function(conn) {
  console
.log("New connection")


  peers
.push(conn);
  conn
.on("text", function(str) {
    console
.log('Received', str);    
 
})
  conn
.on("close", function(code, reason) {
    console
.log("Connection closed");
   
for (var i = 0; i < peers.length; i ++) {
     
if (peers[i] === conn) {
        console
.log('Removed connection #', i);
        peers
.splice(i, 1);
       
break;
     
}
   
}
 
})
}).listen(8300)


Reply all
Reply to author
Forward
0 new messages