Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
#2531564: Fix leaky and brittle container serialization solution only handles services that are objects, but Drupal 9 has some (deprecated) services that are strings, that results in a type error.
Steps to reproduce
The quickest way to replicate is to run this code in 9.5.x using drush php
. The first statement just generates a deprecation warning, but the second generates an error.
>>> \Drupal::getContainer()->get('app.root')
<warning>PHP Deprecated: The "app.root" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the app.root parameter instead. See https://www.drupal.org/node/3080612 in /home/mortenson/repos/drupal/core/lib/Drupal/Component/DependencyInjection/Container.php on line 134</warning>
=> "/home/mortenson/repos/drupal"
>>> \Drupal::getContainer()->getServiceIdMappings()
<warning>PHP Deprecated: The "app.root" service is deprecated in drupal:9.0.0 and is removed from drupal:10.0.0. Use the app.root parameter instead. See https://www.drupal.org/node/3080612 in /home/mortenson/repos/drupal/core/lib/Drupal/Component/DependencyInjection/Container.php on line 134</warning>
TypeError: Argument 1 passed to Drupal\Component\DependencyInjection\Container::generateServiceIdHash() must be an object, string given, called in /home/mortenson/repos/drupal/core/lib/Drupal/Component/DependencyInjection/ServiceIdHashTrait.php on line 19
Proposed resolution
Handle the string services when serializing too.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#13 | 3310271-13.patch | 1.75 KB | catch |
#13 | 3310271-test-only.patch | 936 bytes | catch |
Comments
Comment #2
bradjones1E.g.,
app.root
, see #2531564-261: Fix leaky and brittle container serialization solutionComment #3
catchComment #4
andypostIt's one more reason to not use weakmap
Comment #6
benjifisherI am copying the steps to reproduce from #2531564-261: Fix leaky and brittle container serialization solution to the issue summary and crediting @samuel.mortenson.
I tested the instructions and got the same result.
Comment #7
longwaveCan we just do something like this in 9.5.x? Services must be objects in Drupal 10, so we don't need the fix there.
Comment #8
longwaveAlthough actually I doubt serializing the
app.root
string would have been correctly re-injected before, so maybe we just skip the case when the service isn't an object?Comment #9
BerdirYes, lets not try to handle strings in the trait. That's not really needed from a performance aspect either and while it could get out of sync, that's unchanged to HEAD.
that should also fix the composer install fatal error.
Comment #10
longwaveThis solves the case in the OP:
Comment #11
Wim LeersTagging to indicate this is a regression in
9.5.0
.Comment #12
benjifisherI applied the patch from #10 and repeated the steps in the issue description. There are no errors, just the deprecation warning:
I do notice some odd things, but I am not sure whether any of these are problems.
app.root.factory
, but notapp.root
, is one of the mappings.\Drupal::getContainer()->get('app.root.factory')
generates an error unless I do\Drupal::getContainer()->get('app.root')
first.The second point is not a new problem: I get the same behavior with 9.5.x and 9.4.5.
Comment #13
catchapp.root.factory
is a class so it's probably fine to serialize theoretically. It probably shouldn't be injected anything but it's already deprecated so I think that much is probably covered by the existing deprecation.Added a test based on the OP.
Comment #15
andypostI find it ready
Comment #16
neclimdulShoot sorry! I went on issue queue holiday. I remember seeing this at some point and thought we'd caught it but in a monster issue like that a subtle thing like this can easily look like a bug/cruft and get lost in a reroll/refactor. :(
Aweseome work! Agreed this looks like good to go.
Comment #17
alexpottCommitted 77d7fb6 and pushed to 9.5.x. Thanks!
This fix looks good and the test coverage complete.
Comment #19
maxstarkenburgThanks, I was wondering why a post-upgrade
drush updb
was erroring (before also realizing that the upgrade unexpectedly jumped me to 9.5.0-beta1, though that's a separate composer issue). Can confirm that having the patch in place would prevent the updb error.Comment #20
benjifisher