TOC: CPU fire - High Utilization - may have found culprit
| Project: | Tweakbox : drupal tweaking modules |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | critical |
| Assigned: | acp |
| Status: | postponed (maintainer needs more info) |
Jump to:
I was seeing my CPU begin to bounce its head at 90% utilization.
Background
- recently upped my max_execution_time and max_input_time to handle some long running scripts. Of course I forgot to reset. A low setting may have help mask the issue.
- began to notice CPU utilization exploding
- finally checking my error log file (of course I did everything else first)
Allowed memory size of 268435456 bytes exhausted (tried to allocate 5 bytes) in /var/www/html/drupal51/sites/all/modules/tweakbox/toc.module on line 142
Allowed memory size of 268435456 bytes exhausted (tried to allocate 128 bytes)
- took a look at the code above line 142 [toc.module] (shown below). Note "$cur_depth--; ". Since we are testing for "$cur_depth < $max_depth" this may be the culprit. Changed the code to what is shown below so we increment not decrement. Seems to be working.
while ( $store_matches[$m - 1][0] != end($last_index) ) {
$temp = array_pop($last_index);
// $cur_depth--; we should increase not decrease
$cur_depth++;
if ($cur_depth < $max_depth)
$concat_str .= ($cur_depth > 0 ? '</li></ol></li>' : '</ol>');
}Did I miss anything?
James

#1
Hello there,
there is a problem indeed, but your solution does not fix it. Current depth is the depth in which we are when writing the table of contents. So, for instance :
1. Menu entry <- cur_depth = 1
2. Menu entry <- cur_depth = 1
2.1 Submenu entry <- cur_depth = 2
3. Menu entry <- cur_depth = 3
and so on. So we need to decrease the cur_depth value when we exit a submenu, which is what the code does from lines 138 to 143.
My guess is that there is something wrong with your usage of the H tags. That's something I have in my TODO list I haven't checked for (too) long now, badly used tags, as for example this case :
h1h3
h2
This leads, most probably to the error you got, and the bug is actually me not checking for such cases. Could you please, if possible, give me the structure of the node that leads to this problem (only the H tags actually would be fine) ?
A possible fix would be to check if cur_depth is positive or null, so for instance in line 138 replace
<?phpwhile ( $store_matches[$m - 1][0] != end($last_index) ) {
?>
with
<?phpwhile ( $store_matches[$m - 1][0] != end($last_index) && $cur_depth >= 0 ) {
?>
A better solution, I will implement as soon as I get my hands on my computer, would be to exit gracefully by either not showing anything, or at least a small error message, maybe just to the logs...
Waiting for your input, and thanks for the feedback.
acp