4 ms·
No one checks close()'s result because it lies. What should you write, while (close(fd) != 0) {} ? Or does it risk getting an infinite loop if, e.g., some
by ncmncm 6y ago
No one checks close()'s result because it lies.
What should you write,
while (close(fd) != 0) {}
? Or does it risk getting an infinite loop if, e.g., some network connection disappears?
Maybe
if (fsync(fd) == 0)
while (close(fd) != 0) {}
else abort();
But maybe fsync is interruptible too.
Ultimately there is nothing strictly correct to write, and you should just not really count on files being closed before process termination, but call close() as an optimization not to leak too many fds.
- cesarb 6y agoYou cannot retry close(). Quoting from https://man7.org/linux/man-pages/man2/close.2.html#NOTES https://man7.org/linux/man-pages/man2/close.2.html#NOTES Retrying the close() after a failure return is the wrong thing to do, since this may cause a reused file descriptor from another thread to be closed. This can occur because the Linux kernel always releases the file descriptor early in the close operation, freeing it for reuse; the steps that may return an error, such as flushing data to the filesystem or device, occur only later in the close operation. That is, once you call close(), the file descriptor is always freed (even if close() returns an error), and can be reused for an open() or similar in another thread. Retrying the close() will fail with EBADF in the best case, and close something another thread has opened in the worst case.
- OskarS 6y agoSo what is the point of it having a return value at all then?
- rectang 6y ago> No one checks close()'s result because it lies. And since nobody checks the result of close and very few check the result of every single write operation, many "disk full" errors go unnoticed.
- ncmncm 6y agoIf you want to know whether your writes are getting out, you will get a much more reliable indication from fsync(). So, instead of exhorting people to check close()'s result, you should exhort them to fsync() first and check that result. A very old programming principle says, "Never check for any failure you are not equipped to act on." It is sometimes used as a reminder to ensure you are always so equipped.
- rectang 6y agoMy understanding is fsync() will tell you more than close() only when a flush of the OS cache fails to make it to disk. Is there anything else? The problem with calling fsync() is that you have to wait for it to finish. There are many scenarios where the extra data integrity guarantees you get from calling fsync() aren't important.
- ncmncm 6y agoclose() will fail to report a wide variety of failures. But Cesar is right, you can't re-try it. And fsync() can be very slow on most commonly-used file systems. We are fortunate that now SSDs are fast.
- deleted 6y ago[deleted]
- Jweb_Guru 6y ago> A very old programming principle says, "Never check for any failure you are not equipped to act on." It is sometimes used as a reminder to ensure you are always so equipped. This sounds like a great principle for allowing dangerous bugs to linger in a codebase for years... one can always act upon a failure by aborting.
- einpoklum 6y agofsync() is not part of the standard C library. You won't be able to use it on, say, MS Windows. (Although maybe rust has it?)
- einpoklum 6y ago> What should you write? You should try closing once, and if it fails, either die (e.g. with abort() ), or do something meaningful and relevant to your specific situation. > Ultimately there is nothing strictly correct to write, If that is the case for you - then definitely abort().