All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Jaehoon Kim <jhkim@linux.ibm.com>
Cc: qemu-devel@nongnu.org, jjherne@linux.ibm.com,
	steven.sistare@oracle.com, peterx@redhat.com, farosas@suse.de,
	lvivier@redhat.com, pbonzini@redhat.com
Subject: Re: [PATCH v2] migration: Setup pre-listened cpr.sock to remove race-condition.
Date: Tue, 10 Jun 2025 18:09:04 +0100	[thread overview]
Message-ID: <aEhmsFsmsoFpwWSf@redhat.com> (raw)
In-Reply-To: <20250610150849.326194-1-jhkim@linux.ibm.com>

On Tue, Jun 10, 2025 at 10:08:49AM -0500, Jaehoon Kim wrote:
> When the source VM attempts to connect to the destination VM's Unix
> domain socket (cpr.sock) during a cpr-transfer test, race conditions can
> occur if the socket file isn't ready. This can lead to connection
> failures when running tests.
> 
> This patch creates and listens on the socket in advance, and passes the
> pre-listened FD directly. This avoids timing issues and improves the
> reliability of CPR tests.
> 
> Reviewed-by: Jason J. Herne <jjherne@linux.ibm.com>
> Signed-off-by: Jaehoon Kim <jhkim@linux.ibm.com>
> 
> ---
> Changes since v1:
> - In v1, the patch added a wait loop to poll the existence of the socket
>   file (cpr_validate_socket_path()).
> 
> - This version instead creates the socket beforehand and passes its FD
>   to the destination QEMU, eliminating the race condition entirely.
> 
> - Commit title and message changed accordingly.
> ---
>  migration/cpr-transfer.c          |  3 +-
>  tests/qtest/migration/cpr-tests.c | 72 ++++++++++++++++++++++++++++++-
>  2 files changed, 72 insertions(+), 3 deletions(-)

> diff --git a/tests/qtest/migration/cpr-tests.c b/tests/qtest/migration/cpr-tests.c
> index 5536e14610..6f90160e21 100644
> --- a/tests/qtest/migration/cpr-tests.c
> +++ b/tests/qtest/migration/cpr-tests.c
> @@ -50,6 +50,51 @@ static void *test_mode_transfer_start(QTestState *from, QTestState *to)
>      return NULL;
>  }
>  
> +/*
> + * Create a pre-listened UNIX domain socket at the specified path.
> + *
> + * This is used to eliminate a race condition that can occur
> + * intermittently in qtest during CPR tests. By pre-creating and
> + * listening on the socket, we avoid timing-related issues.
> + */
> +static int setup_socket_listener(const char *path)
> +{
> +    struct sockaddr_un un;
> +    size_t pathlen;
> +    int sock_fd;
> +
> +    sock_fd = socket(PF_UNIX, SOCK_STREAM, 0);
> +    if (sock_fd < 0) {
> +        g_test_message("Failed to create Unix socket");
> +        return -1;
> +    }
> +
> +    pathlen = strlen(path);
> +    if (pathlen >= sizeof(un.sun_path)) {
> +        g_test_message("UNIX socket path '%s' is too long", path);
> +        close(sock_fd);
> +        return -1;
> +    }
> +
> +    memset(&un, 0, sizeof(un));
> +    un.sun_family = AF_UNIX;
> +    strncpy(un.sun_path, path, sizeof(un.sun_path) - 1);
> +
> +    if (bind(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
> +        g_test_message("Failed to bind socket to %s", path);
> +        close(sock_fd);
> +        return -1;
> +    }
> +
> +    if (listen(sock_fd, 1) < 0) {
> +        g_test_message("Failed to listen on socket %s", path);
> +        close(sock_fd);
> +        return -1;
> +    }
> +
> +    return sock_fd;
> +}

This is effectively re-implementing 'unix_listen', so just use
that function.

> @@ -75,6 +120,29 @@ static void test_mode_transfer_common(bool incoming_defer)
>          "              'path': '%s' } } ]",
>          mig_path);
>  
> +    /*
> +     * Determine socket address type and value.
> +     * If socket creation fails, provide the socket path to the target,
> +     * so it can create the Unix domain socket itself.
> +     * Otherwise, use the pre-listened socket file descriptor directly.
> +     */
> +    int cpr_sockfd = setup_socket_listener(cpr_path);
> +
> +    if (cpr_sockfd < 0) {

A failure of this function (or in future 'unix_listen') shouldn't
trigger any fallback logic - we should report it and fail thue
test.

> +        addr_type = g_strdup("unix");
> +        addr_key = g_strdup("path");
> +        addr_value = g_strdup(cpr_path);
> +    } else {
> +        addr_type = g_strdup("fd");
> +        addr_key = g_strdup("str");
> +        addr_value = g_strdup_printf("%d", cpr_sockfd);
> +    }
> +
> +    opts_target = g_strdup_printf("-incoming cpr,addr.transport=socket,"
> +                                  "addr.type=%s,addr.%s=%s %s",
> +                                  addr_type, addr_key, addr_value, opts);
> +
> +
>      MigrateCommon args = {
>          .start.opts_source = opts,
>          .start.opts_target = opts_target,
> -- 
> 2.49.0
> 
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  parent reply	other threads:[~2025-06-10 17:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-10 15:08 [PATCH v2] migration: Setup pre-listened cpr.sock to remove race-condition Jaehoon Kim
2025-06-10 16:02 ` Peter Xu
2025-06-10 17:25   ` JAEHOON KIM
2025-06-10 16:57 ` Steven Sistare
2025-06-10 17:13   ` JAEHOON KIM
2025-06-10 17:05 ` Daniel P. Berrangé
2025-06-10 17:19   ` JAEHOON KIM
2025-06-10 17:09 ` Daniel P. Berrangé [this message]
2025-06-10 17:23   ` JAEHOON KIM

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=aEhmsFsmsoFpwWSf@redhat.com \
    --to=berrange@redhat.com \
    --cc=farosas@suse.de \
    --cc=jhkim@linux.ibm.com \
    --cc=jjherne@linux.ibm.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=steven.sistare@oracle.com \
    /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.