Problem/Motivation

Example: Create a message on node_entity_update() hook that uses some node tokens. Have some modules in the chain that rely on entity wrappers which always load the statically cached node content.

Expected: Correct token replacement with updated node
Experienced: Tokens are replaced with old (un-updated) content.

This stems from the fact (see original report by fago below) that hooks are called before the static entity cache is made consistent. (Note: I remember some while ago the commerce entity controller had the same problem and was fixed the same way.)

Current node_save():

// Let modules modify the node before it is saved to the database.
module_invoke_all('node_presave', $node);
module_invoke_all('entity_presave', $node, 'node');

... // DO the actual save to DB.

module_invoke_all('node_' . $op, $node);
module_invoke_all('entity_' . $op, $node, 'node');
...
// Clear the static loading cache.
entity_get_controller('node')->resetCache(array($node->nid));

Should changed to

// Let modules modify the node before it is saved to the database.
module_invoke_all('node_presave', $node);
module_invoke_all('entity_presave', $node, 'node');

... // DO the actual save to DB.

// Clear the static cache so hooks see a consistent cache.
entity_get_controller('node')->resetCache(array($node->nid));

module_invoke_all('node_' . $op, $node);
module_invoke_all('entity_' . $op, $node, 'node');
...

Proposed resolution

Clear the static entity cache after db write, before invoking hooks.
Write a patch that does this in

[node|user|file|taxonomy_term|taxonomy_vocabulary]_save

(Checked by grepping "entity_get_controller\('.*'\)->resetCache")
(Update: taxonomy_term_save() and taxonomy_vocabulary_save() already do show the proposed behavior and need not be fixed.)

Currently do not address entity_delete as
* this is done with multiple entities at once in e.g. node_delete_multiple() and would require some more thought
* inconsistent cache in hook_entity_delete() (some already deleted entities in the static cache) should be a minor problem compared to hook_entity_update() (*incorrect* entities in the static cache).

Remaining tasks

TBD

User interface changes

None.

API changes

None. (Modules that rely on inconsistent entity cache are broken in the first place.)

Data model changes

None.

Original by fago

Follow-up from #983090: Reset cache after save:

Currently we clear static caches at the end of an save or delete operation, however this is troublesome as the static cache is not yet up2date for any invoked hooks (update|delete). E.g. this cause a bug in profile2, where I had to clear the menu-cache (-> menu_rebuild()) in those hook, however the rebuild happens with the old-cached information, thus it's not effective.

Thus, to avoid any issue we should fix our static caches as soon as the basic db operation (save|delete) is performed and *before* any hook is invoked.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

The example/problem description is a bit blurry. No idea why you need to rebuild menus within an entity save operation. Could you elaborate a bit more on the concrete problem, please?

fago’s picture

In that specific case, the profile_type entity (which is the bundle-entity of the profile-entity) controls whether there is a certain menu path for accessing profiles, e.g. via /profile-foo/uid. Thus hook_menu() items are built with information stored in entities, however due to the static cache containing obsolete information during those hooks - the items are rebuilt wrong.

I think this is a rather specific case, however it has demonstrated that as of now we have in-accurate information in the static-caches during update/delete hooks which *might* lead to problems in some special situations. That problems could not only occur with menu rebuilds, but in particular there it might happen due to the way the menu cache is cleared/rebuilt.

geek-merlin’s picture

Title: ensure static caches are up2date » Inconsistent static entity cache in hook_entity_[insert|update|delete]
Issue summary: View changes

This is still a problem. Update summary.

geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Status: Needs review » Needs work

The last submitted patch, 8: drupal-986024-8-by-Inconsistent-static-cache.patch, failed testing.