Problem/Motivation

On trying to use any kind of node permissions module (the main case: forum_access), when I went to rebuild node permissions from the accepted ~/admin/content/node-settings/rebuild the status bar would complete to 100% and I would get the following error message:

The content access permissions could not be rebuilt.
The content access permissions need to be rebuilt.

That was on two of three sites in a multisite installation. The third would get to 97% and stall.

The only solution, after much consultation with Marco at Advomatic, was to put this in a node and click "Preview":

<?php
set_time_limit(0);
db_query("DELETE FROM {node_access}");
node_access_rebuild();
?>

That makes the error go away. Marco suspects an issue with the batch function.

Steps to reproduce

Proposed resolution

Log an error message when the rebuild fails

Remaining tasks

Convert to MR
Review

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

damienmckenna’s picture

Title: Batch failing on node permissions update » Rebuild Permissions process not completing correctly
Version: 6.12 » 6.14
Status: Active » Needs review
StatusFileSize
new1.17 KB

I ran into this using og_access.module on a site migrated from D5 that has ~90,000 nodes. For completeness sake, here are the steps I've performed so far:

  • Disable all contrib modules.
  • Backed up the database.
  • Switch to D6 branch in working copy.
  • Move all modules out of sites/all/modules.
  • Update.php - 62 updates.
  • Backed up the database.
  • Moved back cck & enable all modules. Ran all updates.
  • Moved back filefield & enabled it. No applicable updates.
  • Moved back imagefield & enabled it. Ran all updates.
  • Moved back link & enabled it. Ran all updates.
  • Moved back date & enabled it. Ran all updates. Had to run updates again to catch them all.
  • Moved back token & enabled it. No applicable updates.
  • Moved back imageapi & enabled it. No applicable updates.
  • Moved back imagecache & enabled it. Ran all updates.
  • Moved back javascript_aggregator & enabled it. Ran all updates.
  • Moved back og & enabled it. Ran all updates.
  • Rebuilt permissions. After completing it said "The content access permissions have not been properly rebuilt.".
  • Disabled OG. Rebuilt permissions.
  • Enabled OG. Rebuilt permissions

Each time I perform the Rebuild Permissions step it fails with the above error. I did a dump of the corresponding {batch} table record when it got to the 'finished' callback and it said the following:

Array
(
    [sets] => Array
        (
            [0] => Array
                (
                    [sandbox] => Array
                        (
                            [progress] => 96600
                            [current_node] => 4605
                            [max] => 96553
                        )
                    [results] => Array
                        (
                        )
                    [success] => 
                    [title] => Rebuilding content access permissions
                    [operations] => Array
                        (
                            [0] => Array
                                (
                                    [0] => _node_access_rebuild_batch_operation
                                    [1] => Array
                                        (
                                        )
                                )
                        )
                    [finished] => _node_access_rebuild_batch_finished
                    [init_message] => Initializing.
                    [progress_message] => Remaining @remaining of @total.
                    [error_message] => An error has occurred.
                    [total] => 1
                )
        )
// and more with the form details..

Tracing through the code I found that _node_access_rebuild_batch_operation() was doing all of the work of managing the batch process, which contains the following:

  $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit);
  while ($row = db_fetch_array($result)) {
    $loaded_node = node_load($row['nid'], NULL, TRUE);
    // To preserve database integrity, only aquire grants if the node
    // loads successfully.
    if (!empty($loaded_node)) {
      node_access_acquire_grants($loaded_node);
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $loaded_node->nid;
  }

The part that seemed confusing to me was the following line:

    $context['sandbox']['current_node'] = $loaded_node->nid;

I figured that there could be circumstances when the $loaded_node would fail on the last node load of a particular loop thus storing an empty value in $current_node for the next loop. On the next loop the database abstraction layer takes this NULL value and the query above, performs a type casting on the NULL to make it an integer, resulting in the following query:

SELECT nid FROM {node} WHERE nid > 0 ORDER BY nid ASC

This could then cause the whole batch process to start over.

In my case it always ran to completion and returned the error above.

What I did to fix it was changed the current_node assignment to just use the nid value from the row itself, i.e.:

    $context['sandbox']['current_node'] = $row['nid'];

That allowed it to successfully complete the task.

liam mcdermott’s picture

subscribing.

janusman’s picture

subscribing

janusman’s picture

This seems quite logical. If we are setting ['progress'] to be some property of an invalid (empty?) object, something is clearly amiss =)

