Fix Coverity Defect CID 304732 (#9402)
Fix a race-hazard in the shutdown sequence that could deadlock the agent.
Andrew Moss committed
Jun 23, 2020 at 18:06 UTC
641a45923789b738a3c75183b5b44b6d26964c86
2 files changed
+8
-5
database/rrdhost.c
+8
-3
@@ -686,11 +686,16 @@ void rrdhost_free(RRDHOST *host) {
686
if (host->receiver) {
687
if (!host->receiver->exited)
688
netdata_thread_cancel(host->receiver->thread);
689
- while (!host->receiver->exited)
689
+ netdata_mutex_unlock(&host->receiver_lock);
690
+ struct receiver_state *rpt = host->receiver;
691
+ while (host->receiver && !rpt->exited)
692
sleep_usec(50 * USEC_PER_MS);
691
- destroy_receiver_state(host->receiver);
693
+ // If the receiver detached from the host then its thread will destroy the state
694
+ if (host->receiver == rpt)
695
+ destroy_receiver_state(host->receiver);
696
}
693
- netdata_mutex_unlock(&host->receiver_lock);
697
+ else
698
+ netdata_mutex_unlock(&host->receiver_lock);
699
}
700
701
libnetdata/threads/threads.c
-2
@@ -157,8 +157,6 @@ void uv_thread_set_name_np(uv_thread_t ut, const char* name) {
157
158
void os_thread_get_current_name_np(char threadname[NETDATA_THREAD_NAME_MAX + 1])
159
{
160
- int ret = 0;
161
-
160
threadname[0] = '\0';
161
#if defined(__FreeBSD__)
162
pthread_get_name_np(pthread_self(), threadname, NETDATA_THREAD_NAME_MAX + 1);