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

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

Comments

keszthelyi created an issue. See original summary.

keszthelyi’s picture

  • marcoscano committed 9b4b30bb on 8.x-2.x
    Issue #3465800 by keszthelyi, marcoscano: Batch update hangs up if...
marcoscano’s picture

Version: 8.x-2.0-beta13 » 8.x-2.x-dev
Status: Active » Fixed
Issue tags: -batch process

Fixed using the second approach. Thanks! 👍

Status: Fixed » Closed (fixed)

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