diff --git a/includes/common.inc b/includes/common.inc
index 0437ec1..1757cb4 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -5241,6 +5241,12 @@ function _drupal_bootstrap_full() {
     //   (e.g., hook_form_alter(), hook_node_view_alter(), hook_page_alter()),
     //   ahead of when rendering starts.
     menu_set_custom_theme();
+    // Set the global variable that tells drupal_theme_initialize() it's allowed
+    // to run the full process. If this is done before this point nasty
+    // side-effects occur. A theme initialization before this point can be
+    // easily triggered when hooks are implemented. E.h. a single call to l()
+    // can cause a cache inconsistency.
+    $GLOBALS['drupal_theme_initialize_allowed'] = TRUE;
     drupal_theme_initialize();
     module_invoke_all('init');
   }
diff --git a/includes/theme.inc b/includes/theme.inc
index 8d5348d..240ea96 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -68,6 +68,12 @@ function _drupal_theme_access($theme) {
  * Initializes the theme system by loading the theme.
  */
 function drupal_theme_initialize() {
+  // Ensure we've reached a bootstrap level in which a theme initialization is
+  // possible without causing side-effects. This global is set in
+  // _drupal_bootstrap_full().
+  if (empty($GLOBALS['drupal_theme_initialize_allowed'])) {
+    return;
+  }
   global $theme, $user, $theme_key;
 
   // If $theme is already set, assume the others are set, too, and do nothing
@@ -253,6 +259,12 @@ function _drupal_theme_initialize($theme, $base_theme = array(), $registry_callb
  *   class.
  */
 function theme_get_registry($complete = TRUE) {
+  // Ensure we've reached a bootstrap level in which a theme initialization is
+  // possible without causing side-effects. This global is set in
+  // _drupal_bootstrap_full().
+  if (empty($GLOBALS['drupal_theme_initialize_allowed'])) {
+    return array();
+  }
   // Use the advanced drupal_static() pattern, since this is called very often.
   static $drupal_static_fast;
   if (!isset($drupal_static_fast)) {
diff --git a/includes/theme.maintenance.inc b/includes/theme.maintenance.inc
index 6baf219..d15352c 100644
--- a/includes/theme.maintenance.inc
+++ b/includes/theme.maintenance.inc
@@ -16,6 +16,12 @@
  */
 function _drupal_maintenance_theme() {
   global $theme, $theme_key, $conf;
+  // Set the global variable that tells drupal_theme_initialize() it's allowed
+  // to run the full process. If this is done before this point nasty
+  // side-effects occur. A theme initialization before this point can be
+  // easily triggered when hooks are implemented. E.h. a single call to l()
+  // can cause a cache inconsistency.
+  $GLOBALS['drupal_theme_initialize_allowed'] = TRUE;
 
   // If $theme is already set, assume the others are set too, and do nothing.
   if (isset($theme)) {
diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info
index 7b139ba..0866f60 100644
--- a/modules/simpletest/simpletest.info
+++ b/modules/simpletest/simpletest.info
@@ -14,6 +14,7 @@ files[] = tests/batch.test
 files[] = tests/bootstrap.test
 files[] = tests/cache.test
 files[] = tests/common.test
+files[] = tests/custom_theme_boot_test.test
 files[] = tests/database_test.test
 files[] = tests/entity_crud.test
 files[] = tests/entity_crud_hook_test.test
diff --git a/modules/simpletest/tests/custom_theme_boot_test.info b/modules/simpletest/tests/custom_theme_boot_test.info
new file mode 100644
index 0000000..3b1d2dd
--- /dev/null
+++ b/modules/simpletest/tests/custom_theme_boot_test.info
@@ -0,0 +1,6 @@
+name = Custom theme boot tests
+description = A support module for custom theme hook boot testing.
+core = 7.x
+package = Testing
+version = VERSION
+hidden = TRUE
diff --git a/modules/simpletest/tests/custom_theme_boot_test.module b/modules/simpletest/tests/custom_theme_boot_test.module
new file mode 100644
index 0000000..d89402e
--- /dev/null
+++ b/modules/simpletest/tests/custom_theme_boot_test.module
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @file
+ * Helper module for the custom theme boot tests.
+ */
+
+/**
+ * Implements hook_custom_theme().
+ */
+function custom_theme_boot_test_custom_theme() {
+  if (arg(0) == 'node' && is_numeric(arg(1))) {
+    // This triggers a node load - which again could trigger a whole bunch of
+    // theme related stuff e.g. by triggering token replacement in fields. If
+    // the theme handling isn't right this will lead to a broken theme registry
+    // and / or the wrong theme load.
+    $node = node_load(arg(1));
+    if ($node->type == 'page') {
+      return 'garland';
+    }
+    elseif ($node->type == 'article') {
+      return 'seven';
+    }
+  }
+}
+
+/**
+ * Implements hook_entity_load().
+ */
+function custom_theme_boot_test_entity_load($entities, $type) {
+  if ($type == 'node') {
+    foreach ($entities as $entity) {
+      // This is going to initialize the theme and break everything if not
+      // properly handled.
+      $entity->foo = l(t('foo'), '');
+    }
+  }
+}
diff --git a/modules/simpletest/tests/custom_theme_boot_test.test b/modules/simpletest/tests/custom_theme_boot_test.test
new file mode 100644
index 0000000..67f1804
--- /dev/null
+++ b/modules/simpletest/tests/custom_theme_boot_test.test
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @file
+ * Helper module for the custom theme boot tests.
+ */
+
+/**
+ * Tests for custom theme boot behaviour.
+ */
+class CustomThemeBootTestCase extends NodeWebTestCase {
+  protected $profile = 'testing';
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Custom theme boot tests',
+      'description' => 'Test that triggering custom theme on boot wont compromise the theme registry.',
+      'group' => 'Theme',
+    );
+  }
+
+  public function setUp() {
+    parent::setUp('custom_theme_boot_test');
+    // Create and login admin user.
+    $this->admin_user = $this->drupalCreateUser(array(
+      'access administration pages',
+      'administer site configuration',
+      'administer themes',
+    ));
+  }
+
+  /**
+   * Create four nodes and ensure they're loaded correctly.
+   */
+  public function testNodeMultipleLoad() {
+    // Create nodes to fetch.
+    $node1 = $this->drupalCreateNode(array(
+      'type' => 'article',
+      'promote' => 1,
+    ));
+    $node2 = $this->drupalCreateNode(array(
+      'type' => 'page',
+      'promote' => 1,
+    ));
+    $this->drupalLogin($this->admin_user);
+    $this->drupalGet('admin/appearance');
+
+    // Enable Garland theme.
+    $this->clickLink('Enable');
+    // Enable Seven theme.
+    $this->clickLink('Enable');
+
+    // Clear all caches.
+    cache_clear_all();
+
+    // Fetch the node. This triggers hook_entity_load(), which is implemented by
+    // custom_theme_boot_test.module. The implementation calls l() which again
+    // end's up loading the theme.
+    $this->drupalGet('node/' . $node1->nid);
+    $this->assertText('seven');
+    $this->assertNoText('garland');
+    $this->assertNoText('bartik');
+
+    $this->drupalGet('node/' . $node2->nid);
+    $this->assertText('garland');
+    $this->assertNoText('seven');
+    $this->assertNoText('bartik');
+  }
+}
