diff --git a/htdocs/drp/includes/bootstrap.inc b/htdocs/drp/includes/bootstrap.inc
index fd5475c..801ff64 100644
--- a/htdocs/drp/includes/bootstrap.inc
+++ b/htdocs/drp/includes/bootstrap.inc
@@ -551,11 +551,11 @@ function variable_init($conf = array()) {
     $variables = $cached->data;
   }
   else {
-    $result = db_query('SELECT * FROM {variable}');
-    while ($variable = db_fetch_object($result)) {
-      $variables[$variable->name] = unserialize($variable->value);
-    }
-    cache_set('variables', $variables);
+    // Signal to variable_reset_cache() that this is a read-only operation.
+    // In this case, if no lock can be acquired, the variable values are
+    // returned without writing to the persistent cache to avoid a race
+    // between reading and writing to the cache.
+    $variables = variable_reset_cache(FALSE);
   }
 
   foreach ($conf as $name => $value) {
@@ -566,6 +566,39 @@ function variable_init($conf = array()) {
 }
 
 /**
+ * Reset the variable cache safely.
+ *
+ * @param bool $variable_write
+ *   Flag to indicate if the cache rebuild was triggered by a change to the
+ *   variables table, defaults to TRUE so that the additional protection
+ *   against race conditions is utilized unless explicitly opted out of.
+ */
+function variable_reset_cache($variable_write = TRUE) {
+  $lock_name = __FUNCTION__;
+  if (!($lock_acquired = lock_acquire($lock_name, 1)) && $variable_write) {
+    // When we've made a change via variable_set() or variable_del(), we try
+    // harder to get the lock.
+    lock_wait($lock_name);
+    $lock_acquired = lock_acquire($lock_name, 1);
+  }
+  $result = db_query('SELECT * FROM {variable}');
+  while ($variable = db_fetch_object($result)) {
+    $variables[$variable->name] = unserialize($variable->value);
+  }
+  if ($lock_acquired) {
+    cache_set('variables', $variables);
+    lock_release($lock_name);
+  }
+  elseif ($variable_write) {
+    // When we've made a change via variable_set() or variable_del() and failed
+    // to rebuild the cache, we need to clear it to ensure that subsequent
+    // requests pick up the changes.
+    cache_clear_all('variables');
+  }
+  return $variables;
+}
+
+/**
  * Returns a persistent variable.
  *
  * Case-sensitivity of the variable_* functions depends on the database
@@ -604,6 +637,7 @@ function variable_get($name, $default) {
  */
 function variable_set($name, $value) {
   global $conf;
+  $conf[$name] = $value;
 
   $serialized_value = serialize($value);
   db_query("UPDATE {variable} SET value = '%s' WHERE name = '%s'", $serialized_value, $name);
@@ -611,9 +645,7 @@ function variable_set($name, $value) {
     @db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $serialized_value);
   }
 
-  cache_clear_all('variables', 'cache');
-
-  $conf[$name] = $value;
+  variable_reset_cache();
 }
 
 /**
@@ -632,9 +664,10 @@ function variable_del($name) {
   global $conf;
 
   db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
-  cache_clear_all('variables', 'cache');
 
   unset($conf[$name]);
+
+  variable_reset_cache();
 }
 
 
