Problem/Motivation

Consider the use case of processing some information in hook_entity_insert and in hook_entity_storage_load loading this information into the entity. We even have example in core for this - comment_entity_insert is creating a new entry into the comment statistics table and comment_entity_storage_load is loading this information into the entity's comment field.

So far so fine ... until there is some contrib or even custom module, which for some reason is loading the very same entity being saved from an inside of its comment_entity_insert hook. This will cause the load of an entity before all the processing of its previous save has been done. So if an information being prepared in the insert hook is being retrieved and set on the entity in the load hook, then the loaded entity will miss this information, as it is not yet available. This is something we cannot prevent. But a problem is caused at this point- every further entity load will be still retrieving that "incomplete" entity even if the prepared information is already written to the DB. This will happen, because we will be retrieving the entity from the entity storage's persistent cache, where it has been added to while being loaded inside of its hook_entity_insert.

Proposed resolution

Option 1
Reset the persistent cache for an entity at the end of the storage's ::postSave() method. Currently the persistent cache is being reset only at the beging of the ::postSave() method.

Option 2
If loading an entity inside its post save hooks, then serve a reference to that very same entity being currently saved. For this we have to keep track of the entity being currently saved in the storage. When loading an entity by its ID / revision ID through ::load() / ::loadReivision() then check if an entity is being currently saved and if so check if it matches the entity ID / revision ID of the requested entity. If it matches then skip all the loading process and just return a reference to the entity being currently saved.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

hchonov created an issue. See original summary.

hchonov’s picture

In my opinion the more natural option is "Option 2 If loading an entity inside its post save hooks, then serve a reference to that very same entity being currently saved".

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

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.

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.

smustgrave’s picture

Issue tags: +Bug Smash Initiative

This came up as a daily BSI target looking at the entity system

Will admit not 100% how to verify this one but question do we know this is still an issue 8 years later? Can go from there.

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.