Ensure output stream is always closed