[rrd-developers] [PATCH] rrd_daemon.c: Fixed a possible race-condition in UPDATE.

Sebastian Harl sh at tokkee.org
Mon Jul 13 09:28:27 CEST 2009


handle_request_update() checks if an entry for some file already exists in the
cache. If that is not the case, the cache lock is released while creating the
new entry (which includes a call to stat() which, supposedly, might block up
to multiple seconds on busy systems). Later, (after re-acquiring the cache
lock) the new entry is added to the cache, overwriting any existing entry for
the same file, thus, possibly overwriting an entry inserted by another UPDATE
in the meantime (while the cache had not been locked), thus, losing the data 
added by that UPDATE.

Now, we check again if the entry already exists before inserting the new
entry. If an entry does exist by now, it is used instead of the newly
allocated entry.
---
 program/src/rrd_daemon.c |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)

diff --git a/program/src/rrd_daemon.c b/program/src/rrd_daemon.c
index 17ec97e..149c4a7 100644
--- a/program/src/rrd_daemon.c
+++ b/program/src/rrd_daemon.c
@@ -1314,6 +1314,7 @@ static int handle_request_update (HANDLER_PROTO) /* {{{ */
   if (ci == NULL) /* {{{ */
   {
     struct stat statbuf;
+    cache_item_t *tmp;
 
     /* don't hold the lock while we setup; stat(2) might block */
     pthread_mutex_unlock(&cache_lock);
@@ -1361,7 +1362,17 @@ static int handle_request_update (HANDLER_PROTO) /* {{{ */
     pthread_cond_init(&ci->flushed, NULL);
 
     pthread_mutex_lock(&cache_lock);
-    g_tree_replace (cache_tree, (void *) ci->file, (void *) ci);
+
+    /* another UPDATE might have added this entry in the meantime */
+    tmp = g_tree_lookup (cache_tree, file);
+    if (NULL == tmp)
+      g_tree_replace (cache_tree, (void *) ci->file, (void *) ci);
+    else
+    {
+      ci->flags = 0;
+      free_cache_item (ci);
+      ci = tmp;
+    }
   } /* }}} */
   assert (ci != NULL);
 
-- 
1.5.6.5

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
Url : http://lists.oetiker.ch/pipermail/rrd-developers/attachments/20090713/3b6d9cd0/attachment.bin 


More information about the rrd-developers mailing list