diff --git a/includes/menu.inc b/includes/menu.inc index fa5a71e..19b9a64 100644 --- a/includes/menu.inc +++ b/includes/menu.inc @@ -453,10 +453,10 @@ function menu_get_item($path = NULL, $router_item = NULL) { $router_items[$path] = $router_item; } if (!isset($router_items[$path])) { - // Rebuild if we know it's needed, or if the menu masks are missing which - // occurs rarely, likely due to a race condition of multiple rebuilds. if (variable_get('menu_rebuild_needed', FALSE) || !variable_get('menu_masks', array())) { - menu_rebuild(); + if (_menu_check_rebuild()) { + menu_rebuild(); + } } $original_map = arg(NULL, $path); @@ -2693,6 +2693,21 @@ function menu_reset_static_cache() { } /** + * Checks that a menu rebuild is necessary and performs it if so. + */ +function _menu_check_rebuild() { + // To absolutely ensure that the menu rebuild is required, re-load the + // variables in case they were set by another process. + $variables = variable_initialize(); + if (empty($variables['menu_rebuild_needed']) && !empty($variables['menu_masks'])) { + unset($GLOBALS['conf']['menu_rebuild_needed']); + $GLOBALS['conf']['menu_masks'] = $variables['menu_masks']; + return FALSE; + } + return TRUE; +} + +/** * Populates the database tables used by various menu functions. * * This function will clear and populate the {menu_router} table, add entries @@ -2712,9 +2727,16 @@ function menu_rebuild() { // We choose to block here since otherwise the router item may not // be available in menu_execute_active_handler() resulting in a 404. lock_wait('menu_rebuild'); + + if (_menu_check_rebuild()) { + // If we get here and menu_masks was not set, then it is possible a menu + // being reloaded, or that the process rebuilding the menu was unable to + // complete successfully. A missing menu_masks variable could result in a + // 404, so re-run the function. + return menu_rebuild(); + } return FALSE; } - $transaction = db_transaction(); try { @@ -2736,6 +2758,12 @@ function menu_rebuild() { $transaction->rollback(); watchdog_exception('menu', $e); } + // Explicitly commit the transaction now, this ensures that the database + // operations during the menu rebuild are committed before the lock is made + // available again, since locks may not always reside in the same database + // connection. The lock is acquired outside of the transaction so should also + // be released outside of it. + unset($transaction); lock_release('menu_rebuild'); return TRUE; diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php old mode 100644 new mode 100755