Skip to content

Instantly share code, notes, and snippets.

@fjakobs
Created March 6, 2014 14:52
Show Gist options
  • Save fjakobs/9391420 to your computer and use it in GitHub Desktop.
Save fjakobs/9391420 to your computer and use it in GitHub Desktop.
node.js when opening multiple fifos
// Opening fifos, which nobody is reading from causes strange behaviour and has
// side effects on opening network connections to localhost
// Bug #1
// ------
// run with "node fifo_bug.js 1"
// The client will connect but process.exit(0) has will not terminate the process
// then do "cat fifos/fifo0"
// this will cause the process to exit
// Note: this does not happen with 0.6 or 0.8
// Bug #2
// ------
// run with "node fifo_bug.js 4"
// The client will never connect to "localhost"
// Note: this happens in node 0.6, 0.8, 0.10 and 0.11
var net = require("net");
var fs = require("fs");
var exec = require("child_process").exec;
var C = process.argv[2] || 3;
net.createServer(function(conn) {
console.log("Conn");
process.exit(0);
}).listen(8989, "localhost", function() {
// first create come fifos
var cmd = ["mkdir -p fifos"];
for (var i = 0; i < C; i++) {
cmd.push("mkfifo fifos/fifo" + i);
}
exec(cmd.join("; "), { cwd: __dirname }, function() {
// then open all fifos. The callback won't be called until someone
// starts reading from the fifo
for (var i = 0; i < C; i++)
fs.open(__dirname + "/fifos/fifo" + i, "a", function(i, err, fd) {
console.log("opened", i, fd);
}.bind(null, i));
// If I use "127.0.0.1" here insted of "localhost" then the connection works
net.createConnection(8989, "localhost");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment