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.

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

Reply via email to