\Drupal::$container is not initialized yet

Created on 27 August 2024, 4 months ago
Updated 11 September 2024, 3 months ago

Problem/Motivation

When trying to run a database update using
drush updb or /update.php

the page is broken with:

The website encountered an unexpected error. Try again later.
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 169 of /web/core/lib/Drupal.php).

Drupal::getContainer() (Line: 543)
Drupal::moduleHandler() (Line: 105)
Drupal\perimeter\EventSubscriber\PerimeterSubscriber::getSubscribedEvents() (Line: 37)
Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass->process() (Line: 80)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile() (Line: 767)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1447)
Drupal\Core\DrupalKernel->compileContainer() (Line: 971)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 42)
Drupal\Core\Update\UpdateKernel->initializeContainer() (Line: 515)
Drupal\Core\DrupalKernel->boot() (Line: 65)
Drupal\Core\Update\UpdateKernel->handle() (Line: 27)

This seems to be caused by the last release. Commenting out these lines as a quickfix brings back the functionality:
  /**
   * {@inheritdoc}
   *
   * Note, that the events won't trigger on already cached pages and therefore
   * accessing cached pages won't trigger bans.
   */
  public static function getSubscribedEvents() {
    // Use onKernelResponse if the fast404 module is enabled, as
    // "onKernelException" is not triggered for fast404 requests.
    // for more information, see
    // https://www.drupal.org/project/perimeter/issues/3349053 and
    // https://www.drupal.org/project/perimeter/issues/3470229.
    // if (\Drupal::moduleHandler()->moduleExists('fast404')) {
    //   $events[KernelEvents::RESPONSE][] = ['onKernelResponse'];
    // }
    // else {
    //   $events[KernelEvents::EXCEPTION][] = ['onKernelException'];
    // }

    return $events;
  }

My assumption is that

\Drupal::moduleHandler()->moduleExists()

may not be used in such an early bootstrapping phase.

I'd suggest to remove the if clause for a hotfix until there's a final solution. The fast404 support is less important than the currently broken pages!

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024