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. + */ + 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 <[email protected]>
