Skip to content

Commit 8a03222

Browse files
fomichevborkmann
authored andcommitted
selftests/bpf: test_progs: fix client/server race in tcp_rtt
This is the same problem I found earlier in test_sockopt_inherit: there is a race between server thread doing accept() and client thread doing connect(). Let's explicitly synchronize them via pthread conditional variable. v2: * don't exit from server_thread without signaling condvar, fixes possible issue where main() would wait forever (Andrii Nakryiko) Fixes: b558739 ("selftests/bpf: test BPF_SOCK_OPS_RTT_CB") Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 733ef7f commit 8a03222

File tree

1 file changed

+19
-2
lines changed
  • tools/testing/selftests/bpf/prog_tests

1 file changed

+19
-2
lines changed

tools/testing/selftests/bpf/prog_tests/tcp_rtt.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,24 @@ static int start_server(void)
203203
return fd;
204204
}
205205

206+
static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
207+
static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
208+
206209
static void *server_thread(void *arg)
207210
{
208211
struct sockaddr_storage addr;
209212
socklen_t len = sizeof(addr);
210213
int fd = *(int *)arg;
211214
int client_fd;
215+
int err;
216+
217+
err = listen(fd, 1);
218+
219+
pthread_mutex_lock(&server_started_mtx);
220+
pthread_cond_signal(&server_started);
221+
pthread_mutex_unlock(&server_started_mtx);
212222

213-
if (CHECK_FAIL(listen(fd, 1)) < 0) {
223+
if (CHECK_FAIL(err < 0)) {
214224
perror("Failed to listed on socket");
215225
return NULL;
216226
}
@@ -248,7 +258,14 @@ void test_tcp_rtt(void)
248258
if (CHECK_FAIL(server_fd < 0))
249259
goto close_cgroup_fd;
250260

251-
pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
261+
if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
262+
(void *)&server_fd)))
263+
goto close_cgroup_fd;
264+
265+
pthread_mutex_lock(&server_started_mtx);
266+
pthread_cond_wait(&server_started, &server_started_mtx);
267+
pthread_mutex_unlock(&server_started_mtx);
268+
252269
CHECK_FAIL(run_test(cgroup_fd, server_fd));
253270
close(server_fd);
254271
close_cgroup_fd:

0 commit comments

Comments
 (0)