execSync icon indicating copy to clipboard operation
execSync copied to clipboard

Some callbacks to `child_process.exec` are never called

Open joshterrell805 opened this issue 10 years ago • 1 comments

If execSync.exec is used before node's child_process.exec, the exec callback to child_process.exec is never called.

This only happens when multiple calls to execSync and child_process.exec are invoked one after the other.

Example script to demonstrate the issue:

 var exec = require('child_process').exec,
     execSync = require('execSync');

 var executions = 5;

 var execFunctions = [];

 for (var i = 0; i < executions; ++i) {
    addExecFunction(i);
 }

 var completedExecutions = 0;
 execFunctions.forEach(function(execFunction) {
    execFunction(function(err, val) {
       if (++completedExecutions == executions) {
          console.log('all complete!');
       }
    });
 });

 function addExecFunction(i) {
    execFunctions.push(function(callback) {
       console.log(i + ' start');
       var pwd = execSync.exec('cd && pwd').stdout.replace('\n', '');
       //var pwd = '~';
       var command = 'ls -l ' + pwd;
       //console.log(command);
       exec(command, function onProcessTermination(err, stdout, stderr) {
          console.log(i + ' complete');
          callback(null, i);
       });
    });
 }

output:

$ node testExec.js
0 start
1 start
2 start
3 start
4 start
3 complete
4 complete

When commenting out var pwd = execSync.exec(...);, and uncommenting var pwd = '~'; the script works fine.

PS: I'm running this on a linux box.

joshterrell805 avatar Sep 16 '14 20:09 joshterrell805

This is pretty serious. This module effectively breaks the process.exec() function when used.

danielbeardsley avatar Sep 22 '14 18:09 danielbeardsley