Problem/Motivation

\Drupal\Core\Theme\Icon\IconCollector::resolveCacheMiss() persists the result of each lookup. This includes a failed lookup (NULL).

A lookup can fail for a temporary reason. For example, SvgExtractor::loadIcon() returns NULL when IconFinder::getFileContents() does not find the SVG file. This occurs when a request asks for an icon during a deployment, before all icon files are in place.

The collector writes the NULL to the icon_info cache item in cache.default. After that, IconCollector::get() returns the stored NULL and does not call the extractor again. The icon stays unavailable when the file is back. Only a cache rebuild, or an invalidation of the icon_pack_collector cache tag, restores the icon.

The result is not consistent: IconPackManager::getIcons() lists the icon, but IconPackManager::getIcon() returns NULL for the same ID.

We found this on a site after a deployment. The icon picker of the UI Icons module showed a loading placeholder for most icons on its first page. All icon files were available at that time. The placeholders stayed until a cache rebuild.

The documentation of CacheCollector says that the class stores NULL for keys that do not exist, to prevent a rebuild on each request. That reason does not apply to IconCollector. A miss does not start a rebuild. A miss costs some array lookups in the cached pack definitions.

Steps to reproduce

  1. Install a module or theme with an icon pack that uses the svg extractor and local files. The steps below use the icon ID my_pack:my_icon.
  2. Rebuild the cache: drush cr
  3. Build the icon pack definitions while the file exists: drush php:eval "\Drupal::service('plugin.manager.icon_pack')->getDefinitions();"
  4. Move the SVG file of my_icon out of the icon pack directory.
  5. Request the icon: drush php:eval "var_dump(\Drupal::service('plugin.manager.icon_pack')->getIcon('my_pack:my_icon'));" The result is NULL, which is correct.
  6. Move the SVG file back.
  7. Do step 5 again.

Expected result: step 7 returns an IconDefinition object.

Actual result: step 7 returns NULL. The icon is available again only after drush cr.

Proposed resolution

Persist only an icon that was found. Keep a miss in $this->storage for the current request, so that one request does not do the same lookup again.

Remaining tasks

  • Open a merge request with the change.
  • Add test coverage: a failed lookup is not persisted, and the next request does the lookup again.
  • Review.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

Not applicable.

Issue fork drupal-3641582

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

klaasvw created an issue. See original summary.

klaasvw’s picture

Status: Active » Needs review