diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php
index d725f5b23d0bd53637703ea0f0e2aabf20da14bc..a231669f9cdc94bc5e0031d780ebd05d994729a4 100644
--- a/core/.phpstan-baseline.php
+++ b/core/.phpstan-baseline.php
@@ -3487,12 +3487,6 @@
 	'count' => 1,
 	'path' => __DIR__ . '/lib/Drupal/Core/CoreServiceProvider.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Method Drupal\\\\Core\\\\CoreServiceProvider\\:\\:registerTest\\(\\) has no return type specified\\.$#',
-	'identifier' => 'missingType.return',
-	'count' => 1,
-	'path' => __DIR__ . '/lib/Drupal/Core/CoreServiceProvider.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Method Drupal\\\\Core\\\\Cron\\:\\:invokeCronHandlers\\(\\) has no return type specified\\.$#',
 	'identifier' => 'missingType.return',
diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php
index 6150c188d182b746bcf26d2e6cf42796e58de236..cd9766c14b5eec8e889c97dd16622a9ca2ac04a0 100644
--- a/core/lib/Drupal/Core/CoreServiceProvider.php
+++ b/core/lib/Drupal/Core/CoreServiceProvider.php
@@ -34,7 +34,6 @@
 use Drupal\Core\Site\Settings;
 use Psr\Log\LoggerAwareInterface;
 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
-use Symfony\Component\DependencyInjection\Reference;
 use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
@@ -56,8 +55,6 @@ class CoreServiceProvider implements ServiceProviderInterface, ServiceModifierIn
    * {@inheritdoc}
    */
   public function register(ContainerBuilder $container) {
-    $this->registerTest($container);
-
     // Only register the private file stream wrapper if a file path has been
     // set.
     if (Settings::get('file_private_path')) {
@@ -162,32 +159,4 @@ public function alter(ContainerBuilder $container) {
     }
   }
 
-  /**
-   * Registers services and event subscribers for a site under test.
-   *
-   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
-   *   The container builder.
-   */
-  protected function registerTest(ContainerBuilder $container) {
-    // Do nothing if we are not in a test environment.
-    if (!drupal_valid_test_ua()) {
-      return;
-    }
-    // The test middleware is not required for kernel tests as there is no child
-    // site. DRUPAL_TEST_IN_CHILD_SITE is not defined in this case.
-    if (!defined('DRUPAL_TEST_IN_CHILD_SITE')) {
-      return;
-    }
-    // Add the HTTP request middleware to Guzzle.
-    $container
-      ->register('test.http_client.middleware', 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware')
-      ->addTag('http_client_middleware');
-    // Add the wait terminate middleware which acquires a lock to signal request
-    // termination to the test runner.
-    $container
-      ->register('test.http_middleware.wait_terminate_middleware', 'Drupal\Core\Test\StackMiddleware\TestWaitTerminateMiddleware')
-      ->setArguments([new Reference('state'), new Reference('lock')])
-      ->addTag('http_middleware', ['priority' => -1024]);
-  }
-
 }
diff --git a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
index 187851a7c82a9c7f7fbf55786aa513d7154a3409..6a4dcfcf8b02d3ed65e594f2c210035887745c3b 100644
--- a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
+++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
@@ -167,6 +167,17 @@ protected function prepareSettings() {
         'tags' => [['name' => 'event_subscriber']],
       ];
     }
+    // Register test middleware.
+    $services['services']['testing.http_client.middleware'] = [
+      'class' => 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware',
+      'tags' => [['name' => 'http_client_middleware']],
+    ];
+    $services['services']['testing.http_middleware.wait_terminate_middleware'] = [
+      'class' => 'Drupal\Core\Test\StackMiddleware\TestWaitTerminateMiddleware',
+      'arguments' => ['@lock', '%drupal.test_wait_terminate%'],
+      'tags' => [['name' => 'http_middleware', 'priority' => -1024]],
+    ];
+    $services['parameters']['drupal.test_wait_terminate'] = FALSE;
     file_put_contents($directory . '/services.yml', $yaml->dump($services));
     // Since Drupal is bootstrapped already, install_begin_request() will not
     // bootstrap again. Hence, we have to reload the newly written custom
diff --git a/core/lib/Drupal/Core/Test/StackMiddleware/TestWaitTerminateMiddleware.php b/core/lib/Drupal/Core/Test/StackMiddleware/TestWaitTerminateMiddleware.php
index be2a675d0209574189794615445b50f31d86316b..6724b814cca473213bdcc621f2a358912a27e822 100644
--- a/core/lib/Drupal/Core/Test/StackMiddleware/TestWaitTerminateMiddleware.php
+++ b/core/lib/Drupal/Core/Test/StackMiddleware/TestWaitTerminateMiddleware.php
@@ -3,7 +3,6 @@
 namespace Drupal\Core\Test\StackMiddleware;
 
 use Drupal\Core\Lock\LockBackendInterface;
-use Drupal\Core\State\StateInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -18,15 +17,15 @@ class TestWaitTerminateMiddleware implements HttpKernelInterface {
    *
    * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
    *   The decorated kernel.
-   * @param \Drupal\Core\State\StateInterface $state
-   *   The state server.
    * @param \Drupal\Core\Lock\LockBackendInterface $lock
    *   The lock backend.
+   * @param bool $waitForTerminate
+   *   Container parameter to toggle this behavior.
    */
   public function __construct(
     protected HttpKernelInterface $httpKernel,
-    protected StateInterface $state,
     protected LockBackendInterface $lock,
+    protected bool $waitForTerminate = FALSE,
   ) {
   }
 
@@ -36,14 +35,16 @@ public function __construct(
   public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
     $result = $this->httpKernel->handle($request, $type, $catch);
 
-    if ($this->state->get('drupal.test_wait_terminate')) {
-      // Set a header on the response to instruct the test runner that it must
-      // await the lock. Note that the lock acquired here is automatically
-      // released from within a shutdown function.
-      $this->lock->acquire('test_wait_terminate');
-      $result->headers->set('X-Drupal-Wait-Terminate', '1');
+    if (!$this->waitForTerminate) {
+      return $result;
     }
 
+    // Set a header on the response to instruct the test runner that it must
+    // await the lock. Note that the lock acquired here is automatically
+    // released from within a shutdown function.
+    $this->lock->acquire('test_wait_terminate');
+    $result->headers->set('X-Drupal-Wait-Terminate', '1');
+
     return $result;
   }
 
diff --git a/core/tests/Drupal/Tests/WaitTerminateTestTrait.php b/core/tests/Drupal/Tests/WaitTerminateTestTrait.php
index ad629abbae4ec6fc6b7954adfb899edab48ad34d..0c273751b61e6eb4d562f7eeac9c2e8db58a4247 100644
--- a/core/tests/Drupal/Tests/WaitTerminateTestTrait.php
+++ b/core/tests/Drupal/Tests/WaitTerminateTestTrait.php
@@ -17,7 +17,8 @@ trait WaitTerminateTestTrait {
    * event need to enable this.
    */
   protected function setWaitForTerminate(): void {
-    $this->container->get('state')->set('drupal.test_wait_terminate', TRUE);
+    $this->setContainerParameter('drupal.test_wait_terminate', TRUE);
+    $this->rebuildContainer();
   }
 
 }