diff --git a/core/core.services.yml b/core/core.services.yml
index 7d83bb12f8eead2538b3a95ada80974eeef19438..e3c1e094a21298dfe9c3f18dc6fee2d2a942f2aa 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -373,6 +373,8 @@ services:
   context.repository:
     class: Drupal\Core\Plugin\Context\LazyContextRepository
     arguments: ['@service_container']
+  Drupal\Component\DependencyInjection\ReverseContainer:
+    arguments: [ '@service_container' ]
   cron:
     class: Drupal\Core\Cron
     arguments: ['@module_handler', '@lock', '@queue', '@state', '@account_switcher', '@logger.channel.cron', '@plugin.manager.queue_worker', '@datetime.time']
diff --git a/core/lib/Drupal/Component/DependencyInjection/ContainerInterface.php b/core/lib/Drupal/Component/DependencyInjection/ContainerInterface.php
index e6c5ea72d455be9d705e4db9dd1f04e6423ea9f7..47567c5cdf866129cff544e7a4af4f4dc8aadf71 100644
--- a/core/lib/Drupal/Component/DependencyInjection/ContainerInterface.php
+++ b/core/lib/Drupal/Component/DependencyInjection/ContainerInterface.php
@@ -25,6 +25,11 @@ public function getServiceIds();
    *
    * @return array
    *   Service ids keyed by a unique hash.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   public function getServiceIdMappings(): array;
 
@@ -36,6 +41,11 @@ public function getServiceIdMappings(): array;
    *
    * @return string
    *   A unique hash identifying the object.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   public function generateServiceIdHash(object $object): string;
 
