]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/loader: add threading coverity warning
authorVictor Julien <vjulien@oisf.net>
Sat, 21 Jun 2025 10:42:17 +0000 (12:42 +0200)
committerVictor Julien <victor@inliniac.net>
Sat, 21 Jun 2025 19:32:54 +0000 (21:32 +0200)
 lock_acquire: Calling pthread_mutex_lock acquires lock ThreadVars_.ctrl_mutex.
725        SCCtrlMutexLock(th_v->ctrl_mutex);

CID 1554214: (#1 of 1): Indefinite wait (BAD_CHECK_OF_WAIT_COND)
dead_wait: A wait is performed without ensuring that the condition is not already satisfied while holding lock ThreadVars_.ctrl_mutex. This can cause a deadlock if the notification happens before the lock is acquired.
      Acquire the lock, then check the wait condition in a loop, without releasing with the lock before the wait. This will prevent deadlocks and failed conditions from spurious wakeups.

src/detect-engine-loader.c

index ac93b1b2a7270f966ca6aa4cced3e8ce0d08aa19..81e9d9c3077f0d56f38719114617a312beaaed7d 100644 (file)
@@ -717,13 +717,22 @@ static TmEcode DetectLoader(ThreadVars *th_v, void *thread_data)
 
         SCMutexUnlock(&loader->m);
 
-        if (TmThreadsCheckFlag(th_v, THV_KILL)) {
-            break;
-        }
-
         /* just wait until someone wakes us up */
         SCCtrlMutexLock(th_v->ctrl_mutex);
-        SCCtrlCondWait(th_v->ctrl_cond, th_v->ctrl_mutex);
+        int rc = 0;
+        while (rc == 0) {
+            if (TmThreadsCheckFlag(th_v, THV_KILL)) {
+                run = false;
+                break;
+            }
+            SCMutexLock(&loader->m);
+            bool has_work = loader->task_list.tqh_first != NULL;
+            SCMutexUnlock(&loader->m);
+            if (has_work)
+                break;
+
+            rc = SCCtrlCondWait(th_v->ctrl_cond, th_v->ctrl_mutex);
+        }
         SCCtrlMutexUnlock(th_v->ctrl_mutex);
 
         SCLogDebug("woke up...");