* [PATCH net-next v2 1/3] vsock/test: add timeout_usleep() to allow sleeping in timeout sections
2025-05-14 14:19 [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability Stefano Garzarella
@ 2025-05-14 14:19 ` Stefano Garzarella
2025-05-14 14:19 ` [PATCH net-next v2 2/3] vsock/test: retry send() to avoid occasional failure in sigpipe test Stefano Garzarella
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Stefano Garzarella @ 2025-05-14 14:19 UTC (permalink / raw)
To: netdev; +Cc: virtualization, Stefano Garzarella, linux-kernel
From: Stefano Garzarella <sgarzare@redhat.com>
The timeout API uses signals, so we have documented not to use sleep(),
but we can use nanosleep(2) since POSIX.1 explicitly specifies that it
does not interact with signals.
Let's provide timeout_usleep() for that.
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
tools/testing/vsock/timeout.h | 1 +
tools/testing/vsock/timeout.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/tools/testing/vsock/timeout.h b/tools/testing/vsock/timeout.h
index ecb7c840e65a..1c3fcad87a49 100644
--- a/tools/testing/vsock/timeout.h
+++ b/tools/testing/vsock/timeout.h
@@ -11,5 +11,6 @@ void sigalrm(int signo);
void timeout_begin(unsigned int seconds);
void timeout_check(const char *operation);
void timeout_end(void);
+int timeout_usleep(useconds_t usec);
#endif /* TIMEOUT_H */
diff --git a/tools/testing/vsock/timeout.c b/tools/testing/vsock/timeout.c
index 44aee49b6cee..1453d38e08bb 100644
--- a/tools/testing/vsock/timeout.c
+++ b/tools/testing/vsock/timeout.c
@@ -21,6 +21,7 @@
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
+#include <time.h>
#include "timeout.h"
static volatile bool timeout;
@@ -28,6 +29,8 @@ static volatile bool timeout;
/* SIGALRM handler function. Do not use sleep(2), alarm(2), or
* setitimer(2) while using this API - they may interfere with each
* other.
+ *
+ * If you need to sleep, please use timeout_sleep() provided by this API.
*/
void sigalrm(int signo)
{
@@ -58,3 +61,18 @@ void timeout_end(void)
alarm(0);
timeout = false;
}
+
+/* Sleep in a timeout section.
+ *
+ * nanosleep(2) can be used with this API since POSIX.1 explicitly
+ * specifies that it does not interact with signals.
+ */
+int timeout_usleep(useconds_t usec)
+{
+ struct timespec ts = {
+ .tv_sec = usec / 1000000,
+ .tv_nsec = (usec % 1000000) * 1000,
+ };
+
+ return nanosleep(&ts, NULL);
+}
--
2.49.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next v2 2/3] vsock/test: retry send() to avoid occasional failure in sigpipe test
2025-05-14 14:19 [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability Stefano Garzarella
2025-05-14 14:19 ` [PATCH net-next v2 1/3] vsock/test: add timeout_usleep() to allow sleeping in timeout sections Stefano Garzarella
@ 2025-05-14 14:19 ` Stefano Garzarella
2025-05-14 14:19 ` [PATCH net-next v2 3/3] vsock/test: check also expected errno on " Stefano Garzarella
2025-05-17 1:10 ` [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: Stefano Garzarella @ 2025-05-14 14:19 UTC (permalink / raw)
To: netdev; +Cc: virtualization, Stefano Garzarella, linux-kernel
From: Stefano Garzarella <sgarzare@redhat.com>
When the other peer calls shutdown(SHUT_RD), there is a chance that
the send() call could occur before the message carrying the close
information arrives over the transport. In such cases, the send()
might still succeed. To avoid this race, let's retry the send() call
a few times, ensuring the test is more reliable.
Sleep a little before trying again to avoid flooding the other peer
and filling its receive buffer, causing false-negative.
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
v2:
- add little sleep [Paolo]
---
tools/testing/vsock/vsock_test.c | 38 +++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c
index d0f6d253ac72..68f425af00cc 100644
--- a/tools/testing/vsock/vsock_test.c
+++ b/tools/testing/vsock/vsock_test.c
@@ -1058,17 +1058,34 @@ static void sigpipe(int signo)
have_sigpipe = 1;
}
+#define SEND_SLEEP_USEC (10 * 1000)
+
static void test_stream_check_sigpipe(int fd)
{
ssize_t res;
have_sigpipe = 0;
- res = send(fd, "A", 1, 0);
- if (res != -1) {
- fprintf(stderr, "expected send(2) failure, got %zi\n", res);
- exit(EXIT_FAILURE);
+ /* When the other peer calls shutdown(SHUT_RD), there is a chance that
+ * the send() call could occur before the message carrying the close
+ * information arrives over the transport. In such cases, the send()
+ * might still succeed. To avoid this race, let's retry the send() call
+ * a few times, ensuring the test is more reliable.
+ */
+ timeout_begin(TIMEOUT);
+ while(1) {
+ res = send(fd, "A", 1, 0);
+ if (res == -1)
+ break;
+
+ /* Sleep a little before trying again to avoid flooding the
+ * other peer and filling its receive buffer, causing
+ * false-negative.
+ */
+ timeout_usleep(SEND_SLEEP_USEC);
+ timeout_check("send");
}
+ timeout_end();
if (!have_sigpipe) {
fprintf(stderr, "SIGPIPE expected\n");
@@ -1077,11 +1094,16 @@ static void test_stream_check_sigpipe(int fd)
have_sigpipe = 0;
- res = send(fd, "A", 1, MSG_NOSIGNAL);
- if (res != -1) {
- fprintf(stderr, "expected send(2) failure, got %zi\n", res);
- exit(EXIT_FAILURE);
+ timeout_begin(TIMEOUT);
+ while(1) {
+ res = send(fd, "A", 1, MSG_NOSIGNAL);
+ if (res == -1)
+ break;
+
+ timeout_usleep(SEND_SLEEP_USEC);
+ timeout_check("send");
}
+ timeout_end();
if (have_sigpipe) {
fprintf(stderr, "SIGPIPE not expected\n");
--
2.49.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net-next v2 3/3] vsock/test: check also expected errno on sigpipe test
2025-05-14 14:19 [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability Stefano Garzarella
2025-05-14 14:19 ` [PATCH net-next v2 1/3] vsock/test: add timeout_usleep() to allow sleeping in timeout sections Stefano Garzarella
2025-05-14 14:19 ` [PATCH net-next v2 2/3] vsock/test: retry send() to avoid occasional failure in sigpipe test Stefano Garzarella
@ 2025-05-14 14:19 ` Stefano Garzarella
2025-05-17 1:10 ` [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: Stefano Garzarella @ 2025-05-14 14:19 UTC (permalink / raw)
To: netdev; +Cc: virtualization, Stefano Garzarella, linux-kernel
From: Stefano Garzarella <sgarzare@redhat.com>
In the sigpipe test, we expect send() to fail, but we do not check if
send() fails with the errno we expect (EPIPE).
Add this check and repeat the send() in case of EINTR as we do in other
tests.
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
v2:
- fixed loop exit condition [Paolo]
note: the code changed a bit from v1, but this time I checked it
better!
---
tools/testing/vsock/vsock_test.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c
index 68f425af00cc..082c5dd6e8f5 100644
--- a/tools/testing/vsock/vsock_test.c
+++ b/tools/testing/vsock/vsock_test.c
@@ -1075,7 +1075,7 @@ static void test_stream_check_sigpipe(int fd)
timeout_begin(TIMEOUT);
while(1) {
res = send(fd, "A", 1, 0);
- if (res == -1)
+ if (res == -1 && errno != EINTR)
break;
/* Sleep a little before trying again to avoid flooding the
@@ -1087,6 +1087,10 @@ static void test_stream_check_sigpipe(int fd)
}
timeout_end();
+ if (errno != EPIPE) {
+ fprintf(stderr, "unexpected send(2) errno %d\n", errno);
+ exit(EXIT_FAILURE);
+ }
if (!have_sigpipe) {
fprintf(stderr, "SIGPIPE expected\n");
exit(EXIT_FAILURE);
@@ -1097,7 +1101,7 @@ static void test_stream_check_sigpipe(int fd)
timeout_begin(TIMEOUT);
while(1) {
res = send(fd, "A", 1, MSG_NOSIGNAL);
- if (res == -1)
+ if (res == -1 && errno != EINTR)
break;
timeout_usleep(SEND_SLEEP_USEC);
@@ -1105,6 +1109,10 @@ static void test_stream_check_sigpipe(int fd)
}
timeout_end();
+ if (errno != EPIPE) {
+ fprintf(stderr, "unexpected send(2) errno %d\n", errno);
+ exit(EXIT_FAILURE);
+ }
if (have_sigpipe) {
fprintf(stderr, "SIGPIPE not expected\n");
exit(EXIT_FAILURE);
--
2.49.0
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability
2025-05-14 14:19 [PATCH net-next v2 0/3] vsock/test: improve sigpipe test reliability Stefano Garzarella
` (2 preceding siblings ...)
2025-05-14 14:19 ` [PATCH net-next v2 3/3] vsock/test: check also expected errno on " Stefano Garzarella
@ 2025-05-17 1:10 ` patchwork-bot+netdevbpf
3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-05-17 1:10 UTC (permalink / raw)
To: Stefano Garzarella; +Cc: netdev, virtualization, linux-kernel
Hello:
This series was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Wed, 14 May 2025 16:19:24 +0200 you wrote:
> Running the tests continuously I noticed that sometimes the sigpipe
> test would fail due to a race between the control message of the test
> and the vsock transport messages.
>
> While I was at it I also improved the test by checking the errno we
> expect.
>
> [...]
Here is the summary with links:
- [net-next,v2,1/3] vsock/test: add timeout_usleep() to allow sleeping in timeout sections
(no matching commit)
- [net-next,v2,2/3] vsock/test: retry send() to avoid occasional failure in sigpipe test
https://git.kernel.org/netdev/net-next/c/135a8a4d25a2
- [net-next,v2,3/3] vsock/test: check also expected errno on sigpipe test
https://git.kernel.org/netdev/net-next/c/3c6abbe85bcc
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 5+ messages in thread