Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Node child process that I created via spawn() hanging?

Tags:

node.js

spawn

I am using spawn() to make a git call. Sometimes it works fine but others it appears to be hanging. I see no events firing (error, exit, close) yet I see evidence that the process did in fact complete successfully.

var spawn = require('child_process').spawn;

spawn('git', ['push', 'origin', 'master'])
  .on('error', function(error) {
    console.log("ERROR: DETAILS: " + error);
  })
  .on('close', function(code) {
    console.log("SUCCESS: CODE: " + code);
  })
  .on('exit', function(code) {
    console.log("EXIT: CODE: " + code);
  })
like image 398
Tom Avatar asked Dec 26 '13 23:12

Tom


1 Answers

As it turns out once the stderr buffer exceeds 24kb you must be reading from it or you not see any events for completion. Possible workarounds:

  1. Set the stdio option on the spawn call.

    spawn('git', ['push', 'origin', 'master'], {stdio: 'ignore'});
    

    See Node ChildProcess doc for all of the possibilities - there are lots.

  2. Add an on(data) handler.

    var git = spawn('git', ['push', 'origin', 'master']);
    ...
    git.stderr.on('data', function(data) {
      // do something with it
    });
    
  3. Pipe it to stdout / stderr. This may be too verbose for your application but including it for completeness.

    var git = spawn('git', ['push', 'origin', 'master']);
    ...
    git.stderr.pipe(process.stderr);
    git.stdout.pipe(process.stdout);
    
like image 78
Tom Avatar answered Nov 15 '22 21:11

Tom