Fix stdout premature EOF

Wait until the goroutines for stdout and stderr have both finished
before closing stdin.

Fixes #3631
I'm not sure if this deferred closure should also be part of the waiter goroutine above - looking for advice.