Call to a member function getTranslation() on null in Drupal\\Core\\Entity\\ContentEntityBase->hasTranslationChanges()

Created on 21 June 2021, over 3 years ago
Updated 2 February 2023, almost 2 years ago

Problem/Motivation

We are seeing a significant number of the following errors in our logs:
Error: Call to a member function getTranslation() on null in Drupal\\Core\\Entity\\ContentEntityBase->hasTranslationChanges()

Steps to reproduce

A contrib module is calling hasTranslationChanges on an entity (method inherited from ContentEntityBase).
By the time the method attempts to load the unchanged entity (L:1415 of ContentEntityBase) the entity has been deleted by a process running in another thread, meaning that $original === NULL. ( The race condition makes this quite challenging to reliably reproduce.)
Therefore, the call on L:1431 as follows $translation = $original->getTranslation($this->activeLangcode); results in an error.

Proposed resolution

Check if the unchanged entity could be loaded. If not, return FALSE from hasTranslationChanges. The logic being that the entity cannot have changes if it has been deleted. Normally actions (such as updated the changed timestamp on the entity) are taken as a result of hasTranslationChanges returning TRUE. Therefore, it is hoped that this change does not introduce unintended side-effects.

Remaining tasks

Create a merge request.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

To be defined.

πŸ› Bug report
Status

Needs work

Version

9.5

Component
EntityΒ  β†’

Last updated about 13 hours ago

Created by

πŸ‡¨πŸ‡­Switzerland tcrawford

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    Think we should investigate what is calling this function to trigger this issue. Fix the cause vs sympton

    Either way it will need a test case.

    Did not test for issue.

Production build 0.71.5 2024