Code

write_graphite plugin: Fix locking.
[collectd.git] / src / write_graphite.c
index de460d7cfc0ba39e94ba4ae94fad4e45b25d34de..6b16bec215539245220fa662352604aad4ecef83 100644 (file)
@@ -120,20 +120,15 @@ static int wg_send_buffer (struct wg_callback *cb)
                 status,
                 strerror (errno));
 
-        pthread_mutex_trylock (&cb->send_lock);
-
-        DEBUG ("write_graphite plugin: closing socket and restting fd "
-                "so reinit will occur");
         close (cb->sock_fd);
         cb->sock_fd = -1;
 
-        pthread_mutex_unlock (&cb->send_lock);
-
         return (-1);
     }
     return (0);
 }
 
+/* NOTE: You must hold cb->send_lock when calling this function! */
 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
 {
     int status;
@@ -240,14 +235,20 @@ static void wg_callback_free (void *data)
 
     cb = data;
 
+    pthread_mutex_lock (&cb->send_lock);
+
     wg_flush_nolock (/* timeout = */ 0, cb);
 
     close(cb->sock_fd);
+    cb->sock_fd = -1;
+
     sfree(cb->node);
     sfree(cb->service);
     sfree(cb->prefix);
     sfree(cb->postfix);
 
+    pthread_mutex_destroy (&cb->send_lock);
+
     sfree(cb);
 }