Also close the temporary file if failed to write it.