io/channel-file.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
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 <filip.hejsek@gmail.com>
---
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.
+ */
+ fioc->fd = -1;
+ if (ret < 0) {
error_setg_errno(errp, errno,
- "Unable to close file");
+ "Got an error while closing file");
return -1;
}
- fioc->fd = -1;
return 0;
}
---
base-commit: 615ece3c406b262996e569f29ea6eed0d81c4d8d
change-id: 20261012-fix-double-close-7bc5b144cc94
Best regards,
--
Filip Hejsek <filip.hejsek@gmail.com>
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 <filip.hejsek@gmail.com>
> ---
> 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.
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.
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?
Thanks,
/mjt
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 <filip.hejsek@gmail.com>
> > ---
> > 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
On 11/12/26 06:10, Filip Hejsek wrote: > On Thu, 2026-11-12 at 05:07 +0300, Michael Tokarev wrote: >> 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. Yes, I understand. Yet, POSIX standartized the HP-UX behavior, so all other systems has become "obscure" this way. >> 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. I mean, this weren't a problem until now, for all the decades qemu exists. Not a problem in practice, that is. I understand what the problem we have. And yes it is better to keep such an FD "dangling" in case of close() returned error and haven't actually closed the FD, than to close an innocent/reused FD. > 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. Yes, let's pick this commit instead. No need to re-send. Thanks, /mjt
© 2016 - 2026 Red Hat, Inc.