correctly check for invalid file descriptors