Exit gracefully when stdout becomes invalid