More robust handling of close_on_eof