Problem/Motivation

During a recent deployment, we saw errors in the logs:

TypeError: Drupal\trash\EventSubscriber\TrashConfigSubscriber::__construct(): 
Argument #6 ($loggerFactory) must be of type Drupal\Core\Logger\LoggerChannelFactoryInterface, Drupal\Core\Action\ActionManager given

At first, we thought the problem was that Commit ed599e43 added a parameter to TrashConfigSubscriber but neglected to update trash.services.yml. Further analysis showed that autowiring should have handled that, so the problem is that the service container was not rebuilt during the deployment.

This may have been caused by a delay between updating the code and updating the database, but it can also happen when deploying with drush updatedb --no-cache-clear. I am not sure how common that is, but the option is there and some site owners will use it in order to avoid clearing caches unnecessarily.

This problem is temporary: as soon as the container is rebuilt, for whatever reason, it goes away. Fixing this issue should help anyone who uses that option and has not yet updated this module to 3.0.35+.

Steps to reproduce

  1. Install Trash 3.0.29 on a Drupal 10.3+ site and enable it for at least one entity type (e.g. Content).
  2. Update the code to 3.0.35:
    composer require drupal/trash:3.0.35
  3. Run drush updatedb --no-cache-clear. It reports "No pending updates."
  4. Visit the Trash settings page. It fails with an error from one of several classes whose constructors changed between 3.0.29 and 3.0.35, for example:
    ArgumentCountError: Too few arguments to function Drupal\trash\TrashEntityPurger::__construct(), 6 passed ... and exactly 7 expected
  5. Alternatively, save trash.settings from the command line:
    drush cset trash.settings auto_purge.enabled 1 -y
    This fails in the config subscriber:
    TypeError: Drupal\trash\EventSubscriber\TrashConfigSubscriber::__construct(): Argument #6 ($loggerFactory) must be of type Drupal\Core\Logger\LoggerChannelFactoryInterface, Drupal\Core\Action\ActionManager given

Proposed resolution

  1. Add a post-update function that rebuilds the service container. Testing shows that an empty post-update function does not trigger a rebuild with drush updatedb --no-cache-clear.
  2. Remove explicit parameters from trash.services.yml to avoid confusion.
  3. Pass a logger object instead of the logger factory, to follow Drupal standards.

Remaining tasks

User interface changes

None

API changes

The trash.config_subscriber service expects a logger object, not the logger factory.

Data model changes

None

Issue fork trash-3632459

Command icon 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

benjifisher created an issue. See original summary.

benjifisher’s picture

Assigned: benjifisher » Unassigned
Issue summary: View changes
Status: Active » Needs review

I worked with Claude code on this issue. I reviewed the code changes, and they are the same things I would have done on my own. I wrote the issue summary myself, except that Claude drafted the steps to reproduce. I tested those steps and the fix (Drupal 10.6.15).

Regarding the 3 items in the proposed resolution:

  1. This is the important step. My testing shows that it fixes the problem.
  2. This is optional code clean-up, and it brings the 3.x branch a little closer to the 3.1.x branch. It is in a separate commit, so I can easily remove it if you like. Or I am happy to make the same change for other classes.
  3. This is also optional clean-up in a separate commit. I can remove it, update other classes that use the logger factory, and/or make a similar change on the 3.1.x branch.
clairero’s picture

Before the patch: I followed the steps to reproduce and got the same issue.

  1. Installed Trash 3.0.29 and enabled it for some content types.
  2. Ran composer require drupal/trash:3.0.35.
  3. Ran drush updatedb --no-cache-clear:
  4. [success] No pending updates.

  5. Visited the Trash settings page. One difference from the issue: every page on the site failed, not only the settings page:
  6. ArgumentCountError: Too few arguments to function Drupal\trash\TrashEntityPurger::__construct(), 6 passed ... and exactly 7 expected

  7. Ran drush cset trash.settings auto_purge.enabled 1 -y:
  8. TypeError: Drupal\trash\EventSubscriber\TrashConfigSubscriber::__construct(): Argument #6 ($loggerFactory) must be of type Drupal\Core\Logger\LoggerChannelFactoryInterface, Drupal\Core\Action\ActionManager given

After the patch: drush updatedb --no-cache-clear now finds the new post-update:

drush updatedb --no-cache-clear
 -------- ------------------- ------------- ------------------------------------------------------------------
  Module   Update ID           Type          Description
 -------- ------------------- ------------- ------------------------------------------------------------------
  trash    rebuild_container   post-update   Rebuild the container for changed service constructor arguments.
 -------- ------------------- ------------- ------------------------------------------------------------------


 ┌ Do you wish to run the specified pending updates? ───────────┐
 │ Yes                                                          │
 └──────────────────────────────────────────────────────────────┘

>  [notice] Update started: trash_post_update_rebuild_container
>  [notice] Update completed: trash_post_update_rebuild_container

Running it rebuilds the service container, so the stale constructor arguments from the old container are gone. After the update:

The site loads normally and the Trash settings page works.
drush cset trash.settings auto_purge.enabled 1 -y saves without errors.

Removing the explicit arguments from trash.services.yml and injecting a logger channel instead of the logger factory also looks good. It follows Drupal conventions and should make this kind of mismatch less likely in the future.

Marking as RTBC.

clairero’s picture

Status: Needs review » Reviewed & tested by the community

amateescu made their first commit to this issue’s fork.

  • amateescu committed 9de76457 on 3.1.x
    fix: #3632459 Cached container causes errors after TrashConfigSubscriber...
amateescu’s picture

Version: 3.x-dev » 3.1.x-dev
Status: Reviewed & tested by the community » Fixed

Looks great! Merged the 3.x MR and ported it to 3.1.x as well, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.