Sometimes it turns out that CRON in drupal requires a LOT of memory, often the problem comes up as a memory exhausted message in menu.inc

This turns out to be because the menu_rebuild is called many times when its actually is only needed to be called once.

This is my quick-fix and i know hacking drupal core isn't the way to go

--- includes/menu.inc.old 2015-02-11 11:01:12.411387934 +0000
+++ includes/menu.inc 2015-02-11 11:10:16.204995356 +0000
@@ -2717,6 +2717,16 @@
return FALSE;
}

+// ****************************
+// Added by arne@hortell.se
+// Found it in admin menus
+
+ $was_flushed = &drupal_static(__FUNCTION__, array());
+ if (isset($was_flushed[$uid])) {
+ return FALSE;
+ }
+// *****************************
+
$transaction = db_transaction();

try {
@@ -2727,6 +2737,11 @@
menu_cache_clear_all();
_menu_clear_page_cache();

+// ****************************
+// Added by arne@hortell.se
+ $was_flushed[$uid] = TRUE;
+// *****************************
+
if (defined('MAINTENANCE_MODE')) {
variable_set('menu_rebuild_needed', TRUE);
}

CommentFileSizeAuthor
#1 includes_menu_inc_ah20150211.patch773 bytesarne_hortell
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

arne_hortell’s picture

arne_hortell’s picture

After running the patch above at some sites with good results, it turned out one site (with a lots of logging) complained that variable $uid was unknown.
Therefore i fixed it by adding global $user and changed $uid to $user->hid like this

Sometimes it turns out that CRON in drupal requires a LOT of memory, often the problem comes up as a memory exhausted message in menu.inc

This turns out to be because the menu_rebuild is called many times when its actually is only needed to be called once.

This is my quick-fix and i know hacking drupal core isn't the way to go

--- includes/menu.inc.old 2015-02-11 11:01:12.411387934 +0000
+++ includes/menu.inc 2015-02-11 11:10:16.204995356 +0000
@@ -2717,6 +2717,16 @@
return FALSE;
}

+// ****************************
+// Added by arne@hortell.se
+// Found it in admin menus
+
+ $was_flushed = &drupal_static(__FUNCTION__, array());
+ global $user;
+ if (isset($was_flushed[$user->uid])) {
+ return FALSE;
+ }
+// *****************************
+
$transaction = db_transaction();

try {
@@ -2727,6 +2737,11 @@
menu_cache_clear_all();
_menu_clear_page_cache();

+// ****************************
+// Added by arne@hortell.se
+ global $user;
+ $was_flushed[$user->uid] = TRUE;
+// *****************************
+
if (defined('MAINTENANCE_MODE')) {
variable_set('menu_rebuild_needed', TRUE);
}

Ayesh’s picture

I haven't had this issue a lot, nor aware of a multiple menu rebuilds. But we surely should not rebuild menu multiple rimes.

May I ask why we need $GLOBALS['user']->uid for?

Ayesh’s picture

The menu_rebuild function has a lock to prevent multiple calls (which is better than a static cache because even multiple users tried to clear the cache, it wouldn't run).

arne_hortell’s picture

I really don't know but the $uid originally came from the admin menu where i found that they prevent rebuilding the menu lot of times.
The thing with the lock is when multiple modules call to rebuild the menu, they surly get locked from OTHER users but the single user which executes it serially does not get aware that the menu already was rebuild and therefore does it again.

cilefen’s picture

Priority: Critical » Major
Issue tags: +needs profiling, +Needs steps to reproduce

How much memory are we talking about?

This issue needs the steps to reproduce, because as far as I know, this isn't an issue for many people. That doesn't mean it isn't an issue—some combination of conditions (lots of modules?) creates this on the reporter's site. A change to core could alleviate the problem. Incidentally, the menu error could be misleading if memory is exhausted.

This issue is major priority according to the guide.

Version: 7.34 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.