Created
August 2, 2012 04:38
-
-
Save garthk/3233620 to your computer and use it in GitHub Desktop.
Node.js does not detect process.stdout pipe closing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this terminates: yes | head | |
// this hangs: node node-yes-simple.js | head | |
// per mhart, this is entirely expected | |
while(true) { | |
process.stdout.write('{"yes": "yes"}\n'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Keep saying yes until interrupted or someone closes your output pipe: | |
var running = true; | |
function say_yes() { | |
if (running) { | |
process.stdout.write('{"yes": "yes"}\n'); | |
process.nextTick(say_yes); | |
} else { | |
process.exit(0); | |
} | |
} | |
process.stdout.on('error', function(err) { | |
running = false; | |
if (err.code === 'EPIPE') { | |
// expected if output pipe closed, e.g. by `head` | |
process.exit(0); | |
} else { | |
process.stderr.write(JSON.stringify(err) + '\n'); | |
process.exit(1); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated. Thanks!