If a close(2) system call fails with EIO, will the file descriptor still be deleted?
If yes, is it not possible to handle a spurious IO error by retrying later? If no, how should one prevent a file descriptor leak?
Description. close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).
RETURN VALUE Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to indicate the error.
close() returns zero on success. On error, -1 is returned, and errno is set to indicate the error.
That's a tricky question. However, the POSIX standard does cover it in the description of close()
:
If close() is interrupted by a signal that is to be caught, it shall return -1 with errno set to [EINTR] and the state of fildes is unspecified. If an I/O error occurred while reading from or writing to the file system during close(), it may return -1 with errno set to [EIO]; if this error is returned, the state of fildes is unspecified.
So, the state of the file descriptor is unspecified by the standard.
For most practical purposes, it is closed; there is precious little you can do with the file descriptor even if it is officially open. You could try an innocuous operation (like fcntl()
and F_GETFL
) and see whether you get EBADF back, indicating the descriptor is formally closed. But if it is open and the cause of the EIO error is permanent, then you're likely to get EIO every time you try to do anything with it (possibly including the fcntl()
call). You might or might not ever get the same descriptor returned by another open-like operation. It is not clear that even dup2()
could be successful specifying the 'dead' file descriptor as the target if the dead file descriptor is open but uncloseable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With