Not sure if others have this issue, but thought I'd paste here to help. We had 2.5 million of these sitting in the database.

What causes this is if the node which owns the token is removed, then the cron for the token module for some reason creates a new token for it, forever and ever. So we had literally millions of unexpired tokens in the database going back years. Not something I want to spend too much time on, but I've quickly modified the cron function to the below. I've set the limit to 100 so as not to kill the cron.

/**
 * Implements hook_cron().
 */
function review_token_cron() {
  $n_expired = 0;
  $removed_expired = [];

  $expired = db_select('node_preview_tokens', 't')
    ->fields('t')
    ->where('expires < UNIX_TIMESTAMP(NOW())')
    ->range(0, 100)
    ->execute()->fetchAll();

  foreach ($expired as $e) {
    $node = node_load($e->nid);
    if ($node) {
      $token = new PreviewToken($node);
      $token->expireToken();
    }
    else {
      $removed_expired[] = $e->token;
    }
    $n_expired++;
  }

  if (count($removed_expired) > 0) {
    $deleted = db_delete('node_preview_tokens')
      ->condition('token', $removed_expired, 'IN')
      ->where('expires < UNIX_TIMESTAMP(NOW())')
      ->execute();

    watchdog('Content Publish Review', 'Cron: %n_expired %pl_tokens orphan expired.',
      array(
        '%n_expired' => $deleted,
        '%pl_tokens' => format_plural($deleted, 'token', 'tokens'),
      ),
      WATCHDOG_NOTICE
    );
  }

  if ($n_expired) {
    watchdog('Content Publish Review', 'Cron: %n_expired %pl_tokens expired.',
      array(
        '%n_expired' => $n_expired,
        '%pl_tokens' => format_plural($n_expired, 'token', 'tokens'),
      ),
      WATCHDOG_NOTICE
    );
  }
}
CommentFileSizeAuthor
#4 2886342-token-node-disappear-4.patch3.22 KBdpi

Comments

camerongreen created an issue. See original summary.

camerongreen’s picture

Issue summary: View changes
camerongreen’s picture

Issue summary: View changes
dpi’s picture

Priority: Normal » Major
Status: Active » Needs review
StatusFileSize
new3.22 KB

Slightly different route:

  • Added exception if node passed to PreviewToken is invalid.
  • Fixed watchdog log type and severity.
  • Added token expiration on node delete.
  • Added extra inline comments.
  • Added PreviewToken::cleanUp which searches for tokens with non existent nodes. This will automatically do a bulk cleanup of all the bogus nid=0 tokens accumulated.

Bumping to major since this is creating an extreme amount of tokens. 2.5million in the case of the original report, 500k in my case.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me

camerongreen’s picture

Given this an other issues with this module we are probably just going to replace it with something like https://www.drupal.org/project/tca

  • jibran committed 46c0a89 on 7.x-1.x authored by dpi
    Issue #2886342 by dpi: Tokens not expiring
    
jibran’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed and pushed to 7.x-1.x. I have also created a new release.

dpi’s picture

Thankyou Jibran-san

Status: Fixed » Closed (fixed)

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