diff --git a/core/includes/common.inc b/core/includes/common.inc
index 4668a90a9401604e07d44f57bbc124f2eb189b46..e03ba330fe9a654e74ab69c4916e32199c39473f 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -3,8 +3,6 @@
 use Drupal\Component\Utility\Crypt;
 use Drupal\Core\Cache\Cache;
 use Symfony\Component\DependencyInjection\Container;
-use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\Yaml\Parser;
 use Drupal\Component\PhpStorage\PhpStorageFactory;
 use Drupal\Component\Utility\NestedArray;
@@ -6069,10 +6067,11 @@ function drupal_implode_tags($tags) {
  *   requests.
  */
 function drupal_flush_all_caches() {
+  $module_handler = Drupal::moduleHandler();
   // Flush all persistent caches.
   // This is executed based on old/previously known information, which is
   // sufficient, since new extensions cannot have any primed caches yet.
-  module_invoke_all('cache_flush');
+  $module_handler->invokeAll('cache_flush');
   foreach (Cache::getBins() as $service_id => $cache_backend) {
     if ($service_id != 'cache.menu') {
       $cache_backend->deleteAll();
@@ -6088,16 +6087,32 @@ function drupal_flush_all_caches() {
   drupal_static_reset();
 
   // Clear all non-drupal_static() static caches.
-  // @todo Rebuild the kernel/container.
   Drupal::entityManager()->clearCachedDefinitions();
 
+  // Wipe the PHP Storage caches.
+  PhpStorageFactory::get('service_container')->deleteAll();
+  PhpStorageFactory::get('twig')->deleteAll();
+
   // Rebuild module and theme data.
-  system_rebuild_module_data();
+  $module_data = system_rebuild_module_data();
   system_rebuild_theme_data();
 
+  // Rebuild and reboot a new kernel. A simple DrupalKernel reboot is not
+  // sufficient, since the list of enabled modules might have been adjusted
+  // above due to changed code.
+  $files = array();
+  foreach ($module_data as $module => $data) {
+    if (isset($data->uri) && $data->status) {
+      $files[$module] = $data->uri;
+    }
+  }
+  Drupal::service('kernel')->updateModules($module_handler->getModuleList(), $files);
+  // New container, new module handler.
+  $module_handler = Drupal::moduleHandler();
+
   // Ensure that all modules that are currently supposed to be enabled are
   // actually loaded.
-  drupal_container()->get('module_handler')->loadAll();
+  $module_handler->loadAll();
 
   // Update the list of bootstrap modules.
   // Allows developers to get new bootstrap hooks implementations registered
@@ -6113,18 +6128,14 @@ function drupal_flush_all_caches() {
   drupal_get_schema(NULL, TRUE);
 
   // Rebuild all information based on new module data.
-  module_invoke_all('rebuild');
+  $module_handler->invokeAll('rebuild');
 
   // Rebuild the menu router based on all rebuilt data.
   // Important: This rebuild must happen last, so the menu router is guaranteed
   // to be based on up to date information.
-  drupal_container()->get('router.builder')->rebuild();
+  Drupal::service('router.builder')->rebuild();
   menu_router_rebuild();
 
-  // Wipe the PHP Storage caches.
-  PhpStorageFactory::get('service_container')->deleteAll();
-  PhpStorageFactory::get('twig')->deleteAll();
-
   // Re-initialize the maintenance theme, if the current request attempted to
   // use it. Unlike regular usages of this function, the installer and update
   // scripts need to flush all caches during GET requests/page building.
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index effdf3e30957bdbb9b5ced234de95d080cb733a1..8651134c8148d68310de8ea19ff5854621718867 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -431,7 +431,19 @@ protected function buildContainer() {
     foreach ($this->bundles as $bundle) {
       $bundle->build($container);
     }
-    $container->setParameter('persistIds', array_keys($container->findTaggedServiceIds('persist')));
+
+    // Identify all services whose instances should be persisted when rebuilding
+    // the container during the lifetime of the kernel (e.g., during a kernel
+    // reboot). Include synthetic services, because by definition, they cannot
+    // be automatically reinstantiated. Also include services tagged to persist.
+    $persist_ids = array();
+    foreach ($container->getDefinitions() as $id => $definition) {
+      if ($definition->isSynthetic() || $definition->getTag('persist')) {
+        $persist_ids[] = $id;
+      }
+    }
+    $container->setParameter('persistIds', $persist_ids);
+
     $container->compile();
     return $container;
   }
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 76988fa1f868d2a33258d0ab08c8972f4d0d810f..af86db7e0e2215c2f3e7989a3ae2ffc5d05e33c2 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -909,6 +909,7 @@ protected function writeSettings($settings) {
   protected function resetAll() {
     // Clear all database and static caches and rebuild data structures.
     drupal_flush_all_caches();
+    $this->container = \Drupal::getContainer();
 
     // Reload global $conf array and permissions.
     $this->refreshVariables();
diff --git a/index.php b/index.php
index f0949e0638b8d7662122d9c3e43e2a994864e1b0..4934bd2700d717ce49c02d3ca66bfd2fc1cca9c8 100644
--- a/index.php
+++ b/index.php
@@ -9,4 +9,10 @@
  */
 
 require_once __DIR__ . '/core/includes/bootstrap.inc';
-drupal_handle_request();
+try {
+  drupal_handle_request();
+}
+catch (Exception $e) {
+  print 'If you have just changed code (for example deployed a new module or moved an existing one) read http://drupal.org/documentation/rebuild';
+  throw $e;
+}