Problem/Motivation

Follow-up to #2219689: Separate out logic for getPluginTypes/viewsHandlerTypes from ViewExecutable
The static class keeps reference to string translation service so the object never free

Proposed resolution

- do not cache string translation service reference in static property
- use \Drupal\Core\StringTranslation\TranslatableMarkup

Remaining tasks

- agree on implementation
- patrch/commit

User interface changes

no

API changes

no

Data model changes

no

Release notes snippet

no

CommentFileSizeAuthor
#2 3150721-2.patch1.05 KBandypost

Issue fork drupal-3150721

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

andypost created an issue. See original summary.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new1.05 KB

Suggested changes

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Triggering for 10.1 tests

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Same as the one I just RTBC tests are green and seem like a good refactoring.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

If we want to do this then I think we should deprecate \Drupal\views\Views::t() and replace all the calls to it with new TranslatableMarkup() since this is what we tell people to use in static methods.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

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

ptmkenny’s picture

This issue has stalled, but I would like to work on it because the static translation caching breaks badly when using FrankenPHP in worker mode (because Drupal is only bootstrapped once for many requests, the cache goes stale after the first request).

I propose expanding the scope of the issue and including the follow-up from @alexpott in #10. I'm including a new IS below; if this is too big of a scope change, just let me know and I'll remove this comment and create a new issue instead.

Problem/Motivation

Views::t() stores the string_translation service in Views::$translationManager. Views::getHandlerTypes() stores its labels in Views::$handlerTypes. This static caching is fine for the way Drupal has traditionally been deployed, but it doesn't work for persistent app servers like FrankenPHP.

Each label references the translation manager. TranslatableMarkup::render() stores the translated text. So, resetting $translationManager does not refresh the labels.

How does this break? A persistent worker serves many requests in one process, so later requests get their labels in the first request's language. In addition, the old service and its container are kept in memory, wasting space.

Steps to reproduce

  1. Set string_translation to a translation manager with English as the default language.
  2. Call Views::getHandlerTypes() and render ['field']['title'].
  3. Replace string_translation with a Japanese translation manager, in the same container or a new one.
  4. Call Views::getHandlerTypes() again. ['field']['title'] still renders the English text.

Proposed resolution

Return a new array of new TranslatableMarkup objects on each call. Labels are translated when rendered.

Deprecate Views::t() and the two unused properties.

AI disclosure: I used AI to help me prepare the new IS and also write the MR (Opus 5.5 and Astra).

ptmkenny’s picture

Status: Needs work » Needs review

Ok, I proposed expanding the scope of this MR in #14 as well as added an MR and a change record, so I'm tentatively setting this to "Needs Review."

I did not measure the performance impact of removing the static caching because I don't know how to do that.

nicxvan’s picture

If we do still need caching we can use a memory cache then you can reset it as needed.