? .directory
? drupal-561990-D6.patch
Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.206.2.23
diff -u -p -r1.206.2.23 bootstrap.inc
--- includes/bootstrap.inc	2 Jun 2010 13:07:45 -0000	1.206.2.23
+++ includes/bootstrap.inc	24 Jun 2010 21:44:51 -0000
@@ -541,16 +541,30 @@ function drupal_get_filename($type, $nam
  * file.
  */
 function variable_init($conf = array()) {
-  // NOTE: caching the variables improves performance by 20% when serving cached pages.
+  // NOTE: caching the variables improves performance by 20% when serving
+  // cached pages.
   if ($cached = cache_get('variables', 'cache')) {
     $variables = $cached->data;
   }
   else {
-    $result = db_query('SELECT * FROM {variable}');
-    while ($variable = db_fetch_object($result)) {
-      $variables[$variable->name] = unserialize($variable->value);
+    // 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_init($conf);
+    }
+    else {
+      // Proceed with variable rebuild.
+      $result = db_query('SELECT * FROM {variable}');
+      $variables = array();
+      while ($variable = db_fetch_object($result)) {
+        $variables[$variable->name] = unserialize($variable->value);
+      }
+      cache_set('variables', $variables);
+      lock_release($name);
     }
-    cache_set('variables', $variables);
   }
 
   foreach ($conf as $name => $value) {
