Search code examples
javascriptnode.jsbashmkfifo

Why does opening a fifo pipe in node block child_process.exec?


When I run this in bash:

mkfifo im-a-pipe && node -e '
var fs = require("fs")
var childProcess = require("child_process")

console.log("pre-open")
fs.open("im-a-pipe", "w", function(err, fd){
if(err)
throw err
console.log("opened")
})
console.log("post-open")
childProcess.exec("echo wat")
console.log("YOU CAN NOT SEE MEEE")
'

I expect the following output:

pre-open
post-open
YOU CAN NOT SEE MEEE

But instead node waits after printing the first two lines:

pre-open
post-open

I'm thinking that this probably has something to do with the pipe blocking until something opens the other side, but this behavior surprised me.

Am I missing something with how these functions are supposed to operate?


Solution

  • This may be a bug in your operating system or just an older version of Node. Works for me on Mac with Node 4. A co-worker was able to run it on Debian but not until he upgraded to Node 8.