joyent/node — Issue #2891: process.stdin piping messes up stdin for child processes?
Author: epeli
Created: 2012-03-07 15:31:51+00:00
State: open

Could somebody take a look at this since I really don't know whats happening here, but I can reproduce the issue easily.

I think this has something to do with stdin file descriptor passed to the child processes. I ran into this when I tried to execute a git commit after I had done some process.stdin piping.

I tried this on v0.6.12.

Here's how to reproduce this. First setup a Git repo:

mkdir tmprepo
cd tmprepo
git init
touch file.txt
git add file.txt

Put this to commit.js and execute it

```javascript
var exec = require("child_process").exec;
var fs = require("fs");

process.stdin.resume();
var cat = exec("cat > /dev/null");
process.stdin.pipe(cat.stdin);
process.stdout.write("Press Ctrl+d\n");

cat.on("exit", function() {

// Uncomment this line to make the Git issue to disappear!
// It doesn't matter what the file is opened here.
// Just open up something and the issue disappears
// var notEvenUsingThis = fs.openSync("/dev/urandom", "r");
exec("git commit --all --message a_commit", function(code, stdout, stderr) {
console.log("Git exit code: " + code + "\n");
console.log("Git stdout: \n" + stdout + "\n");
console.log("Git stderr: \n" + stderr + "\n");
});

});
```

node commit.js

and git should complain something like

error: unable to create temporary sha1 filename : No such file or directory

But this error disappears if the line

var notEvenUsingThis = fs.openSync("/dev/urandom", "r");

is uncommented.

You get the same Git error if you try to [make a git commit when stdin is closed](https://gist.github.com/1992958). This can be also a Git bug, but something fishy is going on here. The child process environment should not change if I just open some file and don't do anything with it.