Skip to content
This repository

child process: .pipe() doesn't always work #4000

Open
bnoordhuis opened this Issue September 11, 2012 · 3 comments

2 participants

Ben Noordhuis Leonard Hecker
Ben Noordhuis
var spawn = require('child_process').spawn;
var net = require('net');

function start(cmd, port) {
  net.createServer(function(conn) {
    var proc = spawn(cmd, [], { stdio: 'pipe' });
    conn.pipe(proc.stdin);
    proc.stdout.pipe(conn);
  }).listen(port);
}

start('cat', 8000); // works, simple echo server
start('rev', 8001); // doesn't work, no output
$ nc 127.0.0.1 8000 # cat, works
ping
ping
pong
pong
^D
$ nc 127.0.0.1 8001 # rev, no output
ping
pong
^D
$ rev # expected output
ping
gnip
pong
gnop
^D
$ strace -fe read,write out/Release/node tmp/pipe.js
<snipped>
[pid  6235] read(0,  <unfinished ...>
[pid  6232] read(9, "ping\n", 65536)    = 5
[pid  6232] write(11, "ping\n", 5)      = 5
[pid  6235] <... read resumed> "ping\n", 4096) = 5
[pid  6235] read(0,  <unfinished ...>
[pid  6232] read(9, "pong\n", 65536)    = 5
[pid  6232] write(11, "pong\n", 5)      = 5
[pid  6235] <... read resumed> "pong\n", 4096) = 5
[pid  6235] read(0,  <unfinished ...>
[pid  6232] read(9, "", 65536)          = 0
[pid  6235] <... read resumed> "", 4096) = 0
[pid  6235] write(1, "gnip\ngnop\n", 10) = 10
Process 6235 detached
[pid  6232] read(12, "gnip\ngnop\n", 65536) = 10
[pid  6232] --- SIGCHLD (Child exited) @ 0 (0) ---

Ergo, the child process does write the expected output but not until the pipe is closed. Needs further investigation.

Ben Noordhuis

Needs further investigation.

Further investigation reveals that the rev tool from util-linux switches to buffering mode if stdin is not a tty. Not very surprising but nice to have it confirmed.

Leonard Hecker

@bnoordhuis I don't know if this is the correct issue, but this might help you to solve the problem of buffered pipes on windows: http://stackoverflow.com/q/3385427 (it's much easier on linux, since you can use stdbuf an unbuffer there).

Edit: According to MSDN one can use FILE_FLAG_NO_BUFFERING with pipes without the need for "any particular alignment". Though I still don't know if the output is buffered by the child process's stdout, instead of the OS as the "middleman" (like the case with linux and "stdbuf").

Ben Noordhuis

/cc @piscisaureus - Is the above relevant to how pipes currently work on Windows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.