diff --git a/includes/common.inc b/includes/common.inc
index 34fa9b9..5087617 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -5255,7 +5255,21 @@ function _drupal_bootstrap_full() {
     // - The theme can have hook_*_alter() implementations affect page building
     //   (e.g., hook_form_alter(), hook_node_view_alter(), hook_page_alter()),
     //   ahead of when rendering starts.
-    menu_set_custom_theme();
+    $custom_theme = menu_set_custom_theme();
+    // If the custom theme is set and the base theme is set and they differ,
+    // reset the theme.
+    global $theme;
+    if (!empty($custom_theme) && !empty($theme) && $custom_theme != $theme) {
+      drupal_static_reset('theme_get_registry');
+      $drupal_static_fast['registry'] = &drupal_static('theme_get_registry');
+      $drupal_static_fast['registry'] = array();
+      drupal_static_reset('drupal_add_js');
+      drupal_static_reset('drupal_add_css');
+      unset($GLOBALS['theme']);
+    }
+    // Initialize the theme with the final initialization flag, this ensures
+    // that the theme is properly set even if it was initialized by calling
+    // theme related function during bootstrap.
     drupal_theme_initialize();
     module_invoke_all('init');
   }
diff --git a/includes/menu.inc b/includes/menu.inc
index 1fe5a64..60abab4 100644
--- a/includes/menu.inc
+++ b/includes/menu.inc
@@ -1777,7 +1777,7 @@ function menu_get_custom_theme($initialize = FALSE) {
  * Sets a custom theme for the current page, if there is one.
  */
 function menu_set_custom_theme() {
-  menu_get_custom_theme(TRUE);
+  return menu_get_custom_theme(TRUE);
 }
 
 /**
diff --git a/modules/simpletest/simpletest.info b/modules/simpletest/simpletest.info
index 1aec619..77bcad9 100644
--- a/modules/simpletest/simpletest.info
+++ b/modules/simpletest/simpletest.info
@@ -15,6 +15,7 @@ files[] = tests/boot.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..20c8a64
--- /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' && $node->promote) {
+      return 'garland';
+    }
+    elseif ($node->type == 'article' && $node->promote) {
+      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..21e736f
--- /dev/null
+++ b/modules/simpletest/tests/custom_theme_boot_test.test
@@ -0,0 +1,78 @@
+<?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,
+    ));
+    $node3 = $this->drupalCreateNode(array(
+      'type' => 'page',
+      'promote' => 0,
+    ));
+    $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('themes/seven/style.css');
+    $this->assertNoText('themes/garland/style.css');
+    $this->assertNoText('themes/bartik/css/style.css');
+
+    $this->drupalGet('node/' . $node2->nid);
+    $this->assertText('themes/garland/style.css');
+    $this->assertNoText('themes/seven/style.css');
+    $this->assertNoText('themes/bartik/css/style.css');
+
+    // This makes sure that the base theme still loads as well.
+    $this->drupalGet('node/' . $node3->nid);
+    $this->assertText('themes/bartik/css/style.css');
+    $this->assertNoText('themes/garland/style.css');
+    $this->assertNoText('themes/seven/style.css');
+  }
+}
