@samitouri / QOSamiQemu / commits / 66e127d50c

tests/qtest/migration: Fix leak of migration tests data

When the migration-test is invoked with the '-p' flag (to run a single test), the glib code won't call the destroy function for the not-executed tests, causing the MigrationTest wrapper data to leak. This doesn't affect make check, but affects debugging use-cases where having a leak pop up in ASAN output is extra annoying. Fix by adding the tests data to a list and freeing them all at the end of migration-test execution. Any tests actually dispatched by glib will have the destroy function called as usual. Note that migration_test_add_suffix() is altered to call migration_test_add() so that there's only one place adding the data to the list. Performance is not an issue at the moment, we have < 100 tests. Reviewed-by: Peter Xu <peterx@redhat.com> Reviewed-by: Prasad Pandit <pjp@fedoraproject.org> Link: https://lore.kernel.org/qemu-devel/20260311213418.16951-2-farosas@suse.de Signed-off-by: Fabiano Rosas <farosas@suse.de>

Fabiano Rosas committed Mar 11, 2026 at 18:34 UTC 66e127d50c890a850e263eebdc3ca608e66f704f
3 files changed +17 -6
tests/qtest/migration/framework.c
+2
@@ -1162,5 +1162,7 @@ int migration_env_clean(MigrationTestEnv *env)
1162 }
1163 g_free(tmpfs);
1164
1165 + migration_tests_free();
1166 +
1167 return ret;
1168 }
tests/qtest/migration/migration-util.c
+14 -5
@@ -38,6 +38,7 @@
38 #include "linux/kvm.h"
39 #endif
40
41 +GQueue *tests;
42
43 static char *SocketAddress_to_str(SocketAddress *addr)
44 {
@@ -243,6 +244,8 @@ static void migration_test_destroy(gpointer data)
244 {
245 MigrationTest *test = (MigrationTest *)data;
246
247 + g_queue_remove(tests, test);
248 +
249 g_free(test->data);
250 g_free(test->name);
251 g_free(test);
@@ -268,21 +271,27 @@ void migration_test_add(const char *path,
271
272 qtest_add_data_func_full(path, test, migration_test_wrapper,
273 migration_test_destroy);
274 + if (!tests) {
275 + tests = g_queue_new();
276 + }
277 + g_queue_push_tail(tests, test);
278 }
279
280 void migration_test_add_suffix(const char *path, const char *suffix,
281 void (*fn)(char *name, MigrateCommon *args))
282 {
276 - MigrationTest *test = g_new0(MigrationTest, 1);
283 + g_autofree char *name = NULL;
284
285 g_assert(g_str_has_suffix(path, "/"));
286 g_assert(!g_str_has_prefix(suffix, "/"));
287
281 - test->func = fn;
282 - test->name = g_strconcat(path, suffix, NULL);
288 + name = g_strconcat(path, suffix, NULL);
289 + migration_test_add(name, fn);
290 +}
291
284 - qtest_add_data_func_full(test->name, test, migration_test_wrapper,
285 - migration_test_destroy);
292 +void migration_tests_free(void)
293 +{
294 + g_queue_free_full(tests, migration_test_destroy);
295 }
296
297 #ifdef O_DIRECT
tests/qtest/migration/migration-util.h
+1 -1
@@ -59,5 +59,5 @@ void migration_test_add_suffix(const char *path, const char *suffix,
59 void (*fn)(char *name, MigrateCommon *args));
60 char *migrate_get_connect_uri(QTestState *who);
61 void migrate_set_ports(QTestState *to, QList *channel_list);
62 -
62 +void migration_tests_free(void);
63 #endif /* MIGRATION_UTIL_H */