diff --git a/core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php b/core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php
new file mode 100644
index 0000000000000000000000000000000000000000..92e1fa58bf37ee012c25baaf1714824e2358a201
--- /dev/null
+++ b/core/lib/Drupal/Component/DependencyInjection/ReverseContainer.php
@@ -0,0 +1,107 @@
+<?php
+
+namespace Drupal\Component\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
+use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
+
+/**
+ * Retrieves service IDs from the container for public services.
+ *
+ * Heavily inspired by \Symfony\Component\DependencyInjection\ReverseContainer.
+ */
+final class ReverseContainer {
+
+  /**
+   * The service container.
+   *
+   * @var \Drupal\Component\DependencyInjection\Container|\Symfony\Component\DependencyInjection\Container
+   */
+  private $serviceContainer;
+
+  /**
+   * A closure on the container that can search for services.
+   *
+   * @var \Closure
+   */
+  private $getServiceId;
+
+  /**
+   * A static map of services to a hash.
+   *
+   * @var array
+   */
+  private static $recordedServices = [];
+
+  /**
+   * Constructs a ReverseContainer object.
+   *
+   * @param \Drupal\Component\DependencyInjection\Container|\Symfony\Component\DependencyInjection\Container $serviceContainer
+   *   The service container.
+   */
+  public function __construct($serviceContainer) {
+    if (!($serviceContainer instanceof Container || $serviceContainer instanceof SymfonyContainer)) {
+      throw new \InvalidArgumentException('The container must be an instance of \Drupal\Component\DependencyInjection\Container or \Symfony\Component\DependencyInjection\Container');
+    }
+    $this->serviceContainer = $serviceContainer;
+    $this->getServiceId = \Closure::bind(function ($service): ?string {
+      return array_search($service, $this->services, TRUE) ?: NULL;
+    }, $serviceContainer, $serviceContainer);
+  }
+
+  /**
+   * Returns the ID of the passed object when it exists as a service.
+   *
+   * To be reversible, services need to be public.
+   *
+   * @param object $service
+   *   The service to find the ID for.
+   */
+  public function getId($service): ?string {
+    if ($this->serviceContainer === $service || $service instanceof SymfonyContainerInterface) {
+      return 'service_container';
+    }
+
+    $hash = $this->generateServiceIdHash($service);
+    $id = self::$recordedServices[$hash] ?? ($this->getServiceId)($service);
+
+    if ($id !== NULL && $this->serviceContainer->has($id)) {
+      self::$recordedServices[$hash] = $id;
+      return $id;
+    }
+
+    return NULL;
+  }
+
+  /**
+   * Records a map of the container's services.
+   *
+   * This method is used so that stale services can be serialized after a
+   * container has been re-initialized.
+   */
+  public function recordContainer(): void {
+    $service_recorder = \Closure::bind(function () : array {
+      return array_filter($this->services, 'is_object');
+    }, $this->serviceContainer, $this->serviceContainer);
+    self::$recordedServices = array_merge(self::$recordedServices, array_flip(array_map([$this, 'generateServiceIdHash'], $service_recorder())));
+  }
+
+  /**
+   * Generates an identifier for a service based on the object class and hash.
+   *
+   * @param object $object
+   *   The object to generate an identifier for.
+   *
+   * @return string
+   *   The object's class and hash concatenated together.
+   */
+  private function generateServiceIdHash(object $object): string {
+    // Include class name as an additional namespace for the hash since
+    // spl_object_hash's return can be recycled. This still is not a 100%
+    // guarantee to be unique but makes collisions incredibly difficult and even
+    // then the interface would be preserved.
+    // @see https://php.net/spl_object_hash#refsect1-function.spl-object-hash-notes
+    return get_class($object) . spl_object_hash($object);
+  }
+
+}
diff --git a/core/lib/Drupal/Component/DependencyInjection/ServiceIdHashTrait.php b/core/lib/Drupal/Component/DependencyInjection/ServiceIdHashTrait.php
index 97bfaf156313101fa198ec35403a3297140a0d15..4812ccb593671d3e8beece87c259710e6e2581d6 100644
--- a/core/lib/Drupal/Component/DependencyInjection/ServiceIdHashTrait.php
+++ b/core/lib/Drupal/Component/DependencyInjection/ServiceIdHashTrait.php
@@ -5,7 +5,10 @@
 /**
  * A trait for service id hashing implementations.
  *
- * Handles delayed cache tag invalidations.
+ * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+ *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+ *
+ * @see https://www.drupal.org/node/3327942
  */
 trait ServiceIdHashTrait {
 
@@ -13,6 +16,7 @@ trait ServiceIdHashTrait {
    * Implements \Drupal\Component\DependencyInjection\ContainerInterface::getServiceIdMappings()
    */
   public function getServiceIdMappings(): array {
+    @trigger_error(__METHOD__ . "() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942", E_USER_DEPRECATED);
     $mapping = [];
     foreach ($this->getServiceIds() as $service_id) {
       if ($this->initialized($service_id) && $service_id !== 'service_container') {
@@ -29,6 +33,7 @@ public function getServiceIdMappings(): array {
    * Implements \Drupal\Component\DependencyInjection\ContainerInterface::generateServiceIdHash()
    */
   public function generateServiceIdHash(object $object): string {
+    @trigger_error(__METHOD__ . "() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942", E_USER_DEPRECATED);
     // Include class name as an additional namespace for the hash since
     // spl_object_hash's return can be recycled. This still is not a 100%
     // guarantee to be unique but makes collisions incredibly difficult and even
diff --git a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
index a23a2f487ab0cafb204ce245a6955b480da593a2..daf0c94a28dc9441335d90ff52038a2451d17d1a 100644
--- a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
+++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
@@ -2,9 +2,9 @@
 
 namespace Drupal\Core\DependencyInjection;
 
+use Drupal\Component\DependencyInjection\ReverseContainer;
 use Drupal\Core\Entity\EntityStorageInterface;
-use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
-use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
+use Drupal\Core\StringTranslation\TranslatableMarkup;
 
 /**
  * Provides dependency injection friendly methods for serialization.
@@ -34,8 +34,12 @@ public function __sleep() {
     $vars = get_object_vars($this);
     try {
       $container = \Drupal::getContainer();
-      $mapping = \Drupal::service('kernel')->getServiceIdMapping();
+      $reverse_container = $container->has(ReverseContainer::class) ? $container->get(ReverseContainer::class) : new ReverseContainer($container);
       foreach ($vars as $key => $value) {
+        if (!is_object($value) || $value instanceof TranslatableMarkup) {
+          // Ignore properties that cannot be services.
+          continue;
+        }
         if ($value instanceof EntityStorageInterface) {
           // If a class member is an entity storage, only store the entity type
           // ID the storage is for, so it can be used to get a fresh object on
@@ -47,34 +51,18 @@ public function __sleep() {
           $this->_entityStorages[$key] = $value->getEntityTypeId();
           unset($vars[$key]);
         }
-        elseif (is_object($value)) {
-          $service_id = FALSE;
-          // Special case the container.
-          if ($value instanceof SymfonyContainerInterface) {
-            $service_id = 'service_container';
-          }
-          else {
-            $id = $container->generateServiceIdHash($value);
-            if (isset($mapping[$id])) {
-              $service_id = $mapping[$id];
-            }
-          }
-          if ($service_id) {
-            // If a class member was instantiated by the dependency injection
-            // container, only store its ID so it can be used to get a fresh object
-            // on unserialization.
-            $this->_serviceIds[$key] = $service_id;
-            unset($vars[$key]);
-          }
+        elseif ($service_id = $reverse_container->getId($value)) {
+          // If a class member was instantiated by the dependency injection
+          // container, only store its ID so it can be used to get a fresh object
+          // on unserialization.
+          $this->_serviceIds[$key] = $service_id;
+          unset($vars[$key]);
         }
       }
     }
     catch (ContainerNotInitializedException $e) {
       // No container, no problem.
     }
-    catch (ServiceNotFoundException $e) {
-      // No kernel, very strange, but still no problem.
-    }
 
     return array_keys($vars);
   }
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 635201593def6dd11c3c279c9eece75b8b6938a9..c70ccdc61b9c9889771a52803c21221be2d4e484 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -11,6 +11,7 @@
 use Drupal\Core\Config\BootstrapConfigStorageFactory;
 use Drupal\Core\Config\NullStorage;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Component\DependencyInjection\ReverseContainer;
 use Drupal\Core\DependencyInjection\ServiceModifierInterface;
 use Drupal\Core\DependencyInjection\ServiceProviderInterface;
 use Drupal\Core\DependencyInjection\YamlFileLoader;
@@ -244,6 +245,11 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
 
   /**
    * A mapping from service classes to service IDs.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   protected $serviceIdMapping = [];
 
@@ -828,8 +834,14 @@ public function updateModules(array $module_list, array $module_filenames = [])
    *
    * @return string
    *   A unique hash value.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   public static function generateServiceIdHash($object) {
+    @trigger_error(__METHOD__ . "() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942", E_USER_DEPRECATED);
     // Include class name as an additional namespace for the hash since
     // spl_object_hash's return can be recycled. This still is not a 100%
     // guarantee to be unique but makes collisions incredibly difficult and even
@@ -842,6 +854,7 @@ public static function generateServiceIdHash($object) {
    * {@inheritdoc}
    */
   public function getServiceIdMapping() {
+    @trigger_error(__METHOD__ . "() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942", E_USER_DEPRECATED);
     $this->collectServiceIdMapping();
     return $this->serviceIdMapping;
   }
@@ -891,8 +904,12 @@ protected function initializeContainer() {
       if ($this->container->initialized('current_user')) {
         $current_user_id = $this->container->get('current_user')->id();
       }
-      // Save the current services.
-      $this->collectServiceIdMapping();
+      // After rebuilding the container some objects will have stale services.
+      // Record a map of objects to service IDs prior to rebuilding the
+      // container in order to ensure
+      // \Drupal\Core\DependencyInjection\DependencySerializationTrait works as
+      // expected.
+      $this->container->get(ReverseContainer::class)->recordContainer();
 
       // If there is a session, close and save it.
       if ($this->container->initialized('session')) {
@@ -1602,8 +1619,14 @@ protected function addServiceFiles(array $service_yamls) {
 
   /**
    * Collect a mapping between service to ids.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   protected function collectServiceIdMapping() {
+    @trigger_error(__METHOD__ . "() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942", E_USER_DEPRECATED);
     if (isset($this->container)) {
       foreach ($this->container->getServiceIdMappings() as $hash => $service_id) {
         $this->serviceIdMapping[$hash] = $service_id;
diff --git a/core/lib/Drupal/Core/DrupalKernelInterface.php b/core/lib/Drupal/Core/DrupalKernelInterface.php
index 0378dff3e7dadd535210005bd7a2a9b44d8dc68e..dfd00f20170ec8eef732ca2438859e592fa014f9 100644
--- a/core/lib/Drupal/Core/DrupalKernelInterface.php
+++ b/core/lib/Drupal/Core/DrupalKernelInterface.php
@@ -139,6 +139,11 @@ public function loadLegacyIncludes();
 
   /**
    * Get a mapping from service hashes to service IDs.
+   *
+   * @deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the
+   *   'Drupal\Component\DependencyInjection\ReverseContainer' service instead.
+   *
+   * @see https://www.drupal.org/node/3327942
    */
   public function getServiceIdMapping();
 
diff --git a/core/lib/Drupal/Core/Test/TestKernel.php b/core/lib/Drupal/Core/Test/TestKernel.php
index cf7ff4e723f31b9a54eb3b356122f24d707fcdbe..952692b9fe070bcde0652624b467640fdca3fd4a 100644
--- a/core/lib/Drupal/Core/Test/TestKernel.php
+++ b/core/lib/Drupal/Core/Test/TestKernel.php
@@ -3,6 +3,7 @@
 namespace Drupal\Core\Test;
 
 use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Component\DependencyInjection\ReverseContainer;
 use Drupal\Core\DrupalKernel;
 
 /**
@@ -38,6 +39,7 @@ public static function setContainerWithKernel() {
     // inside those objects.
     $kernel->container = $container;
     $container->set('kernel', $kernel);
+    $container->set(ReverseContainer::class, new ReverseContainer($container));
     \Drupal::setContainer($container);
     return $container;
   }
diff --git a/core/modules/system/tests/src/Kernel/DecoratedServiceTest.php b/core/modules/system/tests/src/Kernel/DecoratedServiceTest.php
index 099019da517e8e12c695f8015584692923863a01..f1bc1bd88eb68fd52e5640300ed87b792e6bf077 100644
--- a/core/modules/system/tests/src/Kernel/DecoratedServiceTest.php
+++ b/core/modules/system/tests/src/Kernel/DecoratedServiceTest.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\Tests\system\Kernel;
 
+use Drupal\Component\DependencyInjection\ReverseContainer;
 use Drupal\decorated_service_test\TestServiceDecorator;
 use Drupal\KernelTests\KernelTestBase;
 
@@ -22,16 +23,12 @@ class DecoratedServiceTest extends KernelTestBase {
   public function testDecoratedServiceId() {
     // Service decorated once.
     $test_service = $this->container->get('test_service');
-    $hash = $this->container->generateServiceIdHash($test_service);
-    $mappings = $this->container->getServiceIdMappings();
-    $this->assertEquals('test_service', $mappings[$hash]);
+    $this->assertEquals('test_service', $this->container->get(ReverseContainer::class)->getId($test_service));
     $this->assertInstanceOf(TestServiceDecorator::class, $test_service);
 
     // Service decorated twice.
     $test_service2 = $this->container->get('test_service2');
-    $hash = $this->container->generateServiceIdHash($test_service2);
-    $mappings = $this->container->getServiceIdMappings();
-    $this->assertEquals('test_service2', $mappings[$hash]);
+    $this->assertEquals('test_service2', $this->container->get(ReverseContainer::class)->getId($test_service2));
     $this->assertInstanceOf(TestServiceDecorator::class, $test_service2);
   }
 
diff --git a/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php
index 05609018883a7a572d082c379054f9f7a38d3e6d..580b58591d7cc487bf412da649b3adcf180e7ebd 100644
--- a/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php
+++ b/core/tests/Drupal/Tests/Component/DependencyInjection/ContainerTest.php
@@ -10,6 +10,7 @@
 use Drupal\Component\Utility\Crypt;
 use Drupal\Tests\PhpUnitCompatibilityTrait;
 use PHPUnit\Framework\TestCase;
+use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
 use Symfony\Component\DependencyInjection\Exception\LogicException;
@@ -24,7 +25,7 @@
  * @group DependencyInjection
  */
 class ContainerTest extends TestCase {
-
+  use ExpectDeprecationTrait;
   use PhpUnitCompatibilityTrait;
 
   /**
@@ -688,8 +689,12 @@ public function testResolveServicesAndParametersForRawArgument() {
   /**
    * @covers \Drupal\Component\DependencyInjection\ServiceIdHashTrait::getServiceIdMappings
    * @covers \Drupal\Component\DependencyInjection\ServiceIdHashTrait::generateServiceIdHash
+   *
+   * @group legacy
    */
   public function testGetServiceIdMappings() {
+    $this->expectDeprecation("Drupal\Component\DependencyInjection\ServiceIdHashTrait::generateServiceIdHash() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
+    $this->expectDeprecation("Drupal\Component\DependencyInjection\ServiceIdHashTrait::getServiceIdMappings() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
     $this->assertEquals([], $this->container->getServiceIdMappings());
     $s1 = $this->container->get('other.service');
     $s2 = $this->container->get('late.service');
diff --git a/core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php b/core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..ec4e1a67da8f4c5de50b28632c0e2f565b79a21a
--- /dev/null
+++ b/core/tests/Drupal/Tests/Component/DependencyInjection/ReverseContainerTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace Drupal\Tests\Component\DependencyInjection;
+
+use Drupal\Component\DependencyInjection\ReverseContainer;
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/**
+ * @runTestsInSeparateProcesses
+ *   The reverse container uses a static to maintain information across
+ *   container rebuilds.
+ *
+ * @coversDefaultClass \Drupal\Component\DependencyInjection\ReverseContainer
+ * @group DependencyInjection
+ */
+class ReverseContainerTest extends TestCase {
+
+  /**
+   * @covers ::getId
+   */
+  public function testGetId(): void {
+    $container = new ContainerBuilder();
+    $service = new \stdClass();
+    $container->set('bar', $service);
+
+    $reverse_container = new ReverseContainer($container);
+
+    $this->assertSame('bar', $reverse_container->getId($service));
+    $non_service = new \stdClass();
+    $this->assertNull($reverse_container->getId($non_service));
+    $this->assertSame('service_container', $reverse_container->getId($container));
+  }
+
+  /**
+   * @covers ::recordContainer
+   */
+  public function testRecordContainer(): void {
+    $container = new ContainerBuilder();
+    $service = new \stdClass();
+    $container->set('bar', $service);
+
+    $reverse_container = new ReverseContainer($container);
+    $reverse_container->recordContainer();
+
+    $container = new ContainerBuilder();
+    $reverse_container = new ReverseContainer($container);
+
+    // New container does not have a bar service.
+    $this->assertNull($reverse_container->getId($service));
+
+    // Add the bar service to make the lookup based on the old object work as
+    // expected.
+    $container->set('bar', new \stdClass());
+    $this->assertSame('bar', $reverse_container->getId($service));
+  }
+
+  /**
+   * @covers ::recordContainer
+   * @group legacy
+   */
+  public function testRecordContainerStringService(): void {
+    $container = new ContainerBuilder();
+    $service = new \stdClass();
+    $container->set('bar', $service);
+    // String services are deprecated in Symfony 4.4.
+    $container->set('string_service', 'A string');
+
+    $reverse_container = new ReverseContainer($container);
+    $reverse_container->recordContainer();
+
+    $container = new ContainerBuilder();
+    $reverse_container = new ReverseContainer($container);
+
+    // New container does not have a bar service.
+    $this->assertNull($reverse_container->getId($service));
+
+    // Add the bar service to make the lookup based on the old object work as
+    // expected.
+    $container->set('bar', new \stdClass());
+    $this->assertSame('bar', $reverse_container->getId($service));
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
index 22f4d53a96fa4a545be6b4a36bde1c7fb1894ea6..ca80fb128dc080ff1150bb46dfd13039639656fa 100644
--- a/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
+++ b/core/tests/Drupal/Tests/Core/DependencyInjection/DependencySerializationTest.php
@@ -7,6 +7,8 @@
 
 namespace Drupal\Tests\Core\DependencyInjection;
 
+use Drupal\Component\DependencyInjection\ReverseContainer;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
 use Drupal\Core\Test\TestKernel;
 use Drupal\Tests\UnitTestCase;
@@ -37,11 +39,36 @@ public function testSerialization() {
     /** @var \Drupal\Tests\Core\DependencyInjection\DependencySerializationTestDummy $dependencySerialization */
     $dependencySerialization = unserialize($string);
 
+    $this->assertTrue($container->has(ReverseContainer::class));
     $this->assertSame($service, $dependencySerialization->service);
     $this->assertSame($container, $dependencySerialization->container);
     $this->assertEmpty($dependencySerialization->getServiceIds());
   }
 
+  /**
+   * @covers ::__sleep
+   * @covers ::__wakeup
+   */
+  public function testSerializationWithoutReverseContainer() {
+    $container = new ContainerBuilder();
+    \Drupal::setContainer($container);
+    // Create a pseudo service and dependency injected object.
+    $service = new \stdClass();
+    $container->set('test_service', $service);
+    $this->assertSame($container, $container->get('service_container'));
+
+    $dependencySerialization = new DependencySerializationTestDummy($service);
+    $dependencySerialization->setContainer($container);
+
+    $string = serialize($dependencySerialization);
+    /** @var \Drupal\Tests\Core\DependencyInjection\DependencySerializationTestDummy $dependencySerialization */
+    $dependencySerialization = unserialize($string);
+
+    $this->assertFalse($container->has(ReverseContainer::class));
+    $this->assertSame($service, $dependencySerialization->service);
+    $this->assertSame($container, $dependencySerialization->container);
+  }
+
 }
 
 /**
diff --git a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
index b94cf7ec05cca7d6ca3919ec91797a178205bc10..16b5b0d4757a6ccbda183dae05542b1f5c43ad70 100644
--- a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
+++ b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
@@ -140,8 +140,11 @@ public function testFindSitePath() {
 
   /**
    * @covers ::getServiceIdMapping
+   * @group legacy
    */
   public function testGetServiceIdMapping() {
+    $this->expectDeprecation("Drupal\Core\DrupalKernel::getServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
+    $this->expectDeprecation("Drupal\Core\DrupalKernel::collectServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
     $service = new BarClass();
     $container = TestKernel::setContainerWithKernel();
     $container->set('bar', $service);