On Thu, 2026-11-12 at 05:07 +0300, Michael Tokarev wrote: > On 10/12/26 20:38, Filip Hejsek wrote: > > Make qio_channel_file_close() set fd to -1 even if qemu_close() returns > > an error so that qio_channel_file_finalize() doesn't call close again. > > > > On Linux, close() always closes the fd, even when it returns an error. > > Because the fd can be reused after it is closed, the close must not > > be retried. > > > > Signed-off-by: Filip Hejsek <[email protected]> > > --- > > io/channel-file.c | 12 +++++++++--- > > 1 file changed, 9 insertions(+), 3 deletions(-) > > > > diff --git a/io/channel-file.c b/io/channel-file.c > > index 5cef75a67c..afc9640518 100644 > > --- a/io/channel-file.c > > +++ b/io/channel-file.c > > @@ -255,12 +255,18 @@ static int qio_channel_file_close(QIOChannel *ioc, > > { > > QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); > > > > - if (qemu_close(fioc->fd) < 0) { > > + int ret = qemu_close(fioc->fd); > > + /* > > + * The fd should now have been closed even if we got an error. There > > are > > + * obscure systems where this isn't always the case, but an fd leak on > > + * obscure systems is less bad than double close on common systems. > > + */ > > Hmm. It looks like linux is the obscure system now, not the other way > around. Before, POSIX.1-2008 stated that after EINTR, the state of the > FD is unspecified. However, POSIX.1-2024 states that after EINTR, the > FD should remain open. And linux isn't going to change the way close(2) > works there.
The system where close() may keep the fd open is HP-UX, which QEMU doesn't support. On Linux, macOS, Windows, and FreeBSD/NetBSD/OpenBSD, the fd is always closed. Of these, at least Linux can close the fd and return EINTR when the syscall is interrupted, in violation of POSIX.1- 2024. > And there's no posix_close(fd, flags). Which is not available on linux > (yet). > > And there's also an additional possible errno value here: EINPROGRESS. Linux doesn't use EINPROGRESS. I think the kernel folks were opposed to it. Also see <https://sourceware.org/bugzilla/show_bug.cgi?id=14627>. > The other possible failure cases seems to be consistent. > > So at least, it looks like, the comment needs to be updated. Yes, > linux is the main target of qemu, but not only, -- and channel-file is > used in the tools too (qemu-nbd, qemu-img, etc) - where it is rather > irrelevant - keep it open or close it again later. > > Why do we care about this close(2) return value here anyway? > Have you seen actual failure case? This was spotted while reading the code, so no failures observed. But the risk isn't an incorrect return value, it's about finalize() calling close() a second time. That applies to the tools as well. So what about changing the comment to this? I can send a v2 if we agree on wording. Set fioc->fd to -1, to prevent qio_channel_file_finalize() from closing it again. POSIX.1-2024 specifies that the fd is closed even when close() returns an error, except when the error is EINTR. The systems that QEMU supports however violate the standard and always close the fd, even when returning EINTR. If the fd was actually closed, we must not try to close it a second time as we could accidentally close someone else's fd. Regards, Filip Hejsek
