Generated with the help of an LLM.
Problem/Motivation
Summary simplified "written by human"
After drush updatedb, all URLs respond "No route found" until the next drush cr.

We saw it in production with cache_metrics 2.2.0, which decorates cache_factory and injects current_user into it.
https://git.drupalcode.org/project/cache_metrics/-/blob/2.2.0/cache_metr...

The problem comes from the current_user service, which depends on event_dispatcher.
https://git.drupalcode.org/project/drupal/-/blob/main/core/core.services...

HookCollectorKeyValueWritePass calls $container->get('cache.bootstrap') during compilation.
https://git.drupalcode.org/project/drupal/-/blob/main/core/lib/Drupal/Co...
$container->get('cache.bootstrap')->deleteMultiple(['hook_data', 'theme_hook_data']);
Through the cache_metrics decorator, that instantiates current_user and, with it, event_dispatcher, before RegisterEventSubscribersPass has added the listeners to its definition. The dispatcher is therefore created with no listeners.
The command drush updatedb runs with allowDumping set to FALSE, so the ContainerBuilder itself becomes the runtime container and keeps that dispatcher. At the end of the update, drupal_flush_all_caches() calls router.builder->rebuild(). RoutingEvents::STATIC is dispatched but no subscriber receives it, so no routes are collected and the router table is saved empty.
Extended description (written with the help of an LLM):
Some core compiler passes instantiate services while the container is still being compiled:
- HookCollectorKeyValueWritePass (TYPE_OPTIMIZE) calls $container->get("keyvalue") and $container->get("cache.bootstrap") to write hook_data and clear its cache.
- DevelopmentSettingsPass (TYPE_BEFORE_OPTIMIZATION) calls $container->get("keyvalue") to read development_settings.
- BackendCompilerPass calls $container->get("database") to get the driver.
ContainerBuilder::get() stores those instances, and those of all its dependencies, in the builder. At that moment the definitions are still not final, because subsequent passes still modify them. For example, RegisterEventSubscribersPass (TYPE_AFTER_REMOVING) adds the addListener() calls to the event_dispatcher definition.
Normally this has no consequences. DrupalKernel::initializeContainer() dumps the builder to an array and creates a new Container, so the premature instances are discarded.
But when allowDumping is FALSE, the kernel uses the ContainerBuilder itself as the runtime container, and those instances pass to runtime just as they were created. It occurs in these cases:
- update.php creates its kernel with new UpdateKernel("prod", ..., FALSE).
- Drush does the same with the UpdateKernel (DrupalBoot8::bootstrapDrupalConfiguration(): $allow_dumping = $kernel !== Kernels::UPDATE), in each drush updatedb.
- The installer creates new InstallerKernel($environment, $class_loader, FALSE) (install.core.inc), and the InstallerKernel additionally forces allowDumping = FALSE.
- KernelTestBase uses the builder on purpose.
The underlying problem is that allowDumping mixes two distinct decisions:
a) Dump the builder to an array and create a clean Container, or reuse the builder.
b) Save the container in cache for the following requests (containerNeedsDumping and cacheDrupalContainer() only activate if allowDumping is TRUE).
The installer and update need to deactivate b, not a:
- In the installer, at the beginning there is no database or cache tables where to save the container. Additionally, InstallerServiceProvider replaces services (keyvalue in memory, among others) that should never reach the cache, and the container is recompiled each time a module is installed.
- In the update, UpdateServiceProvider decorates cache_factory with UpdateCacheBackendFactory and UpdateCompilerPass removes services with dependencies that cannot be resolved. It is a container exclusive to the update. If it were saved in cache, normal requests would load an update container; that's why UpdateKernel always forces recompilation.
Using the builder as a runtime container is a side effect of the flag. Only KernelTestBase really needs it, because its tests manipulate builder definitions and services.
With core alone, keyvalue, cache.bootstrap and database do not depend on services that are completed in later passes, so the bug is latent. It is enough for a module to decorate or alter one of those services for the chain to reach, for example, event_dispatcher:
HookCollectorKeyValueWritePass (TYPE_OPTIMIZE)
get('cache.bootstrap')
-> cache_factory (decorated by a module)
-> current_user
-> event_dispatcher <- instantiated with 0 listeners
RegisterEventSubscribersPass (TYPE_AFTER_REMOVING)
adds the listeners to the definition (the already created instance does not change)
drush updatedb -> drupal_flush_all_caches() -> router.builder->rebuild()
dispatch(RoutingEvents::STATIC) without listeners -> empty router table
There are precedents of the same pattern, services instantiated before the container is built:
- #3603333: Errors when stream wrappers instantiate services in constructors because register() is called before container is built: stream wrappers instantiated services when registered. It was fixed with StreamWrapperClassesPass, which collects class names during compilation without instantiating anything.
- #3497123: Accessing state service in container build time breaks the update process unexpectedly: a compiler pass that accesses state breaks drush updatedb during drupal_flush_all_caches().
- Redis #3564915: D11 regression: WSOD when Redis configured as cache backend: the deleteMultiple() of HookCollectorKeyValueWritePass reaches \Drupal::database() before the container exists and causes a WSOD with Redis as a backend.
Steps to reproduce
- Create a service decorating cache_factory with service "current_user"
services: testing.cache_factory: class: Drupal\repro\ReproCacheFactory decorates: cache_factory arguments: ['@testing.cache_factory.inner', '@current_user'] - drush cr and then drush sql:query "SELECT COUNT(*) FROM router": it will be N > 0.
- drush -y updatedb (with no updates).
- drush sql:query "SELECT COUNT(*) FROM router: returns 0. Any urls returns "No route found".
- The command drush cr recovers it.
Proposed resolution
Reset the builder when the container does not dump
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | event_dispatcher.png | 18.57 KB | eduardo morales alberti |
| #7 | cache_metrics_220_Services.png | 36.58 KB | eduardo morales alberti |
| #7 | cache_metrics_router.png | 296.26 KB | eduardo morales alberti |
Issue fork drupal-3643169
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
eduardo morales albertiComment #3
eduardo morales albertiComment #4
eduardo morales albertiComment #5
eduardo morales albertiComment #6
eduardo morales albertiComment #7
eduardo morales albertiComment #8
eduardo morales albertiComment #10
eduardo morales albertiFix applied, added test module with the cache factory service to reproduce the same bug.
Comment #11
eduardo morales albertiWaiting for tests
Comment #12
eduardo morales albertiThe reset solution affects the services defined on Tests, so it is not valid.
Comment #13
eduardo morales albertiComment #14
eduardo morales albertiAs the issue modifies how the kernel works, it needs deep revision. We will leave the issue open, as the problem exists, but the solution needs to be reviewed for edge cases.
Comment #15
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.