migration: Setup pre-listened cpr.sock to remove race-condition.
PATCH v2 1/1 migration: Setup pre-listened cpr.sock to remove race-condition.
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.
---
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(-)
migration/cpr-transfer.c +2 -1
@@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)MigrationAddress *addr = channel->addr;if (addr->transport ==MIGRATION_ADDRESS_TYPE_SOCKET && - addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX ) { + (addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX || + addr->u.socket.type ==SOCKET_ADDRESS_TYPE_FD )) {g_autoptr (QIOChannelSocket) sioc =NULL ;SocketAddress *saddr = &addr->u.socket;
tests/qtest/migration/cpr-tests.c +70 -2
95 lines not on this page · raw message
Signature · 1 line
2.49.0
Quoted 11 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>
One quick comment while we can wait for others to look at the details: when it involves both qemu and tests changes, please consider splitting that into two patches. The test patch can be prefixed with "tests/migration:".
Thanks,
Signature · 1 line
Peter Xu
Quoted 10 lines · Peter Xu
Quoted 9 lines · Jaehoon Kim
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.
Quoted 7 lines
Quoted 2 lines
Reviewed-by Jason J. Herne<[email protected]>Signed-off-by Jaehoon Kim<[email protected]>One quick comment while we can wait for others to look at the details: when it involves both qemu and tests changes, please consider splitting that into two patches. The test patch can be prefixed with "tests/migration:".
Thanks,
Thank you for your suggestion. I'll split the patch into two separate patches and submit an updated v3 version
- Jaehoon Kim
Quoted 31 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>---
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(-)migration/cpr-transfer.c +0
-0
migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 50
Quoted 6 lines
migration/cpr-transfer.c +2
-1@@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)MigrationAddress *addr = channel->addr;if (addr->transport ==MIGRATION_ADDRESS_TYPE_SOCKET && - addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX ) { + (addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX || + addr->u.socket.type ==SOCKET_ADDRESS_TYPE_FD )) {
Nice, I did not realize this would be so simple!
Quoted 45 lines
migration/cpr-transfer.c +0
-0g_autoptr (QIOChannelSocket) sioc =NULL ;SocketAddress *saddr = &addr->u.socket;tests/qtest/migration/cpr-tests.c +35
-0@@ -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 ; + }
tests/qtest/migration/cpr-tests.c static void *test_mode_transfer_start(QTestState *from, QTestState *to) · line 97
Quoted 10 lines
tests/qtest/migration/cpr-tests.c +10
-0+ +if (listen (sock_fd,1 ) <0 ) { +g_test_message ("Failed to listen on socket %s" , path); +close (sock_fd); +return -1 ; + } + +return sock_fd; +} +
Very nice code, but you can just use libqtest.c:qtest_socket_server(). That does not check for errors as nicely as you do, but none of its actions should ever fail, unless the system is very starved for resources.
Quoted 20 lines
tests/qtest/migration/cpr-tests.c +2
-2/* * cpr-transfer mode cannot use the target monitor prior to starting the * migration, and cannot connect synchronously to the monitor, so defer @@ -60,13 +105,13 @@ static void test_mode_transfer_common(bool incoming_defer) g_autofree char *cpr_path = g_strdup_printf("%s/cpr.sock", tmpfs); g_autofree char *mig_path = g_strdup_printf("%s/migsocket", tmpfs); g_autofree char *uri = g_strdup_printf("unix:%s", mig_path); + g_autofree char *addr_type, *addr_key, *addr_value; + g_autofree char *opts_target;const char *opts ="-machine aux-ram-share=on -nodefaults" ; g_autofree const char *cpr_channel = g_strdup_printf("cpr,addr.transport=socket,addr.type=unix,addr.path=%s" , cpr_path); - g_autofree char *opts_target = g_strdup_printf("-incoming %s %s", - cpr_channel, opts); g_autofree char *connect_channels = g_strdup_printf( "[ { 'channel-type': 'main',"
tests/qtest/migration/cpr-tests.c static void test_mode_transfer_common(bool incoming_defer) · line 129
Quoted 10 lines
tests/qtest/migration/cpr-tests.c +7
-0@@ -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);
unlink(cpr_path) after the listen socket is created.
g_assert(cpr_sockfd >= 0), then you can simplify the next block of code. qtest_socket_server() will only fail if something is very wrong, in which case trying to proceed with more tests is probably doomed.
- Steve
Quoted 19 lines
tests/qtest/migration/cpr-tests.c +16
-0+ +if (cpr_sockfd <0 ) { + 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,
Quoted 92 lines · Steven Sistare
Quoted 33 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>---
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(-)migration/cpr-transfer.c +0
-0@@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) MigrationAddress *addr = channel->addr;Quoted 4 lines
if (
addr->transport==MIGRATION_ADDRESS_TYPE_SOCKET&& -addr->u.socket.type==SOCKET_ADDRESS_TYPE_UNIX) { + (addr->u.socket.type==SOCKET_ADDRESS_TYPE_UNIX|| +addr->u.socket.type==SOCKET_ADDRESS_TYPE_FD)) {Nice, I did not realize this would be so simple!
Quoted 51 lines
g_autoptr(QIOChannelSocket)sioc = NULL; SocketAddress *saddr =&addr->u.socket;tests/qtest/migration/cpr-tests.c +0
-0@@ -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.
+ */tests/qtest/migration/cpr-tests.c +34
-0+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; + }
Quoted 9 lines
tests/qtest/migration/cpr-tests.c static void *test_mode_transfer_start(QTestState *from, QTestState *to) · line 97
Quoted 4 lines
tests/qtest/migration/cpr-tests.c +4
-0+ + return sock_fd; +} +Very nice code, but you can just use libqtest.c:qtest_socket_server(). That does not check for errors as nicely as you do, but none of its actions should ever fail, unless the system is very starved for resources.
Yes, as you suggested, using qtest_socket_server() makes the code simpler. Thank you very much!
Quoted 32 lines
Quoted 22 lines
/*
* cpr-transfer mode cannot use the target monitor prior to starting the
* migration, and cannot connect synchronously to the monitor, so defertests/qtest/migration/cpr-tests.c +2
-0@@ -60,13 +105,13 @@ static void test_mode_transfer_common(bool incoming_defer) g_autofree char *cpr_path = g_strdup_printf("%s/cpr.sock", tmpfs); g_autofree char *mig_path = g_strdup_printf("%s/migsocket", tmpfs); g_autofree char *uri = g_strdup_printf("unix:%s", mig_path); + g_autofree char *addr_type, *addr_key, *addr_value; + g_autofree char *opts_target; const char *opts = "-machine aux-ram-share=on -nodefaults";
g_autofreeconst char*cpr_channel=g_strdup_printf( "cpr,addr.transport=socket,addr.type=unix,addr.path=%s", cpr_path);
-g_autofreechar*opts_target= g_strdup_printf("-incoming %s %s", -cpr_channel, opts);g_autofreechar*connect_channels= g_strdup_printf( "[ { 'channel-type': 'main',"tests/qtest/migration/cpr-tests.c +0
-0@@ -75,6 +120,29 @@ static void test_mode_transfer_common(bool incoming_defer) " 'path': '%s' } } ]", mig_path);Quoted 7 lines
+ /*
+ * 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. + */
+ intcpr_sockfd=setup_socket_listener(cpr_path);
unlink(cpr_path)after the listen socket is created.
Quoted 6 lines
g_assert(cpr_sockfd >= 0), then you can simplify the next block of code.qtest_socket_server()will only fail if something is very wrong, in which case trying to proceed with more tests is probably doomed.- Steve
Actually, I had considered exiting the process if socket creation failed, but ended up implementing it this way instead. I'll revise the patch to follow your suggestion and make it terminate on failure.
- Jaehoon Kim
Quoted 23 lines
Quoted 21 lines
tests/qtest/migration/cpr-tests.c +12
-0+ + if (cpr_sockfd < 0) { + 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("-incomingcpr,addr.transport=socket,"
+ "addr.type=%s,addr.%s=%s %s", +addr_type,addr_key,addr_value, opts);tests/qtest/migration/cpr-tests.c +2
-0+ +MigrateCommon args = {
.start.opts_source = opts,
.start.opts_target =opts_target,
Quoted 31 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>---
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(-)migration/cpr-transfer.c +0
-0
migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 53
Quoted 9 lines
migration/cpr-transfer.c +2
-1@@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)MigrationAddress *addr = channel->addr;if (addr->transport ==MIGRATION_ADDRESS_TYPE_SOCKET && - addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX ) { + (addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX || + addr->u.socket.type ==SOCKET_ADDRESS_TYPE_FD )) {g_autoptr (QIOChannelSocket) sioc =NULL ;SocketAddress *saddr = &addr->u.socket;
This will likely cause a crash in tracing code that is below this diff:
trace_cpr_transfer_input(addr->u.socket.u.q_unix.path);
q_unix.path is NOT valid to access with TYPE_FD.
With regards,
Daniel
Signature · 3 lines
|: 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 :|
Quoted 43 lines · Daniel P. Berrangé
Quoted 31 lines · Jaehoon Kim
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<[email protected]>Signed-off-by Jaehoon Kim<[email protected]>---
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(-)migration/cpr-transfer.c +0
-0migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 53
Quoted 9 lines
migration/cpr-transfer.c +2
-1@@ -46,7 +46,8 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)MigrationAddress *addr = channel->addr;if (addr->transport ==MIGRATION_ADDRESS_TYPE_SOCKET && - addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX ) { + (addr->u.socket.type ==SOCKET_ADDRESS_TYPE_UNIX || + addr->u.socket.type ==SOCKET_ADDRESS_TYPE_FD )) {g_autoptr (QIOChannelSocket) sioc =NULL ;SocketAddress *saddr = &addr->u.socket;This will likely cause a crash in tracing code that is below this diff:
Quoted 7 lines
trace_cpr_transfer_input(addr->u.socket.u.q_unix.path);q_unix.path is NOT valid to access with
TYPE_FD.With regards,
Daniel
You're right, I should split trace_cpr_transfer_input() call based on addr->u.socket.type, since q_unix.path is not valid when the type is TYPE_FD.
- Jaehoon Kim.
Quoted 68 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>---
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(-)tests/qtest/migration/cpr-tests.c +34
-0@@ -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 ;
tests/qtest/migration/cpr-tests.c static void *test_mode_transfer_start(QTestState *from, QTestState *to) · line 96
Quoted 10 lines
tests/qtest/migration/cpr-tests.c +10
-0+ } + +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.
tests/qtest/migration/cpr-tests.c static void test_mode_transfer_common(bool incoming_defer) · line 131
Quoted 13 lines
tests/qtest/migration/cpr-tests.c +9
-0@@ -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.
Quoted 21 lines
tests/qtest/migration/cpr-tests.c +14
-0+ 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,Signature · 1 line
2.49.0
With regards,
Daniel
Signature · 3 lines
|: 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 :|
Quoted 72 lines · Daniel P. Berrangé
Quoted 71 lines · Jaehoon Kim
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 <[email protected]>Signed-off-by Jaehoon Kim <[email protected]>---
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(-)tests/qtest/migration/cpr-tests.c +38
-0@@ -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);
Quoted 8 lines
tests/qtest/migration/cpr-tests.c static void *test_mode_transfer_start(QTestState *from, QTestState *to) · line 96
Quoted 6 lines
tests/qtest/migration/cpr-tests.c +6
-0+close (sock_fd); +return -1 ; + } + +return sock_fd; +}This is effectively re-implementing '
unix_listen', so just use that function.
I'll revise the patch to use the already defined function instead of re-implementing it. Thanks for pointing it out.
Quoted 4 lines
Quoted 4 lines
tests/qtest/migration/cpr-tests.c +0
-0@@ -75,6 +120,29 @@ static void test_mode_transfer_common(bool incoming_defer)" 'path': '%s' } } ]" , mig_path);
Quoted 12 lines
tests/qtest/migration/cpr-tests.c static void test_mode_transfer_common(bool incoming_defer) · line 131
Quoted 9 lines
tests/qtest/migration/cpr-tests.c +9
-0+/* +* 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.
I was considering both options, but I agree with you.
It's better to fail the test and report the error rather than trigger fallback logic. I'll update the patch accordingly.
- Jaehoon Kim.
Quoted 23 lines
Quoted 21 lines
tests/qtest/migration/cpr-tests.c +14
-0+ 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,Signature · 1 line
2.49.0With regards,
Daniel