Verified Commit 76c65d27 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3626014 DrupalKernel::terminate() leaves the main request on the stack...

fix: #3626014 DrupalKernel::terminate() leaves the main request on the stack when termination throws

By: ptmkenny
By: bbrala
By: daffie
(cherry picked from commit 290b5ad0)
parent 514b9ae3
Loading
Loading
Loading
Loading
Loading
+12 −8
Changes for core/lib/Drupal/Core/DrupalKernel.php: 12 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -692,28 +692,32 @@ public function getServiceProviders($origin) {
   */
  public function terminate(Request $request, Response $response): void {
    if ($this->booted && $this->getHttpKernel() instanceof TerminableInterface) {
      // Only run terminate() when essential services have been set up properly
      // by preHandle() before.
      try {
        // Only run terminate() when essential services have been set up
        // properly by preHandle() before.
        if ($this->prepared === TRUE) {
          $this->getHttpKernel()->terminate($request, $response);
        }
      // For destructable services, always call the destruct method if they were
      // initialized during the request. Destruction is not necessary if the
      // service was not used.
        // For destructable services, always call the destruct method if they
        // were initialized during the request. Destruction is not necessary if
        // the service was not used.
        foreach ($this->container->getParameter('kernel.destructable_services') as $id) {
          if ($this->container->initialized($id)) {
            $service = $this->container->get($id);
            $service->destruct();
          }
        }

      // Pop the request added in \Drupal\Core\DrupalKernel::preHandle() from
      // request stack at the end of the execution cycle.
      }
      finally {
        // Release the request even if termination fails. Subsequent requests
        // may bypass preHandle(), for example when served from the page cache.
        if ($this->prepared === TRUE) {
          $this->prepared = FALSE;
          $this->container->get('request_stack')->pop();
        }
      }
    }
  }

  /**
   * {@inheritdoc}
+59
Changes for core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelRequestStackTest.php: 59 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

@@ -38,6 +39,11 @@ class DrupalKernelRequestStackTest extends KernelTestBase implements EventSubscr
   */
  protected ?int $recordedRequestStackCount;

  /**
   * Whether the kernel terminate event listener throws an exception.
   */
  protected bool $throwOnTerminate = FALSE;

  /**
   * {@inheritdoc}
   */
@@ -146,6 +152,46 @@ public function testRequestStackHandling(): void {
    $this->assertSame(0, $this->getRequestStackCount($request_stack));
  }

  /**
   * Tests the request stack when termination fails.
   */
  public function testRequestStackAfterFailedTermination(): void {
    /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
    $request_stack = \Drupal::service('request_stack');
    // KernelTestBase pushes a request onto the stack.
    $request_stack->pop();

    /** @var \Drupal\Core\DrupalKernel $http_kernel */
    $http_kernel = \Drupal::service('kernel');

    $main_request = Request::create('/http-kernel-test');
    $main_response = $http_kernel->handle($main_request);
    $this->assertSame(1, $this->getRequestStackCount($request_stack));

    $this->throwOnTerminate = TRUE;
    try {
      $http_kernel->terminate($main_request, $main_response);
      $this->fail('Expected exception was not thrown.');
    }
    catch (\RuntimeException $e) {
      $this->assertSame('Termination failed.', $e->getMessage());
    }
    // The main request is popped even though termination failed.
    $this->assertNull($request_stack->getMainRequest());
    $this->assertSame(0, $this->getRequestStackCount($request_stack));

    // A request that bypasses preHandle(), such as a page cache hit, must not
    // be terminated or pop a request it did not push.
    $other_request = Request::create('/http-kernel-test');
    $request_stack->push($other_request);
    $http_kernel->terminate($other_request, $main_response);
    $this->assertSame($other_request, $request_stack->getMainRequest());
    $this->assertSame(1, $this->getRequestStackCount($request_stack));

    $request_stack->pop();
    $this->throwOnTerminate = FALSE;
  }

  /**
   * {@inheritdoc}
   */
@@ -174,12 +220,25 @@ public function onRequest(RequestEvent $event): void {
    $this->recordedRequestStackCount = $this->getRequestStackCount($request_stack);
  }

  /**
   * Throws an exception during termination when requested by a test.
   *
   * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
   *   The terminate event.
   */
  public function onTerminate(TerminateEvent $event): void {
    if ($this->throwOnTerminate) {
      throw new \RuntimeException('Termination failed.');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      KernelEvents::REQUEST => ['onRequest', 50],
      KernelEvents::TERMINATE => ['onTerminate'],
    ];
  }