* [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
@ 2026-11-25 19:31 Denis V. Lunev
2026-11-25 19:31 ` [PATCH 1/2] migration: fix " Denis V. Lunev
` (2 more replies)
0 siblings, 3 replies; 15+ messages in thread
From: Denis V. Lunev @ 2026-11-25 19:31 UTC (permalink / raw)
To: qemu-devel
Cc: den, Peter Xu, Fabiano Rosas, Marc-André Lureau,
Laurent Vivier
Cancelling a migration right after starting it can hang the source
QEMU forever. This happens when the cancel arrives while the source is
still connecting to the destination and the connection then succeeds.
The source goes on as if nothing was cancelled, then waits for a reply
from the destination that never comes. It waits with the BQL held, so
the monitor stops answering too, and the only way out is to kill the
source. We hit this in an automated test that aborts a live migration
right after starting it.
Patch 2 adds a qtest for this window, which Peter left for later when
fixing the early-cancel crash in 4f08b870f8 [1].
[1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
Signed-off-by: Denis V. Lunev <den@openvz.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Fabiano Rosas <farosas@suse.de>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
Cc: Laurent Vivier <lvivier@redhat.com>
Denis V. Lunev (2):
migration: fix source hang on cancel before the channel connects
tests/qtest/migration: test cancel before the channel connects
migration/channel.c | 6 +++
migration/migration.c | 2 +-
migration/migration.h | 1 +
tests/qtest/migration/precopy-tests.c | 66 +++++++++++++++++++++++++++
4 files changed, 74 insertions(+), 1 deletion(-)
base-commit: 2bfbe39d0fff8af881cffe2bab9b615e77807c35
--
2.55.0
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 1/2] migration: fix source hang on cancel before the channel connects
2026-11-25 19:31 [PATCH 0/2] migration: fix a source hang on cancel before the channel connects Denis V. Lunev
@ 2026-11-25 19:31 ` Denis V. Lunev
2026-11-25 19:31 ` [PATCH 2/2] tests/qtest/migration: test " Denis V. Lunev
2026-11-25 23:26 ` [PATCH 0/2] migration: fix a source hang on " Fabiano Rosas
2 siblings, 0 replies; 15+ messages in thread
From: Denis V. Lunev @ 2026-11-25 19:31 UTC (permalink / raw)
To: qemu-devel; +Cc: den, Peter Xu, Fabiano Rosas, Marc-André Lureau
From: Denis V. Lunev <den@openvz.org>
Cancelling a migration while the source is still connecting to the
destination hangs the source QEMU forever if the connection then
succeeds. It hangs with the BQL held, so the monitor stops answering
too.
When the cancel arrives there is no connection yet and nothing to shut
down. Since 4f08b870f8fd, migration_cancel() only marks the migration as
cancelling and leaves the rest to the code that runs when the connect
completes, and that code finishes the cancel only when the connect
fails.
When the connect succeeds, the migration starts anyway. The return path
thread starts and waits for the destination. The migration thread sees
the cancel and quits. The cleanup then waits for the return path thread
to exit, and that never happens: the destination has nothing to send
and keeps the connection open, and a cancel, unlike a failure, does not
make the cleanup shut the return path down.
Check for the cancel as soon as the connection is up, in
migration_channel_connect_outgoing(), and clean up there before
anything is started. A cancel during the TLS handshake gets there too,
once the handshake is done. migration_cleanup() is no longer static so
that channel.c can call it.
Fixes: 4f08b870f8fd ("migration: Fix crash on second migration when cancel early")
Signed-off-by: Denis V. Lunev <den@openvz.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Fabiano Rosas <farosas@suse.de>
Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
---
migration/channel.c | 6 ++++++
migration/migration.c | 2 +-
migration/migration.h | 1 +
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/migration/channel.c b/migration/channel.c
index 1aeb881485..6e2bf9ad39 100644
--- a/migration/channel.c
+++ b/migration/channel.c
@@ -563,6 +563,12 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
{
trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
+ /* migration_cancel() leaves a cancel during connect for us to finish */
+ if (s->state == MIGRATION_STATUS_CANCELLING) {
+ migration_cleanup(s);
+ return;
+ }
+
if (migrate_channel_requires_tls_upgrade(ioc)) {
Error *local_err = NULL;
diff --git a/migration/migration.c b/migration/migration.c
index 5686209ab1..4fc1ca9fd2 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -1378,7 +1378,7 @@ void migration_cleanup_json_writer(MigrationState *s)
g_clear_pointer(&s->vmdesc, json_writer_free);
}
-static void migration_cleanup(MigrationState *s)
+void migration_cleanup(MigrationState *s)
{
QEMUFile *tmp = NULL;
diff --git a/migration/migration.h b/migration/migration.h
index 057c85d611..b3067536cb 100644
--- a/migration/migration.h
+++ b/migration/migration.h
@@ -594,6 +594,7 @@ bool migration_incoming_setup(QIOChannel *ioc, uint8_t channel, Error **errp);
void migration_outgoing_setup(QIOChannel *ioc);
void migration_connect_error_propagate(MigrationState *s, Error *error);
+void migration_cleanup(MigrationState *s);
void migrate_error_propagate(MigrationState *s, Error *error);
bool migrate_has_error(MigrationState *s);
--
2.55.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 2/2] tests/qtest/migration: test cancel before the channel connects
2026-11-25 19:31 [PATCH 0/2] migration: fix a source hang on cancel before the channel connects Denis V. Lunev
2026-11-25 19:31 ` [PATCH 1/2] migration: fix " Denis V. Lunev
@ 2026-11-25 19:31 ` Denis V. Lunev
2026-11-26 19:33 ` Peter Xu
2026-11-25 23:26 ` [PATCH 0/2] migration: fix a source hang on " Fabiano Rosas
2 siblings, 1 reply; 15+ messages in thread
From: Denis V. Lunev @ 2026-11-25 19:31 UTC (permalink / raw)
To: qemu-devel; +Cc: den, Peter Xu, Fabiano Rosas, Laurent Vivier
From: Denis V. Lunev <den@openvz.org>
Cancel a migration while the source is still connecting, then let the
connection succeed. The source must end up in "cancelled", and a
second migration afterwards must complete.
To keep the source waiting in connect(), the test points it at a unix
socket of its own whose listen queue is already full. On Linux,
connect() then blocks until the test accepts a connection, so the test
is Linux-only.
The return-path capability is on because the hang this test guards
against is the cleanup waiting for the return path thread.
Signed-off-by: Denis V. Lunev <den@openvz.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Fabiano Rosas <farosas@suse.de>
Cc: Laurent Vivier <lvivier@redhat.com>
---
tests/qtest/migration/precopy-tests.c | 66 +++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/tests/qtest/migration/precopy-tests.c b/tests/qtest/migration/precopy-tests.c
index d57ffcac23..114dd8caf9 100644
--- a/tests/qtest/migration/precopy-tests.c
+++ b/tests/qtest/migration/precopy-tests.c
@@ -666,6 +666,68 @@ static void test_cancel_src_after_status(char *test_path, MigrateCommon *args)
migrate_end(from, to, false);
}
+#ifdef CONFIG_LINUX
+/* On Linux connect() to a unix socket with a full backlog blocks */
+static int unix_listen_full(const char *path, int *filler)
+{
+ struct sockaddr_un addr = { .sun_family = AF_UNIX };
+ int lfd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+ *filler = socket(AF_UNIX, SOCK_STREAM, 0);
+ g_assert_cmpint(lfd, >=, 0);
+ g_assert_cmpint(*filler, >=, 0);
+
+ g_assert_cmpuint(strlen(path), <, sizeof(addr.sun_path));
+ strcpy(addr.sun_path, path);
+ g_assert_cmpint(bind(lfd, (struct sockaddr *)&addr, sizeof(addr)), ==, 0);
+ g_assert_cmpint(listen(lfd, 0), ==, 0);
+ g_assert_cmpint(connect(*filler, (struct sockaddr *)&addr, sizeof(addr)),
+ ==, 0);
+ return lfd;
+}
+
+static void test_cancel_src_before_connect(char *name, MigrateCommon *args)
+{
+ g_autofree char *full_path = g_strdup_printf("%s/migsocket-full", tmpfs);
+ g_autofree char *full_uri = g_strdup_printf("unix:%s", full_path);
+ g_autofree char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
+ QTestState *from, *to;
+ int lfd, filler, accepted;
+
+ if (migrate_start(&from, &to, &args->start)) {
+ return;
+ }
+
+ migrate_set_capability(from, "return-path", true);
+ migrate_set_capability(to, "return-path", true);
+
+ lfd = unix_listen_full(full_path, &filler);
+ migrate_qmp(from, to, full_uri, NULL, "{}");
+ migrate_cancel(from);
+
+ /* Taking the filler off the backlog lets the source's connect() finish */
+ accepted = accept(lfd, NULL, NULL);
+ g_assert_cmpint(accepted, >=, 0);
+
+ wait_for_migration_status(from, "cancelled",
+ (const char * []) { "completed", NULL });
+
+ close(accepted);
+ close(filler);
+ close(lfd);
+ unlink(full_path);
+
+ migrate_incoming_qmp(to, uri, NULL, "{}");
+ wait_for_serial("src_serial");
+ migrate_ensure_converge(from);
+ migrate_qmp(from, to, uri, NULL, "{}");
+ wait_for_migration_complete(from);
+ wait_for_migration_complete(to);
+
+ migrate_end(from, to, true);
+}
+#endif
+
static void calc_dirty_rate(QTestState *who, uint64_t calc_time)
{
qtest_qmp_assert_success(who,
@@ -1174,4 +1236,8 @@ void migration_test_add_precopy(MigrationTestEnv *env)
test_cancel_src_after_status);
}
}
+#ifdef CONFIG_LINUX
+ migration_test_add("/migration/cancel/src/before/connect",
+ test_cancel_src_before_connect);
+#endif
}
--
2.55.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-25 19:31 [PATCH 0/2] migration: fix a source hang on cancel before the channel connects Denis V. Lunev
2026-11-25 19:31 ` [PATCH 1/2] migration: fix " Denis V. Lunev
2026-11-25 19:31 ` [PATCH 2/2] tests/qtest/migration: test " Denis V. Lunev
@ 2026-11-25 23:26 ` Fabiano Rosas
2026-11-26 14:02 ` Denis V. Lunev
2 siblings, 1 reply; 15+ messages in thread
From: Fabiano Rosas @ 2026-11-25 23:26 UTC (permalink / raw)
To: Denis V. Lunev, qemu-devel
Cc: den, Peter Xu, Marc-André Lureau, Laurent Vivier
"Denis V. Lunev" <den@openvz.org> writes:
What a coincidence, I just spent the afternoon fixing this.
> Cancelling a migration right after starting it can hang the source
> QEMU forever. This happens when the cancel arrives while the source is
> still connecting to the destination and the connection then succeeds.
> The source goes on as if nothing was cancelled, then waits for a reply
> from the destination that never comes. It waits with the BQL held, so
> the monitor stops answering too, and the only way out is to kill the
> source. We hit this in an automated test that aborts a live migration
> right after starting it.
How does this test look like? I'm thinking that instead of writing a new
test for this bug (haven't read your patch 2 yet), what we need it to
remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
test. I think that's an artificial delay that's there for no good reason
in this case. If your CI manages to hit the issue on a real-ish
scenario, that's even more reason to remove that code.
>
> Patch 2 adds a qtest for this window, which Peter left for later when
> fixing the early-cancel crash in 4f08b870f8 [1].
>
> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Fabiano Rosas <farosas@suse.de>
> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> Cc: Laurent Vivier <lvivier@redhat.com>
>
> Denis V. Lunev (2):
> migration: fix source hang on cancel before the channel connects
Here's my version. It's very opinionated due to my long-standing grudge
with this code.
https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
-- >8 --
From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
From: Fabiano Rosas <farosas@suse.de>
Date: Wed, 25 Nov 2026 18:43:57 -0300
Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
If migration is cancelled at just the right moment when the main
channel transport has finished connecting, it's possible that the
shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
entirely. A moment later, migration_channel_connect_outgoing() carries
along with the migration and initializes the QEMUFiles for the first
time.
Migration then hangs because the source goes into migration_cleanup()
due to its state being CANCELLING and hangs waiting for the return
path thread to join, which never happens because the destination is
unaware and never sends any SHUT message. The cleanup itself also
doesn't kick the RP thread because commit 64763e8ca61 ("migration:
Keep s->rp_state.from_dst_file open until migration ends") has changed
the code to keep the QEMUFile until after the RP thread exits. The
only other way to kick it would be from
stop_return_path_thread_on_source() if an error had occurred, but no
error has occurred.
This situation is not surprising because the migration cancelling has
never had any synchronization mechanism aside from the BQL, which in
this case is not enough to protect from the race. It has also never
had a way to inform the rest of the code in a thread safe manner about
the cancellation.
To start moving in the correct direction, make the qmp_migrate_cancel
command (under BQL) set the migration error, which is protected by the
error_mutex and suitable to be used as a way to inform the other
threads of the cancellation. This avoids the need to check the
MigrationStatus which is already fragile in terms of concurrency.
Note that the QMP handler for migrate_cancel is used instead of the
internal function because the latter is used from other parts of the
code that might have set s->error already.
In the case where qmp_migrate_cancel() sets s->error and some other
code hits an error due to the shutdown()s, the
migrate_error_propagate() call chain will simply ignore the error set
for last. This is the correct behavior because errors after cancelling
are very likely to be caused by the cancellation itself. This has the
extra benefit that the migration cancellation now shows up in the
logs, which solves a confusing situation when the user sees "Broken
pipe" and thinks it's due to an error.
For this specific bug, check that the asynchronous entry to
migration_channel_connect_outgoing() doesn't bring an already set
s->error and call migration_connect_error_propagate() under the BQL if
that's the case. We need the whole migration_connect_error_propagate()
here to handle the postcopy resume case as well as making sure
migration_cleanup() is reached.
Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
and my stress branch, which basically skips
migrate_wait_for_dirty_mem().
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/channel.c | 5 +++++
migration/migration.c | 8 ++++++++
2 files changed, 13 insertions(+)
diff --git a/migration/channel.c b/migration/channel.c
index 1aeb8814858..12ff68fc38c 100644
--- a/migration/channel.c
+++ b/migration/channel.c
@@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
{
trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
+ if (migrate_has_error(s)) {
+ migration_connect_error_propagate(s, error_copy(s->error));
+ return;
+ }
+
if (migrate_channel_requires_tls_upgrade(ioc)) {
Error *local_err = NULL;
diff --git a/migration/migration.c b/migration/migration.c
index 5686209ab11..40c3f21d520 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
return;
}
+ migrate_error_free(migrate_get_current());
+
if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
return;
}
@@ -2218,6 +2220,9 @@ out:
void qmp_migrate_cancel(Error **errp)
{
+ MigrationState *s = migrate_get_current();
+ Error *local_err = NULL;
+
/*
* After postcopy migration has started, the source machine is not
* recoverable in case of a migration error. This also means the
@@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
return;
}
+ error_setg(&local_err, "Migration was cancelled by the user.");
+ migrate_error_propagate(s, local_err);
+
migration_cancel();
}
--
2.53.0
^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-25 23:26 ` [PATCH 0/2] migration: fix a source hang on " Fabiano Rosas
@ 2026-11-26 14:02 ` Denis V. Lunev
2026-11-26 14:38 ` Fabiano Rosas
0 siblings, 1 reply; 15+ messages in thread
From: Denis V. Lunev @ 2026-11-26 14:02 UTC (permalink / raw)
To: Fabiano Rosas, Denis V. Lunev, qemu-devel
Cc: Peter Xu, Marc-André Lureau, Laurent Vivier
On 11/26/26 00:26, Fabiano Rosas wrote:
> "Denis V. Lunev" <den@openvz.org> writes:
>
> What a coincidence, I just spent the afternoon fixing this.
>
>> Cancelling a migration right after starting it can hang the source
>> QEMU forever. This happens when the cancel arrives while the source is
>> still connecting to the destination and the connection then succeeds.
>> The source goes on as if nothing was cancelled, then waits for a reply
>> from the destination that never comes. It waits with the BQL held, so
>> the monitor stops answering too, and the only way out is to kill the
>> source. We hit this in an automated test that aborts a live migration
>> right after starting it.
> How does this test look like? I'm thinking that instead of writing a new
> test for this bug (haven't read your patch 2 yet), what we need it to
> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
> test. I think that's an artificial delay that's there for no good reason
> in this case. If your CI manages to hit the issue on a real-ish
> scenario, that's even more reason to remove that code.
This is old fashioned product testing over OpenStack cluster
which specifically performing cancels in a row. Tests are run
inside VMs inside the product and thus L0 host delays comes
as a bit of fuzzing.
Tests are running non-stop inside development cycle :-)
That is why I am coming with this or that problems on paths
like that. But this is not very good in terms of QEMU level
specific tests and that is why patch 2 comes with a pointing
tests.
NOTE: both your are my patches are fixed the problem as stated
and the test passes without both your and my variants.
>> Patch 2 adds a qtest for this window, which Peter left for later when
>> fixing the early-cancel crash in 4f08b870f8 [1].
>>
>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>>
>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>> Cc: Peter Xu <peterx@redhat.com>
>> Cc: Fabiano Rosas <farosas@suse.de>
>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
>> Cc: Laurent Vivier <lvivier@redhat.com>
>>
>> Denis V. Lunev (2):
>> migration: fix source hang on cancel before the channel connects
> Here's my version. It's very opinionated due to my long-standing grudge
> with this code.
> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
>
> -- >8 --
> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
> From: Fabiano Rosas <farosas@suse.de>
> Date: Wed, 25 Nov 2026 18:43:57 -0300
> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
>
> If migration is cancelled at just the right moment when the main
> channel transport has finished connecting, it's possible that the
> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
> entirely. A moment later, migration_channel_connect_outgoing() carries
> along with the migration and initializes the QEMUFiles for the first
> time.
>
> Migration then hangs because the source goes into migration_cleanup()
> due to its state being CANCELLING and hangs waiting for the return
> path thread to join, which never happens because the destination is
> unaware and never sends any SHUT message. The cleanup itself also
> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
> Keep s->rp_state.from_dst_file open until migration ends") has changed
> the code to keep the QEMUFile until after the RP thread exits. The
> only other way to kick it would be from
> stop_return_path_thread_on_source() if an error had occurred, but no
> error has occurred.
>
> This situation is not surprising because the migration cancelling has
> never had any synchronization mechanism aside from the BQL, which in
> this case is not enough to protect from the race. It has also never
> had a way to inform the rest of the code in a thread safe manner about
> the cancellation.
>
> To start moving in the correct direction, make the qmp_migrate_cancel
> command (under BQL) set the migration error, which is protected by the
> error_mutex and suitable to be used as a way to inform the other
> threads of the cancellation. This avoids the need to check the
> MigrationStatus which is already fragile in terms of concurrency.
>
> Note that the QMP handler for migrate_cancel is used instead of the
> internal function because the latter is used from other parts of the
> code that might have set s->error already.
>
> In the case where qmp_migrate_cancel() sets s->error and some other
> code hits an error due to the shutdown()s, the
> migrate_error_propagate() call chain will simply ignore the error set
> for last. This is the correct behavior because errors after cancelling
> are very likely to be caused by the cancellation itself. This has the
> extra benefit that the migration cancellation now shows up in the
> logs, which solves a confusing situation when the user sees "Broken
> pipe" and thinks it's due to an error.
>
> For this specific bug, check that the asynchronous entry to
> migration_channel_connect_outgoing() doesn't bring an already set
> s->error and call migration_connect_error_propagate() under the BQL if
> that's the case. We need the whole migration_connect_error_propagate()
> here to handle the postcopy resume case as well as making sure
> migration_cleanup() is reached.
>
> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
> and my stress branch, which basically skips
> migrate_wait_for_dirty_mem().
>
> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> ---
> migration/channel.c | 5 +++++
> migration/migration.c | 8 ++++++++
> 2 files changed, 13 insertions(+)
>
> diff --git a/migration/channel.c b/migration/channel.c
> index 1aeb8814858..12ff68fc38c 100644
> --- a/migration/channel.c
> +++ b/migration/channel.c
> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
> {
> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
>
> + if (migrate_has_error(s)) {
> + migration_connect_error_propagate(s, error_copy(s->error));
> + return;
> + }
> +
> if (migrate_channel_requires_tls_upgrade(ioc)) {
> Error *local_err = NULL;
>
> diff --git a/migration/migration.c b/migration/migration.c
> index 5686209ab11..40c3f21d520 100644
> --- a/migration/migration.c
> +++ b/migration/migration.c
> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
> return;
> }
>
> + migrate_error_free(migrate_get_current());
> +
> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
> return;
> }
> @@ -2218,6 +2220,9 @@ out:
>
> void qmp_migrate_cancel(Error **errp)
> {
> + MigrationState *s = migrate_get_current();
> + Error *local_err = NULL;
> +
> /*
> * After postcopy migration has started, the source machine is not
> * recoverable in case of a migration error. This also means the
> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
> return;
> }
>
> + error_setg(&local_err, "Migration was cancelled by the user.");
> + migrate_error_propagate(s, local_err);
> +
> migration_cancel();
> }
>
I have spend a bit of time trying to weight against the approaches
and the only item I found is that "cancel" is actually not a error
while we are reusing field as a error state.
Both are working. If you will take your one - pls consider
Tested-by: Denis V. Lunev <den@openvz.org>
Reviewed-by: Denis V. Lunev <den@openvz.org>
I would say - up to you :-)
Regards,
Den
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-26 14:02 ` Denis V. Lunev
@ 2026-11-26 14:38 ` Fabiano Rosas
2026-11-26 19:32 ` Peter Xu
0 siblings, 1 reply; 15+ messages in thread
From: Fabiano Rosas @ 2026-11-26 14:38 UTC (permalink / raw)
To: Denis V. Lunev, Denis V. Lunev, qemu-devel
Cc: Peter Xu, Marc-André Lureau, Laurent Vivier
"Denis V. Lunev" <den@virtuozzo.com> writes:
> On 11/26/26 00:26, Fabiano Rosas wrote:
>> "Denis V. Lunev" <den@openvz.org> writes:
>>
>> What a coincidence, I just spent the afternoon fixing this.
>>
>>> Cancelling a migration right after starting it can hang the source
>>> QEMU forever. This happens when the cancel arrives while the source is
>>> still connecting to the destination and the connection then succeeds.
>>> The source goes on as if nothing was cancelled, then waits for a reply
>>> from the destination that never comes. It waits with the BQL held, so
>>> the monitor stops answering too, and the only way out is to kill the
>>> source. We hit this in an automated test that aborts a live migration
>>> right after starting it.
>> How does this test look like? I'm thinking that instead of writing a new
>> test for this bug (haven't read your patch 2 yet), what we need it to
>> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
>> test. I think that's an artificial delay that's there for no good reason
>> in this case. If your CI manages to hit the issue on a real-ish
>> scenario, that's even more reason to remove that code.
> This is old fashioned product testing over OpenStack cluster
> which specifically performing cancels in a row. Tests are run
> inside VMs inside the product and thus L0 host delays comes
> as a bit of fuzzing.
>
That is good to know because I was not sure whether this issue could
happen outside of migration-test.
> Tests are running non-stop inside development cycle :-)
>
> That is why I am coming with this or that problems on paths
> like that. But this is not very good in terms of QEMU level
> specific tests and that is why patch 2 comes with a pointing
> tests.
>
> NOTE: both your are my patches are fixed the problem as stated
> and the test passes without both your and my variants.
>
>>> Patch 2 adds a qtest for this window, which Peter left for later when
>>> fixing the early-cancel crash in 4f08b870f8 [1].
>>>
>>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>>>
>>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>>> Cc: Peter Xu <peterx@redhat.com>
>>> Cc: Fabiano Rosas <farosas@suse.de>
>>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
>>> Cc: Laurent Vivier <lvivier@redhat.com>
>>>
>>> Denis V. Lunev (2):
>>> migration: fix source hang on cancel before the channel connects
>> Here's my version. It's very opinionated due to my long-standing grudge
>> with this code.
>> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
>>
>> -- >8 --
>> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
>> From: Fabiano Rosas <farosas@suse.de>
>> Date: Wed, 25 Nov 2026 18:43:57 -0300
>> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
>>
>> If migration is cancelled at just the right moment when the main
>> channel transport has finished connecting, it's possible that the
>> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
>> entirely. A moment later, migration_channel_connect_outgoing() carries
>> along with the migration and initializes the QEMUFiles for the first
>> time.
>>
>> Migration then hangs because the source goes into migration_cleanup()
>> due to its state being CANCELLING and hangs waiting for the return
>> path thread to join, which never happens because the destination is
>> unaware and never sends any SHUT message. The cleanup itself also
>> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
>> Keep s->rp_state.from_dst_file open until migration ends") has changed
>> the code to keep the QEMUFile until after the RP thread exits. The
>> only other way to kick it would be from
>> stop_return_path_thread_on_source() if an error had occurred, but no
>> error has occurred.
>>
>> This situation is not surprising because the migration cancelling has
>> never had any synchronization mechanism aside from the BQL, which in
>> this case is not enough to protect from the race. It has also never
>> had a way to inform the rest of the code in a thread safe manner about
>> the cancellation.
>>
>> To start moving in the correct direction, make the qmp_migrate_cancel
>> command (under BQL) set the migration error, which is protected by the
>> error_mutex and suitable to be used as a way to inform the other
>> threads of the cancellation. This avoids the need to check the
>> MigrationStatus which is already fragile in terms of concurrency.
>>
>> Note that the QMP handler for migrate_cancel is used instead of the
>> internal function because the latter is used from other parts of the
>> code that might have set s->error already.
>>
>> In the case where qmp_migrate_cancel() sets s->error and some other
>> code hits an error due to the shutdown()s, the
>> migrate_error_propagate() call chain will simply ignore the error set
>> for last. This is the correct behavior because errors after cancelling
>> are very likely to be caused by the cancellation itself. This has the
>> extra benefit that the migration cancellation now shows up in the
>> logs, which solves a confusing situation when the user sees "Broken
>> pipe" and thinks it's due to an error.
>>
>> For this specific bug, check that the asynchronous entry to
>> migration_channel_connect_outgoing() doesn't bring an already set
>> s->error and call migration_connect_error_propagate() under the BQL if
>> that's the case. We need the whole migration_connect_error_propagate()
>> here to handle the postcopy resume case as well as making sure
>> migration_cleanup() is reached.
>>
>> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
>> and my stress branch, which basically skips
>> migrate_wait_for_dirty_mem().
>>
>> Signed-off-by: Fabiano Rosas <farosas@suse.de>
>> ---
>> migration/channel.c | 5 +++++
>> migration/migration.c | 8 ++++++++
>> 2 files changed, 13 insertions(+)
>>
>> diff --git a/migration/channel.c b/migration/channel.c
>> index 1aeb8814858..12ff68fc38c 100644
>> --- a/migration/channel.c
>> +++ b/migration/channel.c
>> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
>> {
>> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
>>
>> + if (migrate_has_error(s)) {
>> + migration_connect_error_propagate(s, error_copy(s->error));
>> + return;
>> + }
>> +
>> if (migrate_channel_requires_tls_upgrade(ioc)) {
>> Error *local_err = NULL;
>>
>> diff --git a/migration/migration.c b/migration/migration.c
>> index 5686209ab11..40c3f21d520 100644
>> --- a/migration/migration.c
>> +++ b/migration/migration.c
>> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
>> return;
>> }
>>
>> + migrate_error_free(migrate_get_current());
>> +
>> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
>> return;
>> }
>> @@ -2218,6 +2220,9 @@ out:
>>
>> void qmp_migrate_cancel(Error **errp)
>> {
>> + MigrationState *s = migrate_get_current();
>> + Error *local_err = NULL;
>> +
>> /*
>> * After postcopy migration has started, the source machine is not
>> * recoverable in case of a migration error. This also means the
>> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
>> return;
>> }
>>
>> + error_setg(&local_err, "Migration was cancelled by the user.");
>> + migrate_error_propagate(s, local_err);
>> +
>> migration_cancel();
>> }
>>
> I have spend a bit of time trying to weight against the approaches
> and the only item I found is that "cancel" is actually not a error
> while we are reusing field as a error state.
>
That cat is out of the bag already since commit dc487044d5c ("migration:
Make migration_has_failed() work even for CANCELLING"). If
migration_has_failed() includes cancel states, then I think it's
actually a bug that migrate_cancel() doesn't set an error. Otherwise
code that calls migration_has_failed() will have to check
migrate_has_error() to find out if the "failure was an error". That's a
worse pattern in my opinion.
That's how I came up with this patch actually because I was trying to
avoid checking SETUP specifically, in case something else races and sets
another state in the mean time. As I mentioned briefly in the commit
message, the MigrationStatus is not thread-safe, that cmpxchg only
protects the migration event against a duplicated state change.
> Both are working. If you will take your one - pls consider
>
> Tested-by: Denis V. Lunev <den@openvz.org>
> Reviewed-by: Denis V. Lunev <den@openvz.org>
>
> I would say - up to you :-)
>
Thank you. Let's see what Peter thinks about it.
> Regards,
> Den
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-26 14:38 ` Fabiano Rosas
@ 2026-11-26 19:32 ` Peter Xu
2026-11-26 19:39 ` Peter Xu
2026-11-26 21:20 ` Fabiano Rosas
0 siblings, 2 replies; 15+ messages in thread
From: Peter Xu @ 2026-11-26 19:32 UTC (permalink / raw)
To: Fabiano Rosas
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
> "Denis V. Lunev" <den@virtuozzo.com> writes:
>
> > On 11/26/26 00:26, Fabiano Rosas wrote:
> >> "Denis V. Lunev" <den@openvz.org> writes:
> >>
> >> What a coincidence, I just spent the afternoon fixing this.
> >>
> >>> Cancelling a migration right after starting it can hang the source
> >>> QEMU forever. This happens when the cancel arrives while the source is
> >>> still connecting to the destination and the connection then succeeds.
> >>> The source goes on as if nothing was cancelled, then waits for a reply
> >>> from the destination that never comes. It waits with the BQL held, so
> >>> the monitor stops answering too, and the only way out is to kill the
> >>> source. We hit this in an automated test that aborts a live migration
> >>> right after starting it.
> >> How does this test look like? I'm thinking that instead of writing a new
> >> test for this bug (haven't read your patch 2 yet), what we need it to
> >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
> >> test. I think that's an artificial delay that's there for no good reason
> >> in this case. If your CI manages to hit the issue on a real-ish
> >> scenario, that's even more reason to remove that code.
> > This is old fashioned product testing over OpenStack cluster
> > which specifically performing cancels in a row. Tests are run
> > inside VMs inside the product and thus L0 host delays comes
> > as a bit of fuzzing.
> >
>
> That is good to know because I was not sure whether this issue could
> happen outside of migration-test.
>
> > Tests are running non-stop inside development cycle :-)
> >
> > That is why I am coming with this or that problems on paths
> > like that. But this is not very good in terms of QEMU level
> > specific tests and that is why patch 2 comes with a pointing
> > tests.
> >
> > NOTE: both your are my patches are fixed the problem as stated
> > and the test passes without both your and my variants.
> >
> >>> Patch 2 adds a qtest for this window, which Peter left for later when
> >>> fixing the early-cancel crash in 4f08b870f8 [1].
> >>>
> >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
> >>>
> >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
> >>> Cc: Peter Xu <peterx@redhat.com>
> >>> Cc: Fabiano Rosas <farosas@suse.de>
> >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> >>> Cc: Laurent Vivier <lvivier@redhat.com>
> >>>
> >>> Denis V. Lunev (2):
> >>> migration: fix source hang on cancel before the channel connects
> >> Here's my version. It's very opinionated due to my long-standing grudge
> >> with this code.
> >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
> >>
> >> -- >8 --
> >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
> >> From: Fabiano Rosas <farosas@suse.de>
> >> Date: Wed, 25 Nov 2026 18:43:57 -0300
> >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
> >>
> >> If migration is cancelled at just the right moment when the main
> >> channel transport has finished connecting, it's possible that the
> >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
> >> entirely. A moment later, migration_channel_connect_outgoing() carries
> >> along with the migration and initializes the QEMUFiles for the first
> >> time.
> >>
> >> Migration then hangs because the source goes into migration_cleanup()
> >> due to its state being CANCELLING and hangs waiting for the return
> >> path thread to join, which never happens because the destination is
> >> unaware and never sends any SHUT message. The cleanup itself also
> >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
> >> Keep s->rp_state.from_dst_file open until migration ends") has changed
> >> the code to keep the QEMUFile until after the RP thread exits. The
> >> only other way to kick it would be from
> >> stop_return_path_thread_on_source() if an error had occurred, but no
> >> error has occurred.
> >>
> >> This situation is not surprising because the migration cancelling has
> >> never had any synchronization mechanism aside from the BQL, which in
> >> this case is not enough to protect from the race. It has also never
> >> had a way to inform the rest of the code in a thread safe manner about
> >> the cancellation.
> >>
> >> To start moving in the correct direction, make the qmp_migrate_cancel
> >> command (under BQL) set the migration error, which is protected by the
> >> error_mutex and suitable to be used as a way to inform the other
> >> threads of the cancellation. This avoids the need to check the
> >> MigrationStatus which is already fragile in terms of concurrency.
> >>
> >> Note that the QMP handler for migrate_cancel is used instead of the
> >> internal function because the latter is used from other parts of the
> >> code that might have set s->error already.
> >>
> >> In the case where qmp_migrate_cancel() sets s->error and some other
> >> code hits an error due to the shutdown()s, the
> >> migrate_error_propagate() call chain will simply ignore the error set
> >> for last. This is the correct behavior because errors after cancelling
> >> are very likely to be caused by the cancellation itself. This has the
> >> extra benefit that the migration cancellation now shows up in the
> >> logs, which solves a confusing situation when the user sees "Broken
> >> pipe" and thinks it's due to an error.
> >>
> >> For this specific bug, check that the asynchronous entry to
> >> migration_channel_connect_outgoing() doesn't bring an already set
> >> s->error and call migration_connect_error_propagate() under the BQL if
> >> that's the case. We need the whole migration_connect_error_propagate()
> >> here to handle the postcopy resume case as well as making sure
> >> migration_cleanup() is reached.
> >>
> >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
> >> and my stress branch, which basically skips
> >> migrate_wait_for_dirty_mem().
> >>
> >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> >> ---
> >> migration/channel.c | 5 +++++
> >> migration/migration.c | 8 ++++++++
> >> 2 files changed, 13 insertions(+)
> >>
> >> diff --git a/migration/channel.c b/migration/channel.c
> >> index 1aeb8814858..12ff68fc38c 100644
> >> --- a/migration/channel.c
> >> +++ b/migration/channel.c
> >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
> >> {
> >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
> >>
> >> + if (migrate_has_error(s)) {
> >> + migration_connect_error_propagate(s, error_copy(s->error));
[1]
> >> + return;
> >> + }
> >> +
> >> if (migrate_channel_requires_tls_upgrade(ioc)) {
> >> Error *local_err = NULL;
> >>
> >> diff --git a/migration/migration.c b/migration/migration.c
> >> index 5686209ab11..40c3f21d520 100644
> >> --- a/migration/migration.c
> >> +++ b/migration/migration.c
> >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
> >> return;
> >> }
> >>
> >> + migrate_error_free(migrate_get_current());
Is this line somewhat separate from the issue? When s->error will be
non-NULL?
> >> +
> >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
> >> return;
> >> }
> >> @@ -2218,6 +2220,9 @@ out:
> >>
> >> void qmp_migrate_cancel(Error **errp)
> >> {
> >> + MigrationState *s = migrate_get_current();
> >> + Error *local_err = NULL;
> >> +
> >> /*
> >> * After postcopy migration has started, the source machine is not
> >> * recoverable in case of a migration error. This also means the
> >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
> >> return;
> >> }
> >>
> >> + error_setg(&local_err, "Migration was cancelled by the user.");
> >> + migrate_error_propagate(s, local_err);
> >> +
> >> migration_cancel();
> >> }
> >>
> > I have spend a bit of time trying to weight against the approaches
> > and the only item I found is that "cancel" is actually not a error
> > while we are reusing field as a error state.
> >
>
> That cat is out of the bag already since commit dc487044d5c ("migration:
> Make migration_has_failed() work even for CANCELLING"). If
> migration_has_failed() includes cancel states, then I think it's
> actually a bug that migrate_cancel() doesn't set an error. Otherwise
> code that calls migration_has_failed() will have to check
> migrate_has_error() to find out if the "failure was an error". That's a
> worse pattern in my opinion.
Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
sets error for the qemufile error (PS: every time I see these bunch of
error flags I headache..).
>
> That's how I came up with this patch actually because I was trying to
> avoid checking SETUP specifically, in case something else races and sets
> another state in the mean time. As I mentioned briefly in the commit
> message, the MigrationStatus is not thread-safe, that cmpxchg only
> protects the migration event against a duplicated state change.
>
> > Both are working. If you will take your one - pls consider
> >
> > Tested-by: Denis V. Lunev <den@openvz.org>
> > Reviewed-by: Denis V. Lunev <den@openvz.org>
> >
> > I would say - up to you :-)
> >
>
> Thank you. Let's see what Peter thinks about it.
I agree with you that if we already have code treating cancellation as part
of error, then setting error may make things more aligned.
My only complaint might be that above [1], it has "if there is an error,
duplicate that error and try to inject the same error, even if we're
pretty sure is already there, so the dup one will be freed". It was almost
a malloc/free unit test..
So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
instead only at line [1]?
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 2/2] tests/qtest/migration: test cancel before the channel connects
2026-11-25 19:31 ` [PATCH 2/2] tests/qtest/migration: test " Denis V. Lunev
@ 2026-11-26 19:33 ` Peter Xu
0 siblings, 0 replies; 15+ messages in thread
From: Peter Xu @ 2026-11-26 19:33 UTC (permalink / raw)
To: Denis V. Lunev; +Cc: qemu-devel, Fabiano Rosas, Laurent Vivier
On Wed, Nov 25, 2026 at 08:31:02PM +0100, Denis V. Lunev wrote:
> From: Denis V. Lunev <den@openvz.org>
>
> Cancel a migration while the source is still connecting, then let the
> connection succeed. The source must end up in "cancelled", and a
> second migration afterwards must complete.
>
> To keep the source waiting in connect(), the test points it at a unix
> socket of its own whose listen queue is already full. On Linux,
> connect() then blocks until the test accepts a connection, so the test
> is Linux-only.
>
> The return-path capability is on because the hang this test guards
> against is the cleanup waiting for the return path thread.
>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: Fabiano Rosas <farosas@suse.de>
> Cc: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-26 19:32 ` Peter Xu
@ 2026-11-26 19:39 ` Peter Xu
2026-11-26 21:20 ` Fabiano Rosas
1 sibling, 0 replies; 15+ messages in thread
From: Peter Xu @ 2026-11-26 19:39 UTC (permalink / raw)
To: Fabiano Rosas
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
On Thu, Nov 26, 2026 at 02:32:32PM -0500, Peter Xu wrote:
> On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
> > "Denis V. Lunev" <den@virtuozzo.com> writes:
> >
> > > On 11/26/26 00:26, Fabiano Rosas wrote:
> > >> "Denis V. Lunev" <den@openvz.org> writes:
> > >>
> > >> What a coincidence, I just spent the afternoon fixing this.
> > >>
> > >>> Cancelling a migration right after starting it can hang the source
> > >>> QEMU forever. This happens when the cancel arrives while the source is
> > >>> still connecting to the destination and the connection then succeeds.
> > >>> The source goes on as if nothing was cancelled, then waits for a reply
> > >>> from the destination that never comes. It waits with the BQL held, so
> > >>> the monitor stops answering too, and the only way out is to kill the
> > >>> source. We hit this in an automated test that aborts a live migration
> > >>> right after starting it.
> > >> How does this test look like? I'm thinking that instead of writing a new
> > >> test for this bug (haven't read your patch 2 yet), what we need it to
> > >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
> > >> test. I think that's an artificial delay that's there for no good reason
> > >> in this case. If your CI manages to hit the issue on a real-ish
> > >> scenario, that's even more reason to remove that code.
> > > This is old fashioned product testing over OpenStack cluster
> > > which specifically performing cancels in a row. Tests are run
> > > inside VMs inside the product and thus L0 host delays comes
> > > as a bit of fuzzing.
> > >
> >
> > That is good to know because I was not sure whether this issue could
> > happen outside of migration-test.
> >
> > > Tests are running non-stop inside development cycle :-)
> > >
> > > That is why I am coming with this or that problems on paths
> > > like that. But this is not very good in terms of QEMU level
> > > specific tests and that is why patch 2 comes with a pointing
> > > tests.
> > >
> > > NOTE: both your are my patches are fixed the problem as stated
> > > and the test passes without both your and my variants.
> > >
> > >>> Patch 2 adds a qtest for this window, which Peter left for later when
> > >>> fixing the early-cancel crash in 4f08b870f8 [1].
> > >>>
> > >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
> > >>>
> > >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
> > >>> Cc: Peter Xu <peterx@redhat.com>
> > >>> Cc: Fabiano Rosas <farosas@suse.de>
> > >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> > >>> Cc: Laurent Vivier <lvivier@redhat.com>
> > >>>
> > >>> Denis V. Lunev (2):
> > >>> migration: fix source hang on cancel before the channel connects
> > >> Here's my version. It's very opinionated due to my long-standing grudge
> > >> with this code.
> > >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
> > >>
> > >> -- >8 --
> > >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
> > >> From: Fabiano Rosas <farosas@suse.de>
> > >> Date: Wed, 25 Nov 2026 18:43:57 -0300
> > >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
> > >>
> > >> If migration is cancelled at just the right moment when the main
> > >> channel transport has finished connecting, it's possible that the
> > >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
> > >> entirely. A moment later, migration_channel_connect_outgoing() carries
> > >> along with the migration and initializes the QEMUFiles for the first
> > >> time.
> > >>
> > >> Migration then hangs because the source goes into migration_cleanup()
> > >> due to its state being CANCELLING and hangs waiting for the return
> > >> path thread to join, which never happens because the destination is
> > >> unaware and never sends any SHUT message. The cleanup itself also
> > >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
> > >> Keep s->rp_state.from_dst_file open until migration ends") has changed
> > >> the code to keep the QEMUFile until after the RP thread exits. The
> > >> only other way to kick it would be from
> > >> stop_return_path_thread_on_source() if an error had occurred, but no
> > >> error has occurred.
> > >>
> > >> This situation is not surprising because the migration cancelling has
> > >> never had any synchronization mechanism aside from the BQL, which in
> > >> this case is not enough to protect from the race. It has also never
> > >> had a way to inform the rest of the code in a thread safe manner about
> > >> the cancellation.
> > >>
> > >> To start moving in the correct direction, make the qmp_migrate_cancel
> > >> command (under BQL) set the migration error, which is protected by the
> > >> error_mutex and suitable to be used as a way to inform the other
> > >> threads of the cancellation. This avoids the need to check the
> > >> MigrationStatus which is already fragile in terms of concurrency.
> > >>
> > >> Note that the QMP handler for migrate_cancel is used instead of the
> > >> internal function because the latter is used from other parts of the
> > >> code that might have set s->error already.
> > >>
> > >> In the case where qmp_migrate_cancel() sets s->error and some other
> > >> code hits an error due to the shutdown()s, the
> > >> migrate_error_propagate() call chain will simply ignore the error set
> > >> for last. This is the correct behavior because errors after cancelling
> > >> are very likely to be caused by the cancellation itself. This has the
> > >> extra benefit that the migration cancellation now shows up in the
> > >> logs, which solves a confusing situation when the user sees "Broken
> > >> pipe" and thinks it's due to an error.
> > >>
> > >> For this specific bug, check that the asynchronous entry to
> > >> migration_channel_connect_outgoing() doesn't bring an already set
> > >> s->error and call migration_connect_error_propagate() under the BQL if
> > >> that's the case. We need the whole migration_connect_error_propagate()
> > >> here to handle the postcopy resume case as well as making sure
> > >> migration_cleanup() is reached.
> > >>
> > >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
> > >> and my stress branch, which basically skips
> > >> migrate_wait_for_dirty_mem().
> > >>
> > >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> > >> ---
> > >> migration/channel.c | 5 +++++
> > >> migration/migration.c | 8 ++++++++
> > >> 2 files changed, 13 insertions(+)
> > >>
> > >> diff --git a/migration/channel.c b/migration/channel.c
> > >> index 1aeb8814858..12ff68fc38c 100644
> > >> --- a/migration/channel.c
> > >> +++ b/migration/channel.c
> > >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
> > >> {
> > >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
> > >>
> > >> + if (migrate_has_error(s)) {
> > >> + migration_connect_error_propagate(s, error_copy(s->error));
>
> [1]
>
> > >> + return;
> > >> + }
> > >> +
> > >> if (migrate_channel_requires_tls_upgrade(ioc)) {
> > >> Error *local_err = NULL;
> > >>
> > >> diff --git a/migration/migration.c b/migration/migration.c
> > >> index 5686209ab11..40c3f21d520 100644
> > >> --- a/migration/migration.c
> > >> +++ b/migration/migration.c
> > >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
> > >> return;
> > >> }
> > >>
> > >> + migrate_error_free(migrate_get_current());
>
> Is this line somewhat separate from the issue? When s->error will be
> non-NULL?
>
> > >> +
> > >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
> > >> return;
> > >> }
> > >> @@ -2218,6 +2220,9 @@ out:
> > >>
> > >> void qmp_migrate_cancel(Error **errp)
> > >> {
> > >> + MigrationState *s = migrate_get_current();
> > >> + Error *local_err = NULL;
> > >> +
> > >> /*
> > >> * After postcopy migration has started, the source machine is not
> > >> * recoverable in case of a migration error. This also means the
> > >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
> > >> return;
> > >> }
> > >>
> > >> + error_setg(&local_err, "Migration was cancelled by the user.");
> > >> + migrate_error_propagate(s, local_err);
> > >> +
> > >> migration_cancel();
> > >> }
> > >>
> > > I have spend a bit of time trying to weight against the approaches
> > > and the only item I found is that "cancel" is actually not a error
> > > while we are reusing field as a error state.
> > >
> >
> > That cat is out of the bag already since commit dc487044d5c ("migration:
> > Make migration_has_failed() work even for CANCELLING"). If
> > migration_has_failed() includes cancel states, then I think it's
> > actually a bug that migrate_cancel() doesn't set an error. Otherwise
> > code that calls migration_has_failed() will have to check
> > migrate_has_error() to find out if the "failure was an error". That's a
> > worse pattern in my opinion.
>
> Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
> sets error for the qemufile error (PS: every time I see these bunch of
> error flags I headache..).
>
> >
> > That's how I came up with this patch actually because I was trying to
> > avoid checking SETUP specifically, in case something else races and sets
> > another state in the mean time. As I mentioned briefly in the commit
> > message, the MigrationStatus is not thread-safe, that cmpxchg only
> > protects the migration event against a duplicated state change.
> >
> > > Both are working. If you will take your one - pls consider
> > >
> > > Tested-by: Denis V. Lunev <den@openvz.org>
> > > Reviewed-by: Denis V. Lunev <den@openvz.org>
> > >
> > > I would say - up to you :-)
> > >
> >
> > Thank you. Let's see what Peter thinks about it.
>
> I agree with you that if we already have code treating cancellation as part
> of error, then setting error may make things more aligned.
>
> My only complaint might be that above [1], it has "if there is an error,
> duplicate that error and try to inject the same error, even if we're
> pretty sure is already there, so the dup one will be freed". It was almost
> a malloc/free unit test..
>
> So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
> instead only at line [1]?
Or maybe pass NULL to migration_connect_error_propagate() should be
better. However it'll need a touch-up to support NULL only because
trace_migrate_error() will crash, even if the rest seems all fine.
I just noticed there might be weird side effect for postcopy recovery's use
case if we use migrate_cleanup(), so we don't always want to cleanup in
case something set the error during postcopy paused..
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-26 19:32 ` Peter Xu
2026-11-26 19:39 ` Peter Xu
@ 2026-11-26 21:20 ` Fabiano Rosas
2026-11-27 12:22 ` Peter Xu
1 sibling, 1 reply; 15+ messages in thread
From: Fabiano Rosas @ 2026-11-26 21:20 UTC (permalink / raw)
To: Peter Xu
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
Peter Xu <peterx@redhat.com> writes:
> On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
>> "Denis V. Lunev" <den@virtuozzo.com> writes:
>>
>> > On 11/26/26 00:26, Fabiano Rosas wrote:
>> >> "Denis V. Lunev" <den@openvz.org> writes:
>> >>
>> >> What a coincidence, I just spent the afternoon fixing this.
>> >>
>> >>> Cancelling a migration right after starting it can hang the source
>> >>> QEMU forever. This happens when the cancel arrives while the source is
>> >>> still connecting to the destination and the connection then succeeds.
>> >>> The source goes on as if nothing was cancelled, then waits for a reply
>> >>> from the destination that never comes. It waits with the BQL held, so
>> >>> the monitor stops answering too, and the only way out is to kill the
>> >>> source. We hit this in an automated test that aborts a live migration
>> >>> right after starting it.
>> >> How does this test look like? I'm thinking that instead of writing a new
>> >> test for this bug (haven't read your patch 2 yet), what we need it to
>> >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
>> >> test. I think that's an artificial delay that's there for no good reason
>> >> in this case. If your CI manages to hit the issue on a real-ish
>> >> scenario, that's even more reason to remove that code.
>> > This is old fashioned product testing over OpenStack cluster
>> > which specifically performing cancels in a row. Tests are run
>> > inside VMs inside the product and thus L0 host delays comes
>> > as a bit of fuzzing.
>> >
>>
>> That is good to know because I was not sure whether this issue could
>> happen outside of migration-test.
>>
>> > Tests are running non-stop inside development cycle :-)
>> >
>> > That is why I am coming with this or that problems on paths
>> > like that. But this is not very good in terms of QEMU level
>> > specific tests and that is why patch 2 comes with a pointing
>> > tests.
>> >
>> > NOTE: both your are my patches are fixed the problem as stated
>> > and the test passes without both your and my variants.
>> >
>> >>> Patch 2 adds a qtest for this window, which Peter left for later when
>> >>> fixing the early-cancel crash in 4f08b870f8 [1].
>> >>>
>> >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>> >>>
>> >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>> >>> Cc: Peter Xu <peterx@redhat.com>
>> >>> Cc: Fabiano Rosas <farosas@suse.de>
>> >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
>> >>> Cc: Laurent Vivier <lvivier@redhat.com>
>> >>>
>> >>> Denis V. Lunev (2):
>> >>> migration: fix source hang on cancel before the channel connects
>> >> Here's my version. It's very opinionated due to my long-standing grudge
>> >> with this code.
>> >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
>> >>
>> >> -- >8 --
>> >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
>> >> From: Fabiano Rosas <farosas@suse.de>
>> >> Date: Wed, 25 Nov 2026 18:43:57 -0300
>> >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
>> >>
>> >> If migration is cancelled at just the right moment when the main
>> >> channel transport has finished connecting, it's possible that the
>> >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
>> >> entirely. A moment later, migration_channel_connect_outgoing() carries
>> >> along with the migration and initializes the QEMUFiles for the first
>> >> time.
>> >>
>> >> Migration then hangs because the source goes into migration_cleanup()
>> >> due to its state being CANCELLING and hangs waiting for the return
>> >> path thread to join, which never happens because the destination is
>> >> unaware and never sends any SHUT message. The cleanup itself also
>> >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
>> >> Keep s->rp_state.from_dst_file open until migration ends") has changed
>> >> the code to keep the QEMUFile until after the RP thread exits. The
>> >> only other way to kick it would be from
>> >> stop_return_path_thread_on_source() if an error had occurred, but no
>> >> error has occurred.
>> >>
>> >> This situation is not surprising because the migration cancelling has
>> >> never had any synchronization mechanism aside from the BQL, which in
>> >> this case is not enough to protect from the race. It has also never
>> >> had a way to inform the rest of the code in a thread safe manner about
>> >> the cancellation.
>> >>
>> >> To start moving in the correct direction, make the qmp_migrate_cancel
>> >> command (under BQL) set the migration error, which is protected by the
>> >> error_mutex and suitable to be used as a way to inform the other
>> >> threads of the cancellation. This avoids the need to check the
>> >> MigrationStatus which is already fragile in terms of concurrency.
>> >>
>> >> Note that the QMP handler for migrate_cancel is used instead of the
>> >> internal function because the latter is used from other parts of the
>> >> code that might have set s->error already.
>> >>
>> >> In the case where qmp_migrate_cancel() sets s->error and some other
>> >> code hits an error due to the shutdown()s, the
>> >> migrate_error_propagate() call chain will simply ignore the error set
>> >> for last. This is the correct behavior because errors after cancelling
>> >> are very likely to be caused by the cancellation itself. This has the
>> >> extra benefit that the migration cancellation now shows up in the
>> >> logs, which solves a confusing situation when the user sees "Broken
>> >> pipe" and thinks it's due to an error.
>> >>
>> >> For this specific bug, check that the asynchronous entry to
>> >> migration_channel_connect_outgoing() doesn't bring an already set
>> >> s->error and call migration_connect_error_propagate() under the BQL if
>> >> that's the case. We need the whole migration_connect_error_propagate()
>> >> here to handle the postcopy resume case as well as making sure
>> >> migration_cleanup() is reached.
>> >>
>> >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
>> >> and my stress branch, which basically skips
>> >> migrate_wait_for_dirty_mem().
>> >>
>> >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
>> >> ---
>> >> migration/channel.c | 5 +++++
>> >> migration/migration.c | 8 ++++++++
>> >> 2 files changed, 13 insertions(+)
>> >>
>> >> diff --git a/migration/channel.c b/migration/channel.c
>> >> index 1aeb8814858..12ff68fc38c 100644
>> >> --- a/migration/channel.c
>> >> +++ b/migration/channel.c
>> >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
>> >> {
>> >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
>> >>
>> >> + if (migrate_has_error(s)) {
>> >> + migration_connect_error_propagate(s, error_copy(s->error));
>
> [1]
>
>> >> + return;
>> >> + }
>> >> +
>> >> if (migrate_channel_requires_tls_upgrade(ioc)) {
>> >> Error *local_err = NULL;
>> >>
>> >> diff --git a/migration/migration.c b/migration/migration.c
>> >> index 5686209ab11..40c3f21d520 100644
>> >> --- a/migration/migration.c
>> >> +++ b/migration/migration.c
>> >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
>> >> return;
>> >> }
>> >>
>> >> + migrate_error_free(migrate_get_current());
>
> Is this line somewhat separate from the issue? When s->error will be
> non-NULL?
>
One of the cancel tests issues migrate_cancel(to), that puts the error
in s->error and since QEMU keeps running, a subsequent migration
crashes. We free the error at migrate_init() and migrate_prepare() for
similar reasons I think.
>> >> +
>> >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
>> >> return;
>> >> }
>> >> @@ -2218,6 +2220,9 @@ out:
>> >>
>> >> void qmp_migrate_cancel(Error **errp)
>> >> {
>> >> + MigrationState *s = migrate_get_current();
>> >> + Error *local_err = NULL;
>> >> +
>> >> /*
>> >> * After postcopy migration has started, the source machine is not
>> >> * recoverable in case of a migration error. This also means the
>> >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
>> >> return;
>> >> }
>> >>
>> >> + error_setg(&local_err, "Migration was cancelled by the user.");
>> >> + migrate_error_propagate(s, local_err);
>> >> +
>> >> migration_cancel();
>> >> }
>> >>
>> > I have spend a bit of time trying to weight against the approaches
>> > and the only item I found is that "cancel" is actually not a error
>> > while we are reusing field as a error state.
>> >
>>
>> That cat is out of the bag already since commit dc487044d5c ("migration:
>> Make migration_has_failed() work even for CANCELLING"). If
>> migration_has_failed() includes cancel states, then I think it's
>> actually a bug that migrate_cancel() doesn't set an error. Otherwise
>> code that calls migration_has_failed() will have to check
>> migrate_has_error() to find out if the "failure was an error". That's a
>> worse pattern in my opinion.
>
> Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
> sets error for the qemufile error (PS: every time I see these bunch of
> error flags I headache..).
>
>>
>> That's how I came up with this patch actually because I was trying to
>> avoid checking SETUP specifically, in case something else races and sets
>> another state in the mean time. As I mentioned briefly in the commit
>> message, the MigrationStatus is not thread-safe, that cmpxchg only
>> protects the migration event against a duplicated state change.
>>
>> > Both are working. If you will take your one - pls consider
>> >
>> > Tested-by: Denis V. Lunev <den@openvz.org>
>> > Reviewed-by: Denis V. Lunev <den@openvz.org>
>> >
>> > I would say - up to you :-)
>> >
>>
>> Thank you. Let's see what Peter thinks about it.
>
> I agree with you that if we already have code treating cancellation as part
> of error, then setting error may make things more aligned.
>
> My only complaint might be that above [1], it has "if there is an error,
> duplicate that error and try to inject the same error, even if we're
> pretty sure is already there, so the dup one will be freed". It was almost
> a malloc/free unit test..
>
Hmm, maybe NULL is indeed better, let me check. We just need to make
sure the error is set because the RP closing path then gets one more
chance to call shutdown(). But yeah, migrate_has_error() already
guarantees that.
> So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
> instead only at line [1]?
As you noticed, it has to be migration_connect_error_propagate() because
it handles postcopy. I also don't want to have one more loose call to
migration_cleanup(), it makes it difficult to think about concurrency
bugs. The more we can centralize on a single error path, the better.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-26 21:20 ` Fabiano Rosas
@ 2026-11-27 12:22 ` Peter Xu
2026-11-27 12:49 ` Fabiano Rosas
0 siblings, 1 reply; 15+ messages in thread
From: Peter Xu @ 2026-11-27 12:22 UTC (permalink / raw)
To: Fabiano Rosas
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
On Thu, Nov 26, 2026 at 06:20:03PM -0300, Fabiano Rosas wrote:
> Peter Xu <peterx@redhat.com> writes:
>
> > On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
> >> "Denis V. Lunev" <den@virtuozzo.com> writes:
> >>
> >> > On 11/26/26 00:26, Fabiano Rosas wrote:
> >> >> "Denis V. Lunev" <den@openvz.org> writes:
> >> >>
> >> >> What a coincidence, I just spent the afternoon fixing this.
> >> >>
> >> >>> Cancelling a migration right after starting it can hang the source
> >> >>> QEMU forever. This happens when the cancel arrives while the source is
> >> >>> still connecting to the destination and the connection then succeeds.
> >> >>> The source goes on as if nothing was cancelled, then waits for a reply
> >> >>> from the destination that never comes. It waits with the BQL held, so
> >> >>> the monitor stops answering too, and the only way out is to kill the
> >> >>> source. We hit this in an automated test that aborts a live migration
> >> >>> right after starting it.
> >> >> How does this test look like? I'm thinking that instead of writing a new
> >> >> test for this bug (haven't read your patch 2 yet), what we need it to
> >> >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
> >> >> test. I think that's an artificial delay that's there for no good reason
> >> >> in this case. If your CI manages to hit the issue on a real-ish
> >> >> scenario, that's even more reason to remove that code.
> >> > This is old fashioned product testing over OpenStack cluster
> >> > which specifically performing cancels in a row. Tests are run
> >> > inside VMs inside the product and thus L0 host delays comes
> >> > as a bit of fuzzing.
> >> >
> >>
> >> That is good to know because I was not sure whether this issue could
> >> happen outside of migration-test.
> >>
> >> > Tests are running non-stop inside development cycle :-)
> >> >
> >> > That is why I am coming with this or that problems on paths
> >> > like that. But this is not very good in terms of QEMU level
> >> > specific tests and that is why patch 2 comes with a pointing
> >> > tests.
> >> >
> >> > NOTE: both your are my patches are fixed the problem as stated
> >> > and the test passes without both your and my variants.
> >> >
> >> >>> Patch 2 adds a qtest for this window, which Peter left for later when
> >> >>> fixing the early-cancel crash in 4f08b870f8 [1].
> >> >>>
> >> >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
> >> >>>
> >> >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
> >> >>> Cc: Peter Xu <peterx@redhat.com>
> >> >>> Cc: Fabiano Rosas <farosas@suse.de>
> >> >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> >> >>> Cc: Laurent Vivier <lvivier@redhat.com>
> >> >>>
> >> >>> Denis V. Lunev (2):
> >> >>> migration: fix source hang on cancel before the channel connects
> >> >> Here's my version. It's very opinionated due to my long-standing grudge
> >> >> with this code.
> >> >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
> >> >>
> >> >> -- >8 --
> >> >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
> >> >> From: Fabiano Rosas <farosas@suse.de>
> >> >> Date: Wed, 25 Nov 2026 18:43:57 -0300
> >> >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
> >> >>
> >> >> If migration is cancelled at just the right moment when the main
> >> >> channel transport has finished connecting, it's possible that the
> >> >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
> >> >> entirely. A moment later, migration_channel_connect_outgoing() carries
> >> >> along with the migration and initializes the QEMUFiles for the first
> >> >> time.
> >> >>
> >> >> Migration then hangs because the source goes into migration_cleanup()
> >> >> due to its state being CANCELLING and hangs waiting for the return
> >> >> path thread to join, which never happens because the destination is
> >> >> unaware and never sends any SHUT message. The cleanup itself also
> >> >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
> >> >> Keep s->rp_state.from_dst_file open until migration ends") has changed
> >> >> the code to keep the QEMUFile until after the RP thread exits. The
> >> >> only other way to kick it would be from
> >> >> stop_return_path_thread_on_source() if an error had occurred, but no
> >> >> error has occurred.
> >> >>
> >> >> This situation is not surprising because the migration cancelling has
> >> >> never had any synchronization mechanism aside from the BQL, which in
> >> >> this case is not enough to protect from the race. It has also never
> >> >> had a way to inform the rest of the code in a thread safe manner about
> >> >> the cancellation.
> >> >>
> >> >> To start moving in the correct direction, make the qmp_migrate_cancel
> >> >> command (under BQL) set the migration error, which is protected by the
> >> >> error_mutex and suitable to be used as a way to inform the other
> >> >> threads of the cancellation. This avoids the need to check the
> >> >> MigrationStatus which is already fragile in terms of concurrency.
> >> >>
> >> >> Note that the QMP handler for migrate_cancel is used instead of the
> >> >> internal function because the latter is used from other parts of the
> >> >> code that might have set s->error already.
> >> >>
> >> >> In the case where qmp_migrate_cancel() sets s->error and some other
> >> >> code hits an error due to the shutdown()s, the
> >> >> migrate_error_propagate() call chain will simply ignore the error set
> >> >> for last. This is the correct behavior because errors after cancelling
> >> >> are very likely to be caused by the cancellation itself. This has the
> >> >> extra benefit that the migration cancellation now shows up in the
> >> >> logs, which solves a confusing situation when the user sees "Broken
> >> >> pipe" and thinks it's due to an error.
> >> >>
> >> >> For this specific bug, check that the asynchronous entry to
> >> >> migration_channel_connect_outgoing() doesn't bring an already set
> >> >> s->error and call migration_connect_error_propagate() under the BQL if
> >> >> that's the case. We need the whole migration_connect_error_propagate()
> >> >> here to handle the postcopy resume case as well as making sure
> >> >> migration_cleanup() is reached.
> >> >>
> >> >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
> >> >> and my stress branch, which basically skips
> >> >> migrate_wait_for_dirty_mem().
> >> >>
> >> >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> >> >> ---
> >> >> migration/channel.c | 5 +++++
> >> >> migration/migration.c | 8 ++++++++
> >> >> 2 files changed, 13 insertions(+)
> >> >>
> >> >> diff --git a/migration/channel.c b/migration/channel.c
> >> >> index 1aeb8814858..12ff68fc38c 100644
> >> >> --- a/migration/channel.c
> >> >> +++ b/migration/channel.c
> >> >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
> >> >> {
> >> >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
> >> >>
> >> >> + if (migrate_has_error(s)) {
> >> >> + migration_connect_error_propagate(s, error_copy(s->error));
> >
> > [1]
> >
> >> >> + return;
> >> >> + }
> >> >> +
> >> >> if (migrate_channel_requires_tls_upgrade(ioc)) {
> >> >> Error *local_err = NULL;
> >> >>
> >> >> diff --git a/migration/migration.c b/migration/migration.c
> >> >> index 5686209ab11..40c3f21d520 100644
> >> >> --- a/migration/migration.c
> >> >> +++ b/migration/migration.c
> >> >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
> >> >> return;
> >> >> }
> >> >>
> >> >> + migrate_error_free(migrate_get_current());
> >
> > Is this line somewhat separate from the issue? When s->error will be
> > non-NULL?
> >
>
> One of the cancel tests issues migrate_cancel(to), that puts the error
> in s->error and since QEMU keeps running, a subsequent migration
> crashes. We free the error at migrate_init() and migrate_prepare() for
> similar reasons I think.
But this is migrate_incoming, which means if something got wrong, instead
of retrying, we should kill the qemu and re-initiate? I don't know if we
could have other loopholes on this path to be invoked more than once.. as I
never expected it to run twice (either success and take over, or halted for
query error only, or killed).
>
> >> >> +
> >> >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
> >> >> return;
> >> >> }
> >> >> @@ -2218,6 +2220,9 @@ out:
> >> >>
> >> >> void qmp_migrate_cancel(Error **errp)
> >> >> {
> >> >> + MigrationState *s = migrate_get_current();
> >> >> + Error *local_err = NULL;
> >> >> +
> >> >> /*
> >> >> * After postcopy migration has started, the source machine is not
> >> >> * recoverable in case of a migration error. This also means the
> >> >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
> >> >> return;
> >> >> }
> >> >>
> >> >> + error_setg(&local_err, "Migration was cancelled by the user.");
> >> >> + migrate_error_propagate(s, local_err);
> >> >> +
> >> >> migration_cancel();
> >> >> }
> >> >>
> >> > I have spend a bit of time trying to weight against the approaches
> >> > and the only item I found is that "cancel" is actually not a error
> >> > while we are reusing field as a error state.
> >> >
> >>
> >> That cat is out of the bag already since commit dc487044d5c ("migration:
> >> Make migration_has_failed() work even for CANCELLING"). If
> >> migration_has_failed() includes cancel states, then I think it's
> >> actually a bug that migrate_cancel() doesn't set an error. Otherwise
> >> code that calls migration_has_failed() will have to check
> >> migrate_has_error() to find out if the "failure was an error". That's a
> >> worse pattern in my opinion.
> >
> > Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
> > sets error for the qemufile error (PS: every time I see these bunch of
> > error flags I headache..).
> >
> >>
> >> That's how I came up with this patch actually because I was trying to
> >> avoid checking SETUP specifically, in case something else races and sets
> >> another state in the mean time. As I mentioned briefly in the commit
> >> message, the MigrationStatus is not thread-safe, that cmpxchg only
> >> protects the migration event against a duplicated state change.
> >>
> >> > Both are working. If you will take your one - pls consider
> >> >
> >> > Tested-by: Denis V. Lunev <den@openvz.org>
> >> > Reviewed-by: Denis V. Lunev <den@openvz.org>
> >> >
> >> > I would say - up to you :-)
> >> >
> >>
> >> Thank you. Let's see what Peter thinks about it.
> >
> > I agree with you that if we already have code treating cancellation as part
> > of error, then setting error may make things more aligned.
> >
> > My only complaint might be that above [1], it has "if there is an error,
> > duplicate that error and try to inject the same error, even if we're
> > pretty sure is already there, so the dup one will be freed". It was almost
> > a malloc/free unit test..
> >
>
> Hmm, maybe NULL is indeed better, let me check. We just need to make
> sure the error is set because the RP closing path then gets one more
> chance to call shutdown(). But yeah, migrate_has_error() already
> guarantees that.
Yep.
>
> > So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
> > instead only at line [1]?
>
> As you noticed, it has to be migration_connect_error_propagate() because
> it handles postcopy. I also don't want to have one more loose call to
> migration_cleanup(), it makes it difficult to think about concurrency
> bugs. The more we can centralize on a single error path, the better.
Yes I agree.
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-27 12:22 ` Peter Xu
@ 2026-11-27 12:49 ` Fabiano Rosas
2026-11-27 13:28 ` Peter Xu
0 siblings, 1 reply; 15+ messages in thread
From: Fabiano Rosas @ 2026-11-27 12:49 UTC (permalink / raw)
To: Peter Xu
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
Peter Xu <peterx@redhat.com> writes:
> On Thu, Nov 26, 2026 at 06:20:03PM -0300, Fabiano Rosas wrote:
>> Peter Xu <peterx@redhat.com> writes:
>>
>> > On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
>> >> "Denis V. Lunev" <den@virtuozzo.com> writes:
>> >>
>> >> > On 11/26/26 00:26, Fabiano Rosas wrote:
>> >> >> "Denis V. Lunev" <den@openvz.org> writes:
>> >> >>
>> >> >> What a coincidence, I just spent the afternoon fixing this.
>> >> >>
>> >> >>> Cancelling a migration right after starting it can hang the source
>> >> >>> QEMU forever. This happens when the cancel arrives while the source is
>> >> >>> still connecting to the destination and the connection then succeeds.
>> >> >>> The source goes on as if nothing was cancelled, then waits for a reply
>> >> >>> from the destination that never comes. It waits with the BQL held, so
>> >> >>> the monitor stops answering too, and the only way out is to kill the
>> >> >>> source. We hit this in an automated test that aborts a live migration
>> >> >>> right after starting it.
>> >> >> How does this test look like? I'm thinking that instead of writing a new
>> >> >> test for this bug (haven't read your patch 2 yet), what we need it to
>> >> >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
>> >> >> test. I think that's an artificial delay that's there for no good reason
>> >> >> in this case. If your CI manages to hit the issue on a real-ish
>> >> >> scenario, that's even more reason to remove that code.
>> >> > This is old fashioned product testing over OpenStack cluster
>> >> > which specifically performing cancels in a row. Tests are run
>> >> > inside VMs inside the product and thus L0 host delays comes
>> >> > as a bit of fuzzing.
>> >> >
>> >>
>> >> That is good to know because I was not sure whether this issue could
>> >> happen outside of migration-test.
>> >>
>> >> > Tests are running non-stop inside development cycle :-)
>> >> >
>> >> > That is why I am coming with this or that problems on paths
>> >> > like that. But this is not very good in terms of QEMU level
>> >> > specific tests and that is why patch 2 comes with a pointing
>> >> > tests.
>> >> >
>> >> > NOTE: both your are my patches are fixed the problem as stated
>> >> > and the test passes without both your and my variants.
>> >> >
>> >> >>> Patch 2 adds a qtest for this window, which Peter left for later when
>> >> >>> fixing the early-cancel crash in 4f08b870f8 [1].
>> >> >>>
>> >> >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>> >> >>>
>> >> >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>> >> >>> Cc: Peter Xu <peterx@redhat.com>
>> >> >>> Cc: Fabiano Rosas <farosas@suse.de>
>> >> >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
>> >> >>> Cc: Laurent Vivier <lvivier@redhat.com>
>> >> >>>
>> >> >>> Denis V. Lunev (2):
>> >> >>> migration: fix source hang on cancel before the channel connects
>> >> >> Here's my version. It's very opinionated due to my long-standing grudge
>> >> >> with this code.
>> >> >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
>> >> >>
>> >> >> -- >8 --
>> >> >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
>> >> >> From: Fabiano Rosas <farosas@suse.de>
>> >> >> Date: Wed, 25 Nov 2026 18:43:57 -0300
>> >> >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
>> >> >>
>> >> >> If migration is cancelled at just the right moment when the main
>> >> >> channel transport has finished connecting, it's possible that the
>> >> >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
>> >> >> entirely. A moment later, migration_channel_connect_outgoing() carries
>> >> >> along with the migration and initializes the QEMUFiles for the first
>> >> >> time.
>> >> >>
>> >> >> Migration then hangs because the source goes into migration_cleanup()
>> >> >> due to its state being CANCELLING and hangs waiting for the return
>> >> >> path thread to join, which never happens because the destination is
>> >> >> unaware and never sends any SHUT message. The cleanup itself also
>> >> >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
>> >> >> Keep s->rp_state.from_dst_file open until migration ends") has changed
>> >> >> the code to keep the QEMUFile until after the RP thread exits. The
>> >> >> only other way to kick it would be from
>> >> >> stop_return_path_thread_on_source() if an error had occurred, but no
>> >> >> error has occurred.
>> >> >>
>> >> >> This situation is not surprising because the migration cancelling has
>> >> >> never had any synchronization mechanism aside from the BQL, which in
>> >> >> this case is not enough to protect from the race. It has also never
>> >> >> had a way to inform the rest of the code in a thread safe manner about
>> >> >> the cancellation.
>> >> >>
>> >> >> To start moving in the correct direction, make the qmp_migrate_cancel
>> >> >> command (under BQL) set the migration error, which is protected by the
>> >> >> error_mutex and suitable to be used as a way to inform the other
>> >> >> threads of the cancellation. This avoids the need to check the
>> >> >> MigrationStatus which is already fragile in terms of concurrency.
>> >> >>
>> >> >> Note that the QMP handler for migrate_cancel is used instead of the
>> >> >> internal function because the latter is used from other parts of the
>> >> >> code that might have set s->error already.
>> >> >>
>> >> >> In the case where qmp_migrate_cancel() sets s->error and some other
>> >> >> code hits an error due to the shutdown()s, the
>> >> >> migrate_error_propagate() call chain will simply ignore the error set
>> >> >> for last. This is the correct behavior because errors after cancelling
>> >> >> are very likely to be caused by the cancellation itself. This has the
>> >> >> extra benefit that the migration cancellation now shows up in the
>> >> >> logs, which solves a confusing situation when the user sees "Broken
>> >> >> pipe" and thinks it's due to an error.
>> >> >>
>> >> >> For this specific bug, check that the asynchronous entry to
>> >> >> migration_channel_connect_outgoing() doesn't bring an already set
>> >> >> s->error and call migration_connect_error_propagate() under the BQL if
>> >> >> that's the case. We need the whole migration_connect_error_propagate()
>> >> >> here to handle the postcopy resume case as well as making sure
>> >> >> migration_cleanup() is reached.
>> >> >>
>> >> >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
>> >> >> and my stress branch, which basically skips
>> >> >> migrate_wait_for_dirty_mem().
>> >> >>
>> >> >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
>> >> >> ---
>> >> >> migration/channel.c | 5 +++++
>> >> >> migration/migration.c | 8 ++++++++
>> >> >> 2 files changed, 13 insertions(+)
>> >> >>
>> >> >> diff --git a/migration/channel.c b/migration/channel.c
>> >> >> index 1aeb8814858..12ff68fc38c 100644
>> >> >> --- a/migration/channel.c
>> >> >> +++ b/migration/channel.c
>> >> >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
>> >> >> {
>> >> >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
>> >> >>
>> >> >> + if (migrate_has_error(s)) {
>> >> >> + migration_connect_error_propagate(s, error_copy(s->error));
>> >
>> > [1]
>> >
>> >> >> + return;
>> >> >> + }
>> >> >> +
>> >> >> if (migrate_channel_requires_tls_upgrade(ioc)) {
>> >> >> Error *local_err = NULL;
>> >> >>
>> >> >> diff --git a/migration/migration.c b/migration/migration.c
>> >> >> index 5686209ab11..40c3f21d520 100644
>> >> >> --- a/migration/migration.c
>> >> >> +++ b/migration/migration.c
>> >> >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
>> >> >> return;
>> >> >> }
>> >> >>
>> >> >> + migrate_error_free(migrate_get_current());
>> >
>> > Is this line somewhat separate from the issue? When s->error will be
>> > non-NULL?
>> >
>>
>> One of the cancel tests issues migrate_cancel(to), that puts the error
>> in s->error and since QEMU keeps running, a subsequent migration
>> crashes. We free the error at migrate_init() and migrate_prepare() for
>> similar reasons I think.
>
> But this is migrate_incoming, which means if something got wrong, instead
> of retrying, we should kill the qemu and re-initiate? I don't know if we
> could have other loopholes on this path to be invoked more than once.. as I
> never expected it to run twice (either success and take over, or halted for
> query error only, or killed).
>
I think this is only about calling cancel _without_ any migration:
$ (echo "migrate_cancel"; echo "quit") | ~/qemu-system-x86_64 -S -monitor stdio -incoming defer -trace migration_cancel
QEMU 11.1.92 monitor - type 'help' for more information
(qemu) migrate_cancel
migration_cancel
(qemu) quit
migration_cancel
So a state check in there could be good enough.
>>
>> >> >> +
>> >> >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
>> >> >> return;
>> >> >> }
>> >> >> @@ -2218,6 +2220,9 @@ out:
>> >> >>
>> >> >> void qmp_migrate_cancel(Error **errp)
>> >> >> {
>> >> >> + MigrationState *s = migrate_get_current();
>> >> >> + Error *local_err = NULL;
>> >> >> +
>> >> >> /*
>> >> >> * After postcopy migration has started, the source machine is not
>> >> >> * recoverable in case of a migration error. This also means the
>> >> >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
>> >> >> return;
>> >> >> }
>> >> >>
>> >> >> + error_setg(&local_err, "Migration was cancelled by the user.");
>> >> >> + migrate_error_propagate(s, local_err);
>> >> >> +
>> >> >> migration_cancel();
>> >> >> }
>> >> >>
>> >> > I have spend a bit of time trying to weight against the approaches
>> >> > and the only item I found is that "cancel" is actually not a error
>> >> > while we are reusing field as a error state.
>> >> >
>> >>
>> >> That cat is out of the bag already since commit dc487044d5c ("migration:
>> >> Make migration_has_failed() work even for CANCELLING"). If
>> >> migration_has_failed() includes cancel states, then I think it's
>> >> actually a bug that migrate_cancel() doesn't set an error. Otherwise
>> >> code that calls migration_has_failed() will have to check
>> >> migrate_has_error() to find out if the "failure was an error". That's a
>> >> worse pattern in my opinion.
>> >
>> > Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
>> > sets error for the qemufile error (PS: every time I see these bunch of
>> > error flags I headache..).
>> >
>> >>
>> >> That's how I came up with this patch actually because I was trying to
>> >> avoid checking SETUP specifically, in case something else races and sets
>> >> another state in the mean time. As I mentioned briefly in the commit
>> >> message, the MigrationStatus is not thread-safe, that cmpxchg only
>> >> protects the migration event against a duplicated state change.
>> >>
>> >> > Both are working. If you will take your one - pls consider
>> >> >
>> >> > Tested-by: Denis V. Lunev <den@openvz.org>
>> >> > Reviewed-by: Denis V. Lunev <den@openvz.org>
>> >> >
>> >> > I would say - up to you :-)
>> >> >
>> >>
>> >> Thank you. Let's see what Peter thinks about it.
>> >
>> > I agree with you that if we already have code treating cancellation as part
>> > of error, then setting error may make things more aligned.
>> >
>> > My only complaint might be that above [1], it has "if there is an error,
>> > duplicate that error and try to inject the same error, even if we're
>> > pretty sure is already there, so the dup one will be freed". It was almost
>> > a malloc/free unit test..
>> >
>>
>> Hmm, maybe NULL is indeed better, let me check. We just need to make
>> sure the error is set because the RP closing path then gets one more
>> chance to call shutdown(). But yeah, migrate_has_error() already
>> guarantees that.
>
> Yep.
>
>>
>> > So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
>> > instead only at line [1]?
>>
>> As you noticed, it has to be migration_connect_error_propagate() because
>> it handles postcopy. I also don't want to have one more loose call to
>> migration_cleanup(), it makes it difficult to think about concurrency
>> bugs. The more we can centralize on a single error path, the better.
>
> Yes I agree.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-27 12:49 ` Fabiano Rosas
@ 2026-11-27 13:28 ` Peter Xu
2026-11-27 14:21 ` Fabiano Rosas
0 siblings, 1 reply; 15+ messages in thread
From: Peter Xu @ 2026-11-27 13:28 UTC (permalink / raw)
To: Fabiano Rosas
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
On Fri, Nov 27, 2026 at 09:49:50AM -0300, Fabiano Rosas wrote:
> >> One of the cancel tests issues migrate_cancel(to), that puts the error
> >> in s->error and since QEMU keeps running, a subsequent migration
> >> crashes. We free the error at migrate_init() and migrate_prepare() for
> >> similar reasons I think.
> >
> > But this is migrate_incoming, which means if something got wrong, instead
> > of retrying, we should kill the qemu and re-initiate? I don't know if we
> > could have other loopholes on this path to be invoked more than once.. as I
> > never expected it to run twice (either success and take over, or halted for
> > query error only, or killed).
> >
>
> I think this is only about calling cancel _without_ any migration:
>
> $ (echo "migrate_cancel"; echo "quit") | ~/qemu-system-x86_64 -S -monitor stdio -incoming defer -trace migration_cancel
> QEMU 11.1.92 monitor - type 'help' for more information
> (qemu) migrate_cancel
> migration_cancel
> (qemu) quit
> migration_cancel
>
> So a state check in there could be good enough.
Ouch, so it runs through even without migration? :(
If the "state check" you mentioned means failing migrate_cancel() when
!migration_is_running(), I definitely agree.
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-27 13:28 ` Peter Xu
@ 2026-11-27 14:21 ` Fabiano Rosas
2026-11-27 14:59 ` Peter Xu
0 siblings, 1 reply; 15+ messages in thread
From: Fabiano Rosas @ 2026-11-27 14:21 UTC (permalink / raw)
To: Peter Xu
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
Peter Xu <peterx@redhat.com> writes:
> On Fri, Nov 27, 2026 at 09:49:50AM -0300, Fabiano Rosas wrote:
>> >> One of the cancel tests issues migrate_cancel(to), that puts the error
>> >> in s->error and since QEMU keeps running, a subsequent migration
>> >> crashes. We free the error at migrate_init() and migrate_prepare() for
>> >> similar reasons I think.
>> >
>> > But this is migrate_incoming, which means if something got wrong, instead
>> > of retrying, we should kill the qemu and re-initiate? I don't know if we
>> > could have other loopholes on this path to be invoked more than once.. as I
>> > never expected it to run twice (either success and take over, or halted for
>> > query error only, or killed).
>> >
>>
>> I think this is only about calling cancel _without_ any migration:
>>
>> $ (echo "migrate_cancel"; echo "quit") | ~/qemu-system-x86_64 -S -monitor stdio -incoming defer -trace migration_cancel
>> QEMU 11.1.92 monitor - type 'help' for more information
>> (qemu) migrate_cancel
>> migration_cancel
>> (qemu) quit
>> migration_cancel
>>
>> So a state check in there could be good enough.
>
> Ouch, so it runs through even without migration? :(
>
> If the "state check" you mentioned means failing migrate_cancel() when
> !migration_is_running(), I definitely agree.
Are there any consequences for the user in failing the command? We could
also simply skip.
I'll pickup patch 2 from Denis and put into a series along with this^
check.
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
2026-11-27 14:21 ` Fabiano Rosas
@ 2026-11-27 14:59 ` Peter Xu
0 siblings, 0 replies; 15+ messages in thread
From: Peter Xu @ 2026-11-27 14:59 UTC (permalink / raw)
To: Fabiano Rosas
Cc: Denis V. Lunev, Denis V. Lunev, qemu-devel,
Marc-André Lureau, Laurent Vivier
On Fri, Nov 27, 2026 at 11:21:08AM -0300, Fabiano Rosas wrote:
> Peter Xu <peterx@redhat.com> writes:
>
> > On Fri, Nov 27, 2026 at 09:49:50AM -0300, Fabiano Rosas wrote:
> >> >> One of the cancel tests issues migrate_cancel(to), that puts the error
> >> >> in s->error and since QEMU keeps running, a subsequent migration
> >> >> crashes. We free the error at migrate_init() and migrate_prepare() for
> >> >> similar reasons I think.
> >> >
> >> > But this is migrate_incoming, which means if something got wrong, instead
> >> > of retrying, we should kill the qemu and re-initiate? I don't know if we
> >> > could have other loopholes on this path to be invoked more than once.. as I
> >> > never expected it to run twice (either success and take over, or halted for
> >> > query error only, or killed).
> >> >
> >>
> >> I think this is only about calling cancel _without_ any migration:
> >>
> >> $ (echo "migrate_cancel"; echo "quit") | ~/qemu-system-x86_64 -S -monitor stdio -incoming defer -trace migration_cancel
> >> QEMU 11.1.92 monitor - type 'help' for more information
> >> (qemu) migrate_cancel
> >> migration_cancel
> >> (qemu) quit
> >> migration_cancel
> >>
> >> So a state check in there could be good enough.
> >
> > Ouch, so it runs through even without migration? :(
> >
> > If the "state check" you mentioned means failing migrate_cancel() when
> > !migration_is_running(), I definitely agree.
>
> Are there any consequences for the user in failing the command? We could
> also simply skip.
The thing is migrate_cancel() with no migration at all is more or less,
wrong...
If some test runs it and succeeded assuming it cancelled something, but in
reality the migration didn't ever start, it could be a test race / bug,
there's OTOH benefit changing the ABI to start let tester notice something
wrong with the test.
So if we don't breat libvirt in any way (hopefully!), then I think we can
still do the ABI break, considering in good way, at start of dev cycle opens.
>
> I'll pickup patch 2 from Denis and put into a series along with this^
> check.
Sounds good!
--
Peter Xu
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2026-11-27 14:59 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 19:31 [PATCH 0/2] migration: fix a source hang on cancel before the channel connects Denis V. Lunev
2026-11-25 19:31 ` [PATCH 1/2] migration: fix " Denis V. Lunev
2026-11-25 19:31 ` [PATCH 2/2] tests/qtest/migration: test " Denis V. Lunev
2026-11-26 19:33 ` Peter Xu
2026-11-25 23:26 ` [PATCH 0/2] migration: fix a source hang on " Fabiano Rosas
2026-11-26 14:02 ` Denis V. Lunev
2026-11-26 14:38 ` Fabiano Rosas
2026-11-26 19:32 ` Peter Xu
2026-11-26 19:39 ` Peter Xu
2026-11-26 21:20 ` Fabiano Rosas
2026-11-27 12:22 ` Peter Xu
2026-11-27 12:49 ` Fabiano Rosas
2026-11-27 13:28 ` Peter Xu
2026-11-27 14:21 ` Fabiano Rosas
2026-11-27 14:59 ` Peter Xu
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.