From: Victor Julien Date: Sat, 21 Jun 2025 10:42:17 +0000 (+0200) Subject: detect/loader: add threading coverity warning X-Git-Tag: suricata-8.0.0~37 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=65ff3dfa88504320179317e7a9834213a0279a6e;p=thirdparty%2Fsuricata.git detect/loader: add threading coverity warning 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. --- diff --git a/src/detect-engine-loader.c b/src/detect-engine-loader.c index ac93b1b2a7..81e9d9c307 100644 --- a/src/detect-engine-loader.c +++ b/src/detect-engine-loader.c @@ -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...");