tests: Fix possible deadlock in qtest initialization
PATCH v2 1/2 tests/libqtest: Fix possible deadlock in qtest initialization
In reply to Marcel Apfelbaum'socket_accept' waits for Qemu to init its unix socket. If Qemu encounters an error during command line parsing, it can exit before initializing the communication channel.
Using a timeout for sockets fixes the issue.
--- tests/libqtest.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
tests/libqtest.c +9 -2
@@ -34,6 +34,7 @@#include "qapi/qmp/json-parser.h" #define MAX_IRQ 256 +#define SOCKET_TIMEOUT 5QTestState *global_qtest;
@@ -78,12 +79,16 @@ static int socket_accept(int sock)struct sockaddr_un addr;socklen_t addrlen;int ret; +struct timeval timeout = { .tv_sec =SOCKET_TIMEOUT , + .tv_usec =0 }; + +setsockopt (sock,SOL_SOCKET ,SO_RCVTIMEO , (void *)&timeout, +sizeof (timeout)); addrlen =sizeof (addr);do { ret =accept (sock, (struct sockaddr *)&addr, &addrlen); }while (ret ==-1 && errno ==EINTR ); -g_assert_no_errno (ret);close (sock);return ret;
@@ -91,7 +96,7 @@ static int socket_accept(int sock)static void kill_qemu (QTestState *s) { -if (s->qemu_pid !=-1 ) { +if (s && s->qemu_pid !=-1 ) {kill (s->qemu_pid,SIGTERM );waitpid (s->qemu_pid,NULL ,0 ); }
@@ -153,6 +158,8 @@ QTestState *qtest_init(const char *extra_args)g_free (socket_path);g_free (qmp_socket_path); +g_assert (s->fd >=0 && s->qmp_fd >=0 ); + s->rx =g_string_new ("" );for (i =0 ; i <MAX_IRQ ; i++) { s->irq_level[i] = false;
Signature · 1 line
1.8.3.1
PATCH v2 2/2 tests: Tweak the Makefile to produce per-test output.
In reply to Marcel ApfelbaumFrom: Stefan Hajnoczi <[email protected]>
The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on other code and not in the test itself.
--- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tests/Makefile +1 -1
@@ -273,7 +273,7 @@ check-help: @echo "changed with variable GTESTER_OPTIONS." SPEED = quick -GTESTER_OPTIONS = -k $(if $(V),--verbose,-q) +GTESTER_OPTIONS = -k #$(if $(V),--verbose,-q) GCOV_OPTIONS = -n $(if $(V),-f,) # gtester tests, possibly with verbose output
Signature · 1 line
1.8.3.1
v2 cover tests: Fix possible deadlock in qtest initialization
V1 -> V2:
Addressed Stefan Hajnoczi's review:
- Dropped qtest_state_valid API
- Moved socket options in socket_accept.
'socket_accept' waits for Qemu to init its unix socket. If Qemu encounters an error during command line parsing, it can exit before initializing the communication channel.
Using a timeout for sockets fixes the issue, but is not enough. We need to know which test from check-qtest-* fails.
We tweak the Makefile to produce per-test output. The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on other code and not in the test itself.
Marcel Apfelbaum (1):
tests/libqtest: Fix possible deadlock in qtest initialization
Stefan Hajnoczi (1):
tests: Tweak the Makefile to produce per-test output.
tests/Makefile | 2 +- tests/libqtest.c | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH V2 2/2] tests: Tweak the Makefile to produce per-test output.
In reply to Marcel ApfelbaumQuoted 4 lines · Marcel Apfelbaum
From: Stefan Hajnoczi <[email protected]>
The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on
s/asert/assert/
Quoted 13 lines
other code and not in the test itself.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Marcel Apfelbaum <[email protected]>--- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/Makefile +0
-0
tests/Makefile check-help: · line 276
Quoted 5 lines
tests/Makefile +1
-1@@ -273,7 +273,7 @@ check-help: @echo "changed with variable GTESTER_OPTIONS." SPEED = quick -GTESTER_OPTIONS = -k $(if $(V),--verbose,-q) +GTESTER_OPTIONS = -k #$(if $(V),--verbose,-q)
Isn't this effectively commenting out the rest of the line? If so, why not delete everything after the #?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH V2 2/2] tests: Tweak the Makefile to produce per-test output.
Quoted 7 lines · Eric Blake
Quoted 4 lines · Marcel Apfelbaum
From: Stefan Hajnoczi <[email protected]>
The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on
s/asert/assert/
Sure,
Quoted 16 lines
Quoted 16 lines
other code and not in the test itself.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Marcel Apfelbaum <[email protected]>--- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/Makefile +0
-0@@ -273,7 +273,7 @@ check-help: @echo "changed with variable GTESTER_OPTIONS."
Quoted 6 lines
tests/Makefile check-help: · line 276
Quoted 3 lines
tests/Makefile +1
-1SPEED = quick -GTESTER_OPTIONS = -k $(if $(V),--verbose,-q) +GTESTER_OPTIONS = -k #$(if $(V),--verbose,-q)Isn't this effectively commenting out the rest of the line? If so, why not delete everything after the #?
Hi Eric, thank you for catching this.
Indeed, what I actually want to do is to remove the -q flag. I sent V3 that fixes this,
Thanks,
Marcel
Quoted 1 line
Re: [Qemu-devel] [PATCH V2 1/2] tests/libqtest: Fix possible deadlock in qtest initialization
In reply to Marcel ApfelbaumQuoted 6 lines · Marcel Apfelbaum
tests/libqtest.c +2
-0@@ -78,12 +79,16 @@ static int socket_accept(int sock)struct sockaddr_un addr;socklen_t addrlen;int ret; +struct timeval timeout = { .tv_sec =SOCKET_TIMEOUT , + .tv_usec =0 };
tests/libqtest.c static int socket_accept(int sock) · line 92
Quoted 10 lines
tests/libqtest.c +3
-1+ +setsockopt (sock,SOL_SOCKET ,SO_RCVTIMEO , (void *)&timeout, +sizeof (timeout)); addrlen =sizeof (addr);do { ret =accept (sock, (struct sockaddr *)&addr, &addrlen); }while (ret ==-1 && errno ==EINTR ); -g_assert_no_errno (ret);close (sock);
Did you mean to leave SO_RCVTIMEO set after this function completes?
tests/libqtest.c static int socket_accept(int sock) · line 102
Quoted 9 lines
tests/libqtest.c +1
-1@@ -91,7 +96,7 @@ static int socket_accept(int sock)static void kill_qemu (QTestState *s) { -if (s->qemu_pid !=-1 ) { +if (s && s->qemu_pid !=-1 ) {kill (s->qemu_pid,SIGTERM );waitpid (s->qemu_pid,NULL ,0 ); }
This is a bug in libqtest.c, please don't silence the crash.
kill_qemu() gets called from the SIGABRT signal handler but I forgot that global_qtest isn't initialized yet while qtest_init() executes.
In other words, the cleanup is broken if we fail inside qtest_init(). Can you drop this hunk and I'll send a patch to fix the underlying issue?
tests/libqtest.c QTestState *qtest_init(const char *extra_args) · line 162
Quoted 6 lines
tests/libqtest.c +2
-0@@ -153,6 +158,8 @@ QTestState *qtest_init(const char *extra_args)g_free (socket_path);g_free (qmp_socket_path); +g_assert (s->fd >=0 && s->qmp_fd >=0 ); +
We probably shouldn't socket_accept() s->qmp_fd if s->fd already failed. Otherwise we'll wait another 5 seconds for the timeout to explire:
s->fd = socket_accept(sock);
if (s->fd >= 0) {
s->qmp_fd = socket_accept(qmpsock);
}Re: [Qemu-devel] [PATCH V2 2/2] tests: Tweak the Makefile to produce per-test output.
In reply to Marcel ApfelbaumQuoted 17 lines · Marcel Apfelbaum
From: Stefan Hajnoczi <[email protected]>
The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on other code and not in the test itself.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Marcel Apfelbaum <[email protected]>--- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/Makefile +0
-0
tests/Makefile check-help: · line 279
Quoted 8 lines
tests/Makefile +1
-1@@ -273,7 +273,7 @@ check-help: @echo "changed with variable GTESTER_OPTIONS." SPEED = quick -GTESTER_OPTIONS = -k $(if $(V),--verbose,-q) +GTESTER_OPTIONS = -k #$(if $(V),--verbose,-q) GCOV_OPTIONS = -n $(if $(V),-f,) # gtester tests, possibly with verbose output
I didn't propose this as a QEMU patch so I didn't add my Signed-off-by:. I just posted a diff to show you that gtester without -q prints the test binary names.
Please drop this patch and I'll send a final patch after I've checked the output is still clean and doesn't duplicate information.
Re: [Qemu-devel] [PATCH V2 1/2] tests/libqtest: Fix possible deadlock in qtest initialization
In reply to Stefan HajnocziQuoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Marcel Apfelbaum
tests/libqtest.c +5
-0@@ -78,12 +79,16 @@ static int socket_accept(int sock)struct sockaddr_un addr;socklen_t addrlen;int ret; +struct timeval timeout = { .tv_sec =SOCKET_TIMEOUT , + .tv_usec =0 }; + +setsockopt (sock,SOL_SOCKET ,SO_RCVTIMEO , (void *)&timeout, +sizeof (timeout));
Quoted 8 lines
tests/libqtest.c static int socket_accept(int sock) · line 92
Quoted 6 lines
tests/libqtest.c +0
-1addrlen =sizeof (addr);do { ret =accept (sock, (struct sockaddr *)&addr, &addrlen); }while (ret ==-1 && errno ==EINTR ); -g_assert_no_errno (ret);close (sock);Did you mean to leave
SO_RCVTIMEOset after this function completes?
Yes, I don't think it hurts. A 5 sec timeout should be like infinite, Qemu running on the same machine. If you think
Quoted 12 lines
tests/libqtest.c static int socket_accept(int sock) · line 102
Quoted 10 lines
tests/libqtest.c +1
-1@@ -91,7 +96,7 @@ static int socket_accept(int sock)static void kill_qemu (QTestState *s) { -if (s->qemu_pid !=-1 ) { +if (s && s->qemu_pid !=-1 ) {kill (s->qemu_pid,SIGTERM );waitpid (s->qemu_pid,NULL ,0 ); }This is a bug in
libqtest.c, please don't silence the crash.
I didn't see it like hiding a crash, I thought that if there is any problem during init it is because the Qemu failed to start, meaning that you don't have a process to kill (Qemu exited already). Al of the above happens -> you don't have a global state.
Anyway, if you have a better way to deal with it, I have nothing against it :)
Thanks,
Marcel
Quoted 12 lines
kill_qemu()gets called from the SIGABRT signal handler but I forgot thatglobal_qtestisn't initialized yet whileqtest_init()executes.In other words, the cleanup is broken if we fail inside
qtest_init(). Can you drop this hunk and I'll send a patch to fix the underlying issue?Quoted 4 lines
tests/libqtest.c +0
-0@@ -153,6 +158,8 @@ QTestState *qtest_init(const char *extra_args)g_free (socket_path);g_free (qmp_socket_path);
Quoted 5 lines
tests/libqtest.c QTestState *qtest_init(const char *extra_args) · line 162
Quoted 2 lines
tests/libqtest.c +2
-0+g_assert (s->fd >=0 && s->qmp_fd >=0 ); +We probably shouldn't
socket_accept()s->qmp_fdifs->fdalready failed. Otherwise we'll wait another 5 seconds for the timeout to explire:
Yes, I already had this chunk, I have no idea why I dropped it, I'll return it, thanks.
Thanks,
Marcel
Quoted 5 lines
s->fd = socket_accept(sock);
if (s->fd >= 0) {
s->qmp_fd = socket_accept(qmpsock);
}Re: [Qemu-devel] [PATCH V2 2/2] tests: Tweak the Makefile to produce per-test output.
In reply to Stefan HajnocziQuoted 25 lines · Stefan Hajnoczi
Quoted 24 lines · Marcel Apfelbaum
From: Stefan Hajnoczi <[email protected]>
The effect is that the output will grow, but we will know which qtest failed. This helps when an asert is made on other code and not in the test itself.
Signed-off-by Stefan Hajnoczi <[email protected]>Signed-off-by Marcel Apfelbaum <[email protected]>--- tests/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tests/Makefile +1
-1@@ -273,7 +273,7 @@ check-help: @echo "changed with variable GTESTER_OPTIONS." SPEED = quick -GTESTER_OPTIONS = -k $(if $(V),--verbose,-q) +GTESTER_OPTIONS = -k #$(if $(V),--verbose,-q) GCOV_OPTIONS = -n $(if $(V),-f,)
Quoted 8 lines
tests/Makefile check-help: · line 279
Quoted 1 line
tests/Makefile +0
-0# gtester tests, possibly with verbose outputI didn't propose this as a QEMU patch so I didn't add my Signed-off-by:. I just posted a diff to show you that gtester without -q prints the test binary names.
Please drop this patch and I'll send a final patch after I've checked the output is still clean and doesn't duplicate information.
Sure,
Marcel
Re: [Qemu-devel] [PATCH V2 1/2] tests/libqtest: Fix possible deadlock in qtest initialization
In reply to Marcel ApfelbaumQuoted 20 lines · Marcel Apfelbaum
Quoted 11 lines · Stefan Hajnoczi
Quoted 10 lines · Marcel Apfelbaum
tests/libqtest.c +5
-0@@ -78,12 +79,16 @@ static int socket_accept(int sock)struct sockaddr_un addr;socklen_t addrlen;int ret; +struct timeval timeout = { .tv_sec =SOCKET_TIMEOUT , + .tv_usec =0 }; + +setsockopt (sock,SOL_SOCKET ,SO_RCVTIMEO , (void *)&timeout, +sizeof (timeout));Quoted 8 lines
tests/libqtest.c static int socket_accept(int sock) · line 92
Quoted 6 lines
tests/libqtest.c +0
-1addrlen =sizeof (addr);do { ret =accept (sock, (struct sockaddr *)&addr, &addrlen); }while (ret ==-1 && errno ==EINTR ); -g_assert_no_errno (ret);close (sock);Did you mean to leave
SO_RCVTIMEOset after this function completes?
Quoted 2 lines
Yes, I don't think it hurts. A 5 sec timeout should be like infinite, Qemu running on the same machine. If you think
... otherwise, I can remove the timeout, but I think it is OK.
Quoted 18 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -91,7 +96,7 @@ static int socket_accept(int sock)Quoted 9 lines
tests/libqtest.c static int socket_accept(int sock) · line 102
Quoted 7 lines
tests/libqtest.c +1
-1static void kill_qemu (QTestState *s) { -if (s->qemu_pid !=-1 ) { +if (s && s->qemu_pid !=-1 ) {kill (s->qemu_pid,SIGTERM );waitpid (s->qemu_pid,NULL ,0 ); }This is a bug in
libqtest.c, please don't silence the crash.I didn't see it like hiding a crash, I thought that if there is any problem during init it is because the Qemu failed to start, meaning that you don't have a process to kill (Qemu exited already). Al of the above happens -> you don't have a global state.
Quoted 12 lines
Anyway, if you have a better way to deal with it, I have nothing against it :)
Thanks,
MarcelQuoted 7 lines
kill_qemu()gets called from the SIGABRT signal handler but I forgot thatglobal_qtestisn't initialized yet whileqtest_init()executes.In other words, the cleanup is broken if we fail inside
qtest_init(). Can you drop this hunk and I'll send a patch to fix the underlying issue?
I dropped it, please take care of it as it gets a segmentation fault if we abort in qtest_init.
Thanks,
Marcel
Quoted 24 lines
Quoted 10 lines
tests/libqtest.c QTestState *qtest_init(const char *extra_args) · line 162
Quoted 7 lines
tests/libqtest.c +2
-0@@ -153,6 +158,8 @@ QTestState *qtest_init(const char *extra_args)g_free (socket_path);g_free (qmp_socket_path); +g_assert (s->fd >=0 && s->qmp_fd >=0 ); +We probably shouldn't
socket_accept()s->qmp_fdifs->fdalready failed. Otherwise we'll wait another 5 seconds for the timeout to explire:Yes, I already had this chunk, I have no idea why I dropped it, I'll return it, thanks.
Thanks,
MarcelQuoted 5 lines
s->fd = socket_accept(sock); if (s->fd >= 0) { s->qmp_fd = socket_accept(qmpsock); }
Re: [Qemu-devel] [PATCH V2 1/2] tests/libqtest: Fix possible deadlock in qtest initialization
Quoted 21 lines · Marcel Apfelbaum
Quoted 20 lines · Marcel Apfelbaum
Quoted 19 lines · Stefan Hajnoczi
Quoted 6 lines · Marcel Apfelbaum
tests/libqtest.c +2
-0@@ -78,12 +79,16 @@ static int socket_accept(int sock)struct sockaddr_un addr;socklen_t addrlen;int ret; +struct timeval timeout = { .tv_sec =SOCKET_TIMEOUT , + .tv_usec =0 };tests/libqtest.c static int socket_accept(int sock) · line 92
Quoted 10 lines
tests/libqtest.c +3
-1+ +setsockopt (sock,SOL_SOCKET ,SO_RCVTIMEO , (void *)&timeout, +sizeof (timeout)); addrlen =sizeof (addr);do { ret =accept (sock, (struct sockaddr *)&addr, &addrlen); }while (ret ==-1 && errno ==EINTR ); -g_assert_no_errno (ret);close (sock);Did you mean to leave
SO_RCVTIMEOset after this function completes?
Quoted 3 lines
Quoted 2 lines
Yes, I don't think it hurts. A 5 sec timeout should be like infinite, Qemu running on the same machine. If you think
... otherwise, I can remove the timeout, but I think it is OK.
I think you are right. I checked that the qtest protocol has no long-running operations. It doesn't seem realistic that any qtest command would take 5 seconds or longer.
So let's leave in the timeout.
Stefan