Skip to content

Instantly share code, notes, and snippets.

@niallo
Created October 10, 2012 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niallo/3868709 to your computer and use it in GitHub Desktop.
Save niallo/3868709 to your computer and use it in GitHub Desktop.
//
// Node Child Process Spawn() race bug
//
// Triggerable on:
// Node 0.8.11 / OS X 10.8.2 / MBP 13" 2011 (4 cores)
// Doesn't seem to happen on:
// Node 0.8.11 / Ubuntu 10.04 LTS / EC2 Medium (1 core)
var assert = require('assert')
var spawn = require('child_process').spawn
function runTest(cb) {
var proc = spawn("/usr/bin/env", [], {env:{foo:"bar"}})
proc.stdoutBuf = ""
proc.stderrBuf = ""
proc.stdmergedBuf = ""
proc.stdout.on('data', function(buf) {
proc.stdoutBuf += buf
proc.stdmergedBuf += buf
})
proc.stderr.on('data', function(buf) {
proc.stderrBuf += buf
proc.stdmergedBuf += buf
})
proc.on('exit', function(exitCode) {
console.log(proc.stdoutBuf)
assert.notEqual(proc.stdoutBuf.length, 0, "proc.stdoutBuf has length 0!")
if (cb) cb()
})
return proc
}
var i = 0
function cb() {
i++
// Run in a loop until it is triggered
if (i < 3000) runTest(cb)
}
runTest(cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment