Don't close PTY stdin, stdout until after process exits.
Work around odd pause-before-exit bug; rearrange exit logic to avoid possible race.