gf3 / sandbox

A nifty JavaScript sandbox for Node.js
ISC License
844 stars 123 forks source link

Concurrency problem: child.exit occurs before final child.stdout.data #25

Closed iameli closed 10 years ago

iameli commented 10 years ago

For [grunt-ngbuild](), I am spawning lots of Sandboxes in parallel to parse files. I was frequently getting this error:

SyntaxError: Unexpected end of input
    at Object.parse (native)
    at ChildProcess.<anonymous> (/Users/eli/code/grunt-angular-builder/node_modules/sandbox/lib/sandbox.js:27:34)
    at ChildProcess.EventEmitter.emit (events.js:98:17)
    at Process.ChildProcess._handle.onexit (child_process.js:797:12)

which I tracked down to an empty stdout on sandbox.js:27. Apparently that "exit" event is being called before the preceding stdout event, so we're trying to call the callback before we've gotten the data from the exiting program. I'm not sure why this happens; maybe it's a node bug. (I don't know node's child process even model biz well enough to diagnose, anyway). It never ever happened when I wasn't spawning a bunch of processes in parallel (using async.eachSeries instead of async.each).

In any event, the problem is fixed as long as I wrap the hollaback call in a setImmediate. Pull Request incoming shortly.