From: Victor Julien Date: Thu, 21 Feb 2019 19:33:01 +0000 (+0100) Subject: flow-manager: improve thread shutdown loops X-Git-Tag: suricata-5.0.0-beta1~186 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=034724ec7239072c58014f55f0b06b920000d27c;p=thirdparty%2Fsuricata.git flow-manager: improve thread shutdown loops --- 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;