In my case, it fixed a recurring problem in a site with 30k nodes which has both Workflow Access and OG Access modules enabled. (Before, after finishing the rebuild it still informed me that they should be rebuilt).

Just a change: patch contains some cruft:

diff -u -p -r1.36 cron.php
--- cron.php	9 Aug 2006 07:42:55 -0000	1.36
+++ cron.php	8 Nov 2009 20:48:48 -0000
@@ -8,4 +8,6 @@
 include_once './includes/bootstrap.inc';
 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
-drupal_cron_run();
+// drupal_cron_run();
+print("<pre>");
+print_r(node_get_types());

I think the above should be removed. After that I think it's ready to go.

BTW I think this might also be a problem in D7? Here's a snippet from D7's _node_access_rebuild_batch_operation():

  // Process the next 20 nodes.
  $limit = 20;
  $nids = db_query_range("SELECT nid FROM {node} WHERE nid > :nid ORDER BY nid ASC", 0, $limit, array(':nid' => $context['sandbox']['current_node']))->fetchCol();
  $nodes = node_load_multiple($nids, array(), TRUE);
  foreach ($nodes as $node) {
    // To preserve database integrity, only acquire grants if the node
    // loads successfully.
    if (!empty($node)) {
      node_access_acquire_grants($node);
    }
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $node->nid;
  }

If so we should attract some attention to this =)

chx’s picture

I am very close to mark this as wont fix. How can the nid of a loaded node be 0 / NULL? You are fixing a symptom not the cause.

fizk’s picture

Same problem here. For example, in node_load():

SELECT * FROM node n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN node_revisions r ON r.vid = n.vid WHERE n.nid=817

returns empty, but

SELECT * FROM node n INNER JOIN node_revisions r ON r.vid = n.vid WHERE n.nid=817

returns one row. These rows were of type 'uprofile', so it seems the advanced_profile_kit module didn't delete the profile node when the user account was deleted.

Please add the patch for the next D6 release.

janusman’s picture

@chx: re #6, is it possible that a node_delete could occur while a batch is processing, therefore causing node_load($nid) to not return a valid object? Are there any other possible cases? (My guess is YES, due to bad modules, database table corruption, cosmic rays, etc... of course, unlikely ... but possible.)

IMO if this patch makes Drupal more bulletproof... even if just by a tiny bit [EDIT: I missed saying "it's worth implementing" here] =)

And, OK, maybe it's not a "bug" as such. =)

salvis’s picture

You might try commenting out the

node_access_acquire_grants($loaded_node);

line. That should allow the batch process to run much faster and still attempt to load each node. Does that run to completion?

WARNING:
Running the rebuild after commenting out that line only fakes rebuilding permissions —
if it 'succeeds,' then Drupal will stop asking to rebuild permissions, but you haven't actually done it!

This is intended only as an intermediate step in finding the cause of the problem, by checking whether you can at least load all nodes. It will probably result in all nodes becoming inaccessible to all but administrative users!

webchick’s picture

Version: 6.14 » 7.x-dev
Status: Needs review » Needs work

I agree with chx that this is only making the problem. The real problem is that node_load() failed, and thus the node's access grants rebuild was not executed, so it gets flagged as a failure.

But that said, it's all but impossible to figure out what the root cause of the error was when such a thing occurs. I encountered the issue "somewhere" in my 60K nodes website, and only managed to track down the problem thanks to this issue providing a hint, and then tossing in some debug code (see #281326-51: The content access permissions need to be rebuilt.).

At the very least it seems like we ought to fire a dsm() and/or a watchdog when this condition happens:

    if (!empty($node)) {
      node_access_acquire_grants($node);
    }
    else {
      // OMG. PEOPLE ARE DYING.
    }

Since the issue exists in D7 too, bumping to 7.x-dev so we can fix it there first.

webchick’s picture

Title: Rebuild Permissions process not completing correctly » Trigger watchdog when node rebuild permissions fails

Turns out that technically, this is a duplicate of #315302: Node Access Rebuild never finishes (infinite loop) since that one is older. It's also been marked RTBC, pending tests. So I'm going to hijack this one and make it about error trapping instead. ;)

cyberswat’s picture

