handle i/o errors when trying to flush stdout

this can happend when stdout is not a proper file descriptor or has
been closed somehow