Skip to contentSkip to conversation

migration: fix QIOChannelFile leak on error in file_connect_outgoing

PATCH In review Trieu Huynh 2 participants 3 messages Mar 28 – Apr 1

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: fix QIOChannelFile leak on error in file_connect_outgoing

Commit 03a680c978 changed g_autoptr(QIOChannelFile) to a plain pointer but failed to restore the necessary object_unref() calls on error paths. Previously, these were handled implicitly by the g_autoptr cleanup mechanism.

Two error paths currently leak the QIOChannelFile object and its underlying file descriptor:

1. When ftruncate() fails (e.g., on character or block devices).
2. When qio_channel_io_seek() fails after the channel is created.

In environments that retry migration automatically (e.g., libvirt), these FDs accumulate until QEMU hits RLIMIT_NOFILE and fails with EMFILE (Too many open files).

Add the missing object_unref() calls to both error paths to ensure resources are properly released.

---
 migration/file.c                   |  2 ++
 tests/qtest/migration/file-tests.c | 48 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
migration/file.c +2 -0
index 5618aced49..962b4b3a1b 100644
@@ -112,6 +112,7 @@ QIOChannel *file_connect_outgoing(MigrationState *s,
         error_setg_errno(errp, errno,
                          "failed to truncate migration file to offset %" PRIx64,
                          offset);
+        object_unref(OBJECT(fioc));
         goto out;
     }
 
@@ -119,6 +120,7 @@ QIOChannel *file_connect_outgoing(MigrationState *s,
 
     ioc = QIO_CHANNEL(fioc);
     if (offset && qio_channel_io_seek(ioc, offset, SEEK_SET, errp) < 0) {
+        object_unref(OBJECT(fioc));
         ioc = NULL;
         goto out;
     }
tests/qtest/migration/file-tests.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Trieu Huynh

Commit 03a680c978 changed g_autoptr(QIOChannelFile) to a plain pointer but failed to restore the necessary object_unref() calls on error paths. Previously, these were handled implicitly by the g_autoptr cleanup mechanism.

Two error paths currently leak the QIOChannelFile object and its underlying file descriptor:

1. When ftruncate() fails (e.g., on character or block devices).
2. When qio_channel_io_seek() fails after the channel is created.

Quoted 8 lines

In environments that retry migration automatically (e.g., libvirt), these FDs accumulate until QEMU hits RLIMIT_NOFILE and fails with EMFILE (Too many open files).

Add the missing object_unref() calls to both error paths to ensure resources are properly released.

Good catch.

I'm just curious, how did you find it?

Signature · 1 line
Peter Xu
#2 lore raw
Quoted 19 lines · Peter Xu
Quoted 18 lines · Trieu Huynh

Commit 03a680c978 changed g_autoptr(QIOChannelFile) to a plain pointer but failed to restore the necessary object_unref() calls on error paths. Previously, these were handled implicitly by the g_autoptr cleanup mechanism.

Two error paths currently leak the QIOChannelFile object and its underlying file descriptor:

1. When ftruncate() fails (e.g., on character or block devices).
2. When qio_channel_io_seek() fails after the channel is created.

In environments that retry migration automatically (e.g., libvirt), these FDs accumulate until QEMU hits RLIMIT_NOFILE and fails with EMFILE (Too many open files).

Add the missing object_unref() calls to both error paths to ensure resources are properly released.

Quoted 8 lines
Quoted 1 line

Good catch.

I'm just curious, how did you find it?

Thanks, just by change, while researching the codebase and blame some locs :)

Quoted 3 lines
Signature · 1 line
Peter Xu