PHP Timing Out - tags.php

Please report bugs here!

Moderator: Thorsten

Post Reply
aksival
Posts: 2
Joined: Wed Apr 22, 2009 2:55 pm

PHP Timing Out - tags.php

Post by aksival »

Tags.php is causing PHP to timeout on every page that loads the cloud tag. This did not occur at first, but began happening yesterday after a number of people were adding entries into the system. The problematic code is:

Code: Select all

for ($n = 0; $n < $numberOfItems; $n++) {
	$valid = false;
	while (!$valid) {
		$rand = rand(1, count($allTags) + 1);
		if (!isset($soFar[$rand])) {
			if (isset($allTags[$rand])) {
				$valid = true;
				$soFar[$rand] = '';
				$tags[$rand] = $allTags[$rand];
			}
	
		}
	}
}
An infinite while loop is sometimes caused because

Code: Select all

rand(1, count($allTags) + 1)
cannot produce a unique value with the provided range. We've patched the file as a temporary fix just to get the system back up and running:

Code: Select all

$rand = rand(1, count($allTags) + 1000);
This affects version 2.0.12 as well as the latest release, 2.0.13. Please review my logic and verify that it will not have any adverse affects on the system until an official patch can be released.

Regards,
Cory
Thorsten
Posts: 15562
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: PHP Timing Out - tags.php

Post by Thorsten »

Hi,

please try this patch:

Code: Select all

        if (isset($allTags) && ($numberOfItems < count($allTags))) {
        	$keys = array_keys($allTags);
        	for ($n = 0; $n < $numberOfItems; $n++) {
                $valid = false;
                while (!$valid) {
                    $rand = array_rand($keys);
                    if (!isset($soFar[$rand])) {
                        if (isset($allTags[$rand])) {
                            $valid        = true;
                            $soFar[$rand] = '';
                            $tags[$rand]  = $allTags[$rand];
                        }
                    }
                }
            }
        } else {
            $tags = PMF_Utils::shuffleData($allTags);
        }
Thanks for the hint!

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
djbeefburger
Posts: 3
Joined: Mon Apr 13, 2009 9:53 pm

Re: PHP Timing Out - tags.php

Post by djbeefburger »

I was able to replicate the problem after applying the proposed patch...

I created a large number of tags for a few articles (~90) and then deleting a bunch of them (and their respective relations to articles) from the faqtags table on the back end. This is similar to the process I used to clear out faqdata_tags in my other post (viewtopic.php?f=3&t=9787) . Clearing unused tags from the faqtags table appears to cause the problem. So ... one *shouldn't* see this problem unless they are doing some house-cleaning directly on the database as the current version doesn't delete unused tags from the faqtags table.

The reason it gets stuck in a loop is that it exhausts the possible number of matches whose index is less than the number of tags. When a large number of tags are deleted, the maximum index is much larger than the number of tags. The while loop finds all of the tags whose index is less than or equal to the number of tags. When all of those tags are found, the while loop gets stuck.

Using a modified version of Cory's original proposed patch:

add a function outside the class:

Code: Select all

function scmp($a, $b){
	return rand(-1,1);
}
Then replace

Code: Select all

for ($n = 0; $n < $numberOfItems; $n++) {
		   $valid = false;
		   while (!$valid) {
		      $rand = rand(1, count($allTags) + 1);
		      if (!isset($soFar[$rand])) {
		         if (isset($allTags[$rand])) {
		            $valid = true;
		            $soFar[$rand] = '';
		            $tags[$rand] = $allTags[$rand];
		         }
		   
		      }
with

Code: Select all

		$tags=$allTags;
		uasort($tags, 'scmp');
		$tags=array_slice(0, $numberOfItems);
I haven't fully tested this on a densely populated DB... Can you review this and confirm it is sufficient and compatible with other functions?

Cheers,

Eric
Thorsten
Posts: 15562
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: PHP Timing Out - tags.php

Post by Thorsten »

Hi Eric,

here's my patch:

Code: Select all

        if (isset($allTags) && ($numberOfItems < count($allTags))) {
            $keys = array_keys($allTags);
            for ($n = 0; $n < $numberOfItems; $n++) {
                $valid = false;
                while (!$valid) {
                    $rand = array_rand($keys);
                    if (isset($allTags[$rand])) {
                        $valid       = true;
                        $tags[$rand] = $allTags[$rand];
                        unset($keys[$rand]);
                    }
                }
            }
        } else {
            $tags = PMF_Utils::shuffleData($allTags);
        }
I get all IDs by using array_keys() and then do the randomizing with array_rand(). Should be much faster.

Could you also try my patch?

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
aksival
Posts: 2
Joined: Wed Apr 22, 2009 2:55 pm

Re: PHP Timing Out - tags.php

Post by aksival »

@Thorsten - thanks for the help. I'll keep you posted :)
bepluy
Posts: 3
Joined: Sat Mar 13, 2010 11:03 am

Re: PHP Timing Out - tags.php

Post by bepluy »

I still have that problem... last 2.6.3 version , asap i have tag №66 in faqfaqtags table - i'm getting stuck with ....

Code: Select all

 PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/......../inc/Tags.php on line 117
 PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/......./inc/Tags.php on line 116
 PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /home/......../inc/Tags.php on line 115
Tags.php is absolutely unmodified (as it coming with 2.6.3)

So if i add new article and write there any NEW tag , it adds as tagging_id №66 and we have timeout...
When i delete it manually from phpmyadmin - everything working like a charm ...

Any suggestions ?
UTAKA
Posts: 37
Joined: Sun Jan 24, 2010 8:01 am
Location: phpmyfaq-jp.org
Contact:

Re: PHP Timing Out - tags.php

Post by UTAKA »

Hi

The resulting Tagcloud is no shuffled. 2.6.3
viewtopic.php?f=3&t=10403
---------------------------------------
Sorry...I am not good at English.
http://www.phpmyfaq-jp.org
---------------------------------------
Thorsten
Posts: 15562
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: PHP Timing Out - tags.php

Post by Thorsten »

Hi,

so all your "new" tagging ids have the id 66?

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
bepluy
Posts: 3
Joined: Sat Mar 13, 2010 11:03 am

Re: PHP Timing Out - tags.php

Post by bepluy »

Hi! No , not all ... i mean from 65 and on..

i can't check with 67 or 68 , because when i'm adding new taf - it becomess 66 and i'm gettin timeout...

Should try UTAKA solution...

UPD. It works !!! Thank you all !
Thorsten
Posts: 15562
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Re: PHP Timing Out - tags.php

Post by Thorsten »

Hi,

I added UTAKAs version already for the 2.6.4 release!

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
Post Reply