Problem/Motivation

ContentEntityStorageBase::loadUnchanged() should return an entity's persisted state, discarding unsaved in-memory changes. But it only calls parent::resetCache(), which clears the entity static cache and not the static revision cache.

When a module returns a non-default revision from hook_entity_preload() (as Workspaces does), the reload goes through loadMultipleRevisions() -> getFromStaticRevisionCache(), which still holds the same object that might have been changed in the current request.

Steps to reproduce

  1. Create an entity (default revision).
  2. Create a second, non-default (pending) revision.
  3. Make that pending revision the result of hook_entity_preload() for the entity ID.
  4. load the entity (gets the pending revision), then modify a field in memory without saving.
  5. Call loadUnchanged() on the same ID.

Proposed resolution

In ContentEntityStorageBase::loadUnchanged(), after parent::resetCache($ids), also invalidate the static revision cache for statically-cacheable revisionable entity types (via the {type}:{id}:revisions memory cache tags). This forces the subsequent preload/reload to fetch fresh revisions from the persistent cache or storage, without deleting the persistent cache that loadUnchanged()'s fast path relies on.

Remaining tasks

Review.

User interface changes

Nope.

Introduced terminology

N/A

API changes

Nope.

Data model changes

Nope.

Release notes snippet

N/A

Issue fork drupal-3594092

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

amateescu created an issue. See original summary.

amateescu’s picture

Status: Active » Needs review
godotislate’s picture

Test only job fails as expected: https://git.drupalcode.org/project/drupal/-/jobs/10156723

1 small comment on the MR. OK to self-RTBC after the change.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Applied the suggestion and sheepishly RTBC-ing :)

  • catch committed bf994712 on 11.4.x
    fix: #3594092 loadUnchanged() returns an in-memory-modified entity when...

  • catch committed 330c0dde on 11.x
    fix: #3594092 loadUnchanged() returns an in-memory-modified entity when...

  • catch committed aad45020 on main
    fix: #3594092 loadUnchanged() returns an in-memory-modified entity when...
catch’s picture

Version: main » 11.4.x-dev
Status: Reviewed & tested by the community » Fixed

This makes sense and revision caching is pretty new so let's get this into 11.4.x before anyone else runs into it.

Committed/pushed to main, 11.x and 11.4.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.