From 034724ec7239072c58014f55f0b06b920000d27c Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 21 Feb 2019 20:33:01 +0100 Subject: [PATCH] flow-manager: improve thread shutdown loops --- src/flow-manager.c | 53 ++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/src/flow-manager.c b/src/flow-manager.c index 6f378dd3a1..97166fdb9a 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -156,15 +156,18 @@ void FlowDisableFlowManagerThread(void) } SCMutexUnlock(&tv_root_lock); - double total_wait_time = 0; - /* value in seconds */ - #define THREAD_KILL_MAX_WAIT_TIME 60 - /* value in microseconds */ - #define WAIT_TIME 100 + struct timeval start_ts; + struct timeval cur_ts; + gettimeofday(&start_ts, NULL); again: - SCMutexLock(&tv_root_lock); + gettimeofday(&cur_ts, NULL); + if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) { + FatalError(SC_ERR_SHUTDOWN, "unable to get all flow manager " + "threads to shutdown in time"); + } + SCMutexLock(&tv_root_lock); tv = tv_root[TVT_MGMT]; while (tv != NULL) { @@ -173,15 +176,8 @@ again: { if (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) { SCMutexUnlock(&tv_root_lock); - - usleep(WAIT_TIME); - total_wait_time += WAIT_TIME / 1000000.0; - if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) { - SCLogError(SC_ERR_FATAL, "Engine unable to " - "disable detect thread - \"%s\". " - "Killing engine", tv->name); - exit(EXIT_FAILURE); - } + /* sleep outside lock */ + SleepMsec(1); goto again; } } @@ -1052,13 +1048,17 @@ void FlowDisableFlowRecyclerThread(void) } SCMutexUnlock(&tv_root_lock); - double total_wait_time = 0; - /* value in seconds */ -#define THREAD_KILL_MAX_WAIT_TIME 60 - /* value in microseconds */ -#define WAIT_TIME 100 + struct timeval start_ts; + struct timeval cur_ts; + gettimeofday(&start_ts, NULL); again: + gettimeofday(&cur_ts, NULL); + if ((cur_ts.tv_sec - start_ts.tv_sec) > 60) { + FatalError(SC_ERR_SHUTDOWN, "unable to get all flow recycler " + "threads to shutdown in time"); + } + SCMutexLock(&tv_root_lock); tv = tv_root[TVT_MGMT]; while (tv != NULL) @@ -1067,27 +1067,20 @@ again: strlen(thread_name_flow_rec)) == 0) { if (!TmThreadsCheckFlag(tv, THV_RUNNING_DONE)) { - if (total_wait_time > THREAD_KILL_MAX_WAIT_TIME) { - SCLogError(SC_ERR_FATAL, "Engine unable to " - "disable detect thread - \"%s\". " - "Killing engine", tv->name); - exit(EXIT_FAILURE); - } SCMutexUnlock(&tv_root_lock); - usleep(WAIT_TIME); - total_wait_time += WAIT_TIME / 1000000.0; + /* sleep outside lock */ + SleepMsec(1); goto again; } } tv = tv->next; } + SCMutexUnlock(&tv_root_lock); /* wake up threads, another try */ for (u = 0; u < flowrec_number; u++) SCCtrlCondSignal(&flow_recycler_ctrl_cond); - SCMutexUnlock(&tv_root_lock); - /* reset count, so we can kill and respawn (unix socket) */ SC_ATOMIC_SET(flowrec_cnt, 0); return; -- 2.47.2