Re: [RFC v1] man/man2/close.2: CAVEATS: Document divergence from POSIX.1-2024
From: Alejandro Colomar <alx@kernel.org>
Date: 2025-05-17 13:46:30
Also in:
linux-fsdevel
On Sat, May 17, 2025 at 09:32:52AM -0400, Rich Felker wrote:
On Fri, May 16, 2025 at 04:39:57PM +0200, Vincent Lefevre wrote:quoted
On 2025-05-16 09:05:47 -0400, Rich Felker wrote:quoted
FWIW musl adopted the EINPROGRESS as soon as we were made aware of the issue, and later changed it to returning 0 since applications (particularly, any written prior to this interpretation) are prone to interpret EINPROGRESS as an error condition rather than success and possibly misinterpret it as meaning the fd is still open and valid to pass to close again.If I understand correctly, this is a poor choice. POSIX.1-2024 says: ERRORS The close() and posix_close() functions shall fail if: [...] [EINPROGRESS] The function was interrupted by a signal and fildes was closed but the close operation is continuing asynchronously. But this does not mean that the asynchronous close operation will succeed.There are no asynchronous behaviors specified for there to be a conformance distinction here. The only observable behaviors happen instantly, mainly the release of the file descriptor and the process's handle on the underlying resource. Abstractly, there is no async operation that could succeed or fail.quoted
So the application could incorrectly deduce that the close operation was done without any error.This deduction is correct, not incorrect. Rather, failing with EINPROGRESS would make the application incorrectly deduce that there might be some error it missed (even if it's aware of the new error code), and absolutely does make all existing applications written prior to the new text in POSIX 2024 unable to determine if the fd was even released and needs to be passed to close again or not.
Hi Rich,
I think this is not correct; at least on Linux. The manual page is very
clear that close(2) should not be retried on error:
Dealing with error returns from close()
A careful programmer will check the return value of close(),
since it is quite possible that errors on a previous write(2)
operation are reported only on the final close() that releases
the open file description. Failing to check the return value
when closing a file may lead to silent loss of data. This can
especially be observed with NFS and with disk quota.
Note, however, that a failure return should be used only for di‐
agnostic purposes (i.e., a warning to the application that there
may still be I/O pending or there may have been failed I/O) or
remedial purposes (e.g., writing the file once more or creating
a backup).
Retrying the close() after a failure return is the wrong thing
to do, since this may cause a reused file descriptor from an‐
other 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.
...
A careful programmer who wants to know about I/O errors may pre‐
cede close() with a call to fsync(2).
Cheers,
Alex
--
<https://www.alejandro-colomar.es/> Attachments
- signature.asc [application/pgp-signature] 833 bytes