Just an FYI that this is similar to #225864: Don't assume that node_load returns a node ... I'm wondering if node_load itself should handle the watchdog entries to mitigate other possible occurrences. This won't stop the error messages but would at least give admins the ability to debug their problematic data.

janusman’s picture

Status: Needs work » Needs review
StatusFileSize
new598 bytes

This patch logs to watchdog inside node_access_rebuild().

Status: Needs review » Needs work

The last submitted patch, 471080-12-watchdog_in_node_access_rebuild.patch, failed testing.

cyberswat’s picture

StatusFileSize
new864 bytes

I think the root problem is bad data being passed to node_load and that issues like this one are a symptom of the cause. I don't believe Drupal should have to account for bad data that has been entered but I do believe that we should at least notify admins of the root problem. I'm of the opinion that the only place this can happen is in node_load() ... otherwise you would have to write a patch to fix every instance of a module that uses node_load and assumes the node is proper.

For the sake of argument here's a quick patch that modifies node_load()

cyberswat’s picture

Status: Needs work » Needs review
StatusFileSize
new864 bytes

Here's one that corrects the typo in my last patch

Status: Needs review » Needs work

The last submitted patch, drupal-471080-16.patch, failed testing.

janusman’s picture

Status: Needs work » Needs review
StatusFileSize
new605 bytes

Gah! Bad format in #13; sorry. retesting.

cyberswat’s picture

I re-rolled the patch I was working on it's original issue #225864: Don't assume that node_load returns a node

janusman’s picture

tomhung’s picture

#1: drupal-n471080.patch queued for re-testing.

brianV’s picture

Status: Needs review » Needs work

The last submitted patch, 471080-12-watchdog_in_node_access_rebuild.patch, failed testing.

damienmckenna’s picture

Version: 7.x-dev » 8.x-dev
Component: base system » node system
Issue summary: View changes
Related issues: +#315302: Node Access Rebuild never finishes (infinite loop)

Hello again, dear bug =)

Updating this to the node system and it needs to be rerolled for 8.0.

damienmckenna’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Version: 8.9.x-dev » 9.2.x-dev
Issue tags: +Bug Smash Initiative, +Needs issue summary update
catch’s picture

Issue tags: +Novice

Still looks valid in node_access_rebuild(), adding a log message seems OK.

guilhermevp’s picture

Status: Needs work » Needs review
StatusFileSize
new599 bytes
new625 bytes

Not sure if this is the expected message, but please review, also is it applicable for 9.3.x?

guilhermevp’s picture

Sorry, patch 37 was sent by accident. Please dot not consider, patch #36 is the one to be reviewd.

guilhermevp’s picture

StatusFileSize
new599 bytes

Sending patch without whitespace.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rudrakumar188’s picture

StatusFileSize
new2.15 MB

The patch #38 is applied successfully on version 9.4.x . Added a screenshot of patch.

gaurav-mathur’s picture

Assigned: Unassigned » gaurav-mathur
gaurav-mathur’s picture

Assigned: gaurav-mathur » Unassigned
gaurav-mathur’s picture

Applied patch #38 successfully on drupal version 9.4.x
Thank you.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

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.

Did not review or test

Moving to NW for the issue summary update requested in #34

catch’s picture

Category: Bug report » Task

This is a task now that #315302: Node Access Rebuild never finishes (infinite loop) is dealing with the actual bug.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update
acbramley’s picture

Status: Needs work » Postponed (maintainer needs more info)

This issue was originally created because when the node wasn't loaded, the batch failed and the user was presented with "could not be rebuilt" and "need to be rebuilt" messages simultaneously.

Now that #315302: Node Access Rebuild never finishes (infinite loop) fixed the bug when a node couldn't be loaded, I'm not really sure what relevant information we could provide in this log message and why it would be the job of this batch process to call out a faulty node? A node has to exist in the database and then produce an error on load to even hit this issue (since $nids come from an entity query).

What information could we actually provide here that would help a user in any way? They'd still see a successful batch operation afaict.

smustgrave’s picture

@acbramley where would that info popup though? Way I read this one was just adding a logger to the batch

acbramley’s picture

@acbramley where would that info popup though?

My point in #50 is that there's not really any helpful info to add anyway. I'm not sure what you're referring to about an info popup.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

quietone’s picture

Issue tags: -Novice

I am removing the 'Novice' tag because what task someone new to Drupal contribution is not clearly defined. Converting to an MR would be but the subsystem maintainer is questioning the need for this change.