From: Fabiano Rosas <farosas@suse.de>
To: "Denis V. Lunev" <den@virtuozzo.com>,
"Denis V. Lunev" <den@openvz.org>,
qemu-devel@nongnu.org
Cc: "Peter Xu" <peterx@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>
Subject: Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
Date: Thu, 26 Nov 2026 11:38:14 -0300 [thread overview]
Message-ID: <8733snslnt.fsf@suse.de> (raw)
In-Reply-To: <6314eded-6904-4722-92f8-8db90cc4688b@virtuozzo.com>
"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
next prev parent reply other threads:[~2026-11-26 14:38 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8733snslnt.fsf@suse.de \
--to=farosas@suse.de \
--cc=den@openvz.org \
--cc=den@virtuozzo.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.