Browse Source

Fix Coverity Defect CID 304732 (#9402)

Fix a race-hazard in the shutdown sequence that could deadlock the agent.
Andrew Moss 4 years ago
parent
commit
641a459237
2 changed files with 8 additions and 5 deletions
  1. 8 3
      database/rrdhost.c
  2. 0 2
      libnetdata/threads/threads.c

+ 8 - 3
database/rrdhost.c

@@ -686,11 +686,16 @@ void rrdhost_free(RRDHOST *host) {
         if (host->receiver) {
             if (!host->receiver->exited)
                 netdata_thread_cancel(host->receiver->thread);
-            while (!host->receiver->exited)
+            netdata_mutex_unlock(&host->receiver_lock);
+            struct receiver_state *rpt = host->receiver;
+            while (host->receiver && !rpt->exited)
                 sleep_usec(50 * USEC_PER_MS);
-            destroy_receiver_state(host->receiver);
+            // If the receiver detached from the host then its thread will destroy the state
+            if (host->receiver == rpt)
+                destroy_receiver_state(host->receiver);
         }
-        netdata_mutex_unlock(&host->receiver_lock);
+        else
+            netdata_mutex_unlock(&host->receiver_lock);
     }
 
 

+ 0 - 2
libnetdata/threads/threads.c

@@ -157,8 +157,6 @@ void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
 
 void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
 {
-    int ret = 0;
-
     threadname[0] = '\0';
 #if defined(__FreeBSD__)
     pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);