Batch update hangs up if source entity ID is 0

Created on 4 August 2024, 5 months ago
Updated 7 September 2024, 4 months ago

Problem/Motivation

The 8.x-2.0-beta13 release contained a fix for the batch process crashing when source entity type had no existing entities: #3444332 🐛 batch process crashes if an entity type has no entities Fixed

The fix is preventing to try to load the entity if the value of $entity_id is FALSE, using an empty() check instead of the previous isset().

However, the empty() check is also preventing loading the entity if the entity ID is 0 or '0'. In case of user entity type, zero entity ID is possible. In this case, when the batch process reaches this point, the process hangs up (enters into an infinite loop).

Steps to reproduce

Tested on Drupal 10.3.1
1. Enable user as source entity type on /admin/config/entity-usage/settings
2. Run the entity usage batch update on /admin/config/entity-usage/batch-update
3. The batch process hangs up on 'Updating entity usage for user...' and never finishes.

Proposed resolution

The condition in EntityUsageBatchManager::updateSourcesBatchWorker() should allow numeric values even if they are falsy (should allow 0 or '0').

1. A possible option is to check if the ID is numeric:

if ((is_numeric($entity_id) || !empty($entity_id)) && $entity = $entity_storage->load($entity_id)) {
  ...
}

2. Another solution would be to only check if $entity_id is not FALSE. The variable is set above the condition as:

    $entity_ids = $entity_storage->getQuery()
      ->condition($entity_type_key, $context['sandbox']['current_id'], $op)
      ->range(0, 1)
      ->accessCheck(FALSE)
      ->sort($entity_type_key)
      ->execute();
    $entity_id = reset($entity_ids);

The query result will either be an array of IDs or an empty array. So reset($entity_ids) will either result in an ID or FALSE. So this should also work:

if ($entity_id !== FALSE && $entity = $entity_storage->load($entity_id)) {
  ...
}

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇧🇪Belgium keszthelyi Brussels

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024