While using background batch, if an exception occurs within an operation then the batch process continues rather than showing an error message. This might go into an infinite loop depending on the point at which the exception occurs and if the client is still sending those ping requests.

Sample to reproduce the issue:

Consider the following snippet for a batch operation.

/**
 * Batch operation to print Fibonacci sequence
 */
function sample_batch_operation(&$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['max'] = SAMPLE_BATCH_MAX;
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['prev_value'] = 0;
    $context['sandbox']['current_value'] = 1;
  }
  
  $counter = 0;
  $limit = SAMPLE_BATCH_ITERATION_LIMIT;
  $value = 0;
  $sandbox = &$context['sandbox'];
  while ($counter < $limit && $sandbox['progress'] < $sandbox['max']) {
    $value = $sandbox['prev_value'] + $sandbox['current_value'];
    watchdog(__FUNCTION__, $value);

    if($value == 144) {
      throw new Exception;
    }
    
    $sandbox['prev_value'] = $sandbox['current_value'];
    $sandbox['current_value'] = $value;
    $sandbox['progress']++;
    $counter++;
  }

  $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}

In case of a regular batch operation, the results would have terminated as soon as an exception/error occurred.

Proposed solution:

After a background process is removed, a new background process is initiated again if the client sends a ping.

function _background_batch_page_do_js() {
.
.
  else {
    // Not running ... and stale?
    _background_batch_initiate();
  }
}

Instead the following can be done

function _background_batch_page_do_js() {
.
.
  else {
    // Not running ... and stale?
    // This could be an indication that the operation has been interrupted/failed.
    drupal_json_output(array('status' => FALSE));
    drupal_exit();
  }
}

Attached a patch with above changes.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ajohnpraveen’s picture

Issue summary: View changes
gielfeldt’s picture

Hmm. I can see that. But actually, this was a "feature" that was supposed to pick up a batch job where it left, in case the background process request never reached the webserver. I have to rethink this I guess.