Services are not destroyed in script mode

Created on 6 September 2018, about 6 years ago
Updated 3 July 2024, 2 months ago

The feature description that services can finalize their operation in a destruct() method (via implementing DestructableInterface and tagging the service with needs_destruction) suggests to me that destruction will happen at the end of each process in which such a service is instantiated.

But this is only true for web requests and not in script mode, i.e. destruction will not happen if code is executed in drupal context with "drush scr script.php", for example.

This may have serious consequences up to data loss, depending on what happens in the destruct() method.

One example where a feature is broken by failure to destroy the service can be found in search API:
The "index immediately" option of a search API index does not work due to this in cases where the creation/update/deletion of entities is done on the command line. This is not obvious in some use cases as the indexing will happen anyway with the next cron run(s). But in a use case that mandates immediate indexing the problem will show.

I asked myself whether this may rather be a bug in drush(*) but then again there may be a way to make this service destruction work "no matter what".

(*): I opened an issue there, too: https://github.com/drush-ops/drush/issues/3672

πŸ› Bug report
Status

Closed: works as designed

Version

8.7 ⚰️

Component
BaseΒ  β†’

Last updated less than a minute ago

Created by

πŸ‡©πŸ‡ͺGermany cspitzlay πŸ‡©πŸ‡ͺπŸ‡ͺπŸ‡Ί

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom Rob230

    Not fixed in Drush - as of Drush 12.4 it is still a problem.

    For us it caused major data loss because event subscribers are not able to save the entity being acted on, so we have to do it in the destruct call, which doesn't fire from CLI.

    Now that I know that's happening, my workaround is to load the EventSubscriber myself (via \Drupal::service()) and then manually call the ->destruct() method on it.

  • πŸ‡©πŸ‡ͺGermany cspitzlay πŸ‡©πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Well, at least it used to be fixed. I had successfully tested this 6 years ago.
    See https://github.com/drush-ops/drush/issues/3672#issuecomment-420950688

    Could be a new issue. You may want to report it against drush along with your steps to reproduce it.

Production build 0.71.5 2024