Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.453
diff -u -p -r1.453 bootstrap.inc
--- includes/bootstrap.inc	11 Dec 2010 21:03:47 -0000	1.453
+++ includes/bootstrap.inc	15 Dec 2010 16:16:44 -0000
@@ -738,20 +738,11 @@ function variable_initialize($conf = arr
     $variables = $cached->data;
   }
   else {
-    // Cache miss. Avoid a stampede.
-    $name = 'variable_init';
-    if (!lock_acquire($name, 1)) {
-      // Another request is building the variable cache.
-      // Wait, then re-run this function.
-      lock_wait($name);
-      return variable_initialize($conf);
-    }
-    else {
-      // Proceed with variable rebuild.
-      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
-      cache_set('variables', $variables, 'cache_bootstrap');
-      lock_release($name);
-    }
+    // 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) {
@@ -762,6 +753,34 @@ function variable_initialize($conf = arr
 }
 
 /**
+ * Reset the variable cache safely.
+ *
+ * @param bool $variable_write
+ *   Flag to indicate if we are resetting the cache after a write operation.
+ */
+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);
+  }
+  $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
+  if ($lock_acquired) {
+    cache_set('variables', $variables, 'cache_bootstrap');
+    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', 'cache_bootstrap');
+  }
+  return $variables;
+}
+
+/**
  * Returns a persistent variable.
  *
  * Case-sensitivity of the variable_* functions depends on the database
@@ -803,12 +822,10 @@ function variable_get($name, $default = 
  */
 function variable_set($name, $value) {
   global $conf;
+  $conf[$name] = $value;
 
   db_merge('variable')->key(array('name' => $name))->fields(array('value' => serialize($value)))->execute();
-
-  cache_clear_all('variables', 'cache_bootstrap');
-
-  $conf[$name] = $value;
+  variable_reset_cache();
 }
 
 /**
@@ -830,9 +847,10 @@ function variable_del($name) {
   db_delete('variable')
     ->condition('name', $name)
     ->execute();
-  cache_clear_all('variables', 'cache_bootstrap');
 
   unset($conf[$name]);
+
+  variable_reset_cache();
 }
 
 /**
Index: includes/install.core.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.core.inc,v
retrieving revision 1.46
diff -u -p -r1.46 install.core.inc
--- includes/install.core.inc	4 Dec 2010 01:54:37 -0000	1.46
+++ includes/install.core.inc	15 Dec 2010 16:16:44 -0000
@@ -241,6 +241,9 @@ function install_begin_request(&$install
   // This must go after drupal_bootstrap(), which unsets globals!
   global $conf;
 
+  // Initialize the locking system.
+  require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
+  lock_initialize();
   require_once DRUPAL_ROOT . '/modules/system/system.install';
   require_once DRUPAL_ROOT . '/includes/common.inc';
   require_once DRUPAL_ROOT . '/includes/file.inc';
