From 24043759311c76c9696c17b621866e5c29989dc1 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Tue, 15 Mar 2011 02:05:50 -0400
Subject: [PATCH] Initialize database before calling lock_acquire.

---
 includes/bootstrap.inc |   44 ++++++++++++++++++++++++++++++++------------
 includes/common.inc    |   15 ++++++++++++++-
 2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index fe1741d..b4976db 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -856,11 +856,32 @@ function drupal_page_get_cache($check_only = FALSE) {
   }
 
   if (drupal_page_is_cacheable()) {
-    $cache = cache_get($base_root . request_uri(), 'cache_page');
+    // If the site is configured to load the page from cache without hitting
+    // the database, drupal_page_get_cache() could get called before the lock
+    // system has been initialized.
+
+    $cid = $base_root . request_uri();
+    if (!($cache = cache_get($cid, 'cache_page'))) {
+      if (variable_get('page_cache_without_database')) {
+        // The lock_acquire() function requires the database.
+        require_once DRUPAL_ROOT . '/includes/database/database.inc');
+        require_once DRUPAL_ROOT . '/' . variable_get('lock_inc', 'includes/lock.inc');
+        lock_initialize();
+      }
+      if (!lock_acquire($cid)) {
+        lock_wait($cid);
+        if (!$cache = cache_get($cid, 'cache_page')) {
+          // Proceed with page build, cache its result, then release the lock.
+          lock_acquire($cid);
+        }
+      }
+    }
+
     if ($cache !== FALSE) {
       $cache_hit = TRUE;
+      return $cache;
     }
-    return $cache;
+    header('X-Drupal-Cache: MISS');
   }
 }
 
@@ -2022,13 +2043,17 @@ function _drupal_bootstrap_page_cache() {
   foreach (variable_get('cache_backends', array()) as $include) {
     require_once DRUPAL_ROOT . '/' . $include;
   }
-  // Check for a cache mode force from settings.php.
+  // Check for a cache mode force from settings.php. It's not safe to invoke
+  // hooks without the database, so set a flag based on the value of
+  // $conf['page_cache_without_database'].
   if (variable_get('page_cache_without_database')) {
     $cache_enabled = TRUE;
+    $hook_invocation_is_safe = FALSE;
   }
   else {
     drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
     $cache_enabled = variable_get('cache');
+    $hook_invocation_is_safe = TRUE;
   }
   drupal_block_denied(ip_address());
   // If there is no session cookie and cache is enabled (or forced), try
@@ -2046,23 +2071,18 @@ function _drupal_bootstrap_page_cache() {
       $_GET['q'] = $cache->data['path'];
       drupal_set_title($cache->data['title'], PASS_THROUGH);
       date_default_timezone_set(drupal_get_user_timezone());
-      // If the skipping of the bootstrap hooks is not enforced, call
-      // hook_boot.
-      if (variable_get('page_cache_invoke_hooks', TRUE)) {
+      // If the skipping of bootstrap hooks is not enforced, call hook_boot().
+      if ($hook_invocation_is_safe && variable_get('page_cache_invoke_hooks', TRUE)) {
         bootstrap_invoke_all('boot');
       }
       drupal_serve_page_from_cache($cache);
-      // If the skipping of the bootstrap hooks is not enforced, call
-      // hook_exit.
-      if (variable_get('page_cache_invoke_hooks', TRUE)) {
+      // If the skipping of bootstrap hooks is not enforced, call hook_exit().
+      if ($hook_invocation_is_safe && variable_get('page_cache_invoke_hooks', TRUE)) {
         bootstrap_invoke_all('exit');
       }
       // We are done.
       exit;
     }
-    else {
-      header('X-Drupal-Cache: MISS');
-    }
   }
 }
 
diff --git a/includes/common.inc b/includes/common.inc
index e9286f4..8a51dcd 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -4976,6 +4976,7 @@ function drupal_page_set_cache() {
         $cache->data['body'] = gzencode($cache->data['body'], 9, FORCE_GZIP);
       }
       cache_set($cache->cid, $cache->data, 'cache_page', $cache->expire);
+      lock_release($cache->cid);
     }
     return $cache;
   }
@@ -5788,7 +5789,14 @@ function drupal_render_cache_get($elements) {
   }
   $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
 
-  if (!empty($cid) && $cache = cache_get($cid, $bin)) {
+  if (!($cache = cache_get($cid, $bin)) && !lock_acquire($cid)) {
+    lock_wait($cid);
+    if (!$cache = cache_get($cid, $bin)) {
+      lock_acquire($cid);
+    }
+  }
+
+  if ($cache) {
     // Add additional libraries, JavaScript, CSS and other data attached
     // to this element.
     if (isset($cache->data['#attached'])) {
@@ -5797,6 +5805,10 @@ function drupal_render_cache_get($elements) {
     // Return the rendered output.
     return $cache->data['#markup'];
   }
+
+  // Proceed with rendering. Even if we failed to acquire the lock, we
+  // should try to render the elements. Locking is a pluggable system, so the
+  // site may still function even when repeated attempts to use it fail.
   return FALSE;
 }
 
@@ -5834,6 +5846,7 @@ function drupal_render_cache_set(&$markup, $elements) {
   $bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
   $expire = isset($elements['#cache']['expire']) ? $elements['#cache']['expire'] : CACHE_PERMANENT;
   cache_set($cid, $data, $bin, $expire);
+  lock_release($cid);
 }
 
 /**
-- 
1.7.1

