SIGPIPE on stdout should kill the process by default
authorRyan Dahl <ry@tinyclouds.org>
Thu, 22 Apr 2010 21:25:13 +0000 (14:25 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 22 Apr 2010 21:25:13 +0000 (14:25 -0700)
src/node.js
test/fixtures/print-10-lines.js [new file with mode: 0644]
test/simple/test-pipe-head.js [new file with mode: 0644]

index 7c5af33..82dd748 100644 (file)
@@ -131,6 +131,9 @@ process.__defineGetter__('stdout', function () {
   if (stdout) return stdout;
   var net = module.requireNative('net');
   stdout = new net.Stream(process.binding('stdio').stdoutFD);
+
+  stdout.addListener('error', function (err) { throw err; });
+
   return stdout;
 });
 
@@ -140,6 +143,7 @@ process.openStdin = function () {
   var net = module.requireNative('net');
   var fd = process.binding('stdio').openStdin();
   stdin = new net.Stream(fd);
+  stdin.addListener('error', function (err) { throw err; });
   stdin.resume();
   stdin.readable = true;
   return stdin;
diff --git a/test/fixtures/print-10-lines.js b/test/fixtures/print-10-lines.js
new file mode 100644 (file)
index 0000000..fc6b8b5
--- /dev/null
@@ -0,0 +1,4 @@
+puts = require('sys').puts;
+for (var i = 0; i < 10; i++) {
+  puts('count ' + i);
+}
diff --git a/test/simple/test-pipe-head.js b/test/simple/test-pipe-head.js
new file mode 100644 (file)
index 0000000..56e4147
--- /dev/null
@@ -0,0 +1,23 @@
+require('../common');
+
+exec = require('child_process').exec;
+join = require('path').join;
+
+nodePath = process.argv[0];
+script = join(fixturesDir, 'print-10-lines.js');
+
+cmd = nodePath + ' ' + script + ' | head -2';
+
+finished = false;
+
+exec(cmd, function (err, stdout, stderr) {
+  if (err) throw err;
+  lines = stdout.split('\n');
+  assert.equal(3, lines.length);
+  finished = true;
+});
+
+
+process.addListener('exit', function () {
+  assert.ok(finished);
+});