diff options
author | Dimitri Staessens <[email protected]> | 2020-09-20 13:08:02 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2020-09-25 13:20:02 +0200 |
commit | 511e03a7f7b977ba52a19ab290254ca82320cb62 (patch) | |
tree | 0e24b58903dd6e9eeb5015583a2759a7806f206d /src/tools | |
parent | 1e3a9e464cbb2f02c057e9f63c1f270ff27530f4 (diff) | |
download | ouroboros-511e03a7f7b977ba52a19ab290254ca82320cb62.tar.gz ouroboros-511e03a7f7b977ba52a19ab290254ca82320cb62.zip |
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 <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/oping/oping_server.c | 24 |
1 files 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); |