]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow-manager: improve thread shutdown loops
authorVictor Julien <victor@inliniac.net>
Thu, 21 Feb 2019 19:33:01 +0000 (20:33 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 22 Feb 2019 09:43:01 +0000 (10:43 +0100)
src/flow-manager.c

index 6f378dd3a1d935c88541e4c9dd0ce27ddf03d79f..97166fdb9a42a848517b0c2041133fba7964e100 100644 (file)
@@ -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;