From 511e03a7f7b977ba52a19ab290254ca82320cb62 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 20 Sep 2020 13:08:02 +0200 Subject: tools: Improve locking in oping server There was a dealloc() call in oping server under mutex, which could leave that mutex locked when the thread was cancelled, causing oping to hang on exit. This avoids calling dealloc under lock. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/tools/oping/oping_server.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/tools/oping/oping_server.c b/src/tools/oping/oping_server.c index 5cc347cb..e7a7b12d 100644 --- a/src/tools/oping/oping_server.c +++ b/src/tools/oping/oping_server.c @@ -61,16 +61,21 @@ void * cleaner_thread(void * o) while (true) { clock_gettime(CLOCK_REALTIME, &now); - pthread_mutex_lock(&server.lock); + for (i = 0; i < OPING_MAX_FLOWS; ++i) - if (fset_has(server.flows, i) && - ts_diff_ms(&server.times[i], &now) > deadline_ms) { - printf("Flow %d timed out.\n", i); - fset_del(server.flows, i); - flow_dealloc(i); + if (fset_has(server.flows, i)) { + time_t diff; + + pthread_mutex_lock(&server.lock); + diff = ts_diff_ms(&server.times[i], &now); + pthread_mutex_unlock(&server.lock); + + if (diff > deadline_ms) { + printf("Flow %d timed out.\n", i); + fset_del(server.flows, i); + flow_dealloc(i); + } } - - pthread_mutex_unlock(&server.lock); sleep(1); } } @@ -139,8 +144,9 @@ void * accept_thread(void * o) clock_gettime(CLOCK_REALTIME, &now); - pthread_mutex_lock(&server.lock); fset_add(server.flows, fd); + + pthread_mutex_lock(&server.lock); server.times[fd] = now; pthread_mutex_unlock(&server.lock); -- cgit v1.2.3