Fix stdout premature EOF

Wait until the goroutines for stdout and stderr have both finished
before closing stdin.

Fixes #3631