Hello,

    do {
      sleep(3);
      $buddylist_online = _drupalchat_buddylist_online($buddylist);
      //$message_count = db_query('SELECT COUNT(*) FROM {drupalchat_msg} m INNER JOIN {users} u ON m.uid1 = u.uid WHERE m.uid2 = :m.uid2 AND m.timestamp > :m.timestamp', array(':m.uid2' => $user->uid, ':m.timestamp' => $last_timestamp))->fetchField();
      $message_count = db_query('   SELECT COUNT(*)
                                    FROM {drupalchat_msg} m
                                    INNER JOIN {users} u ON m.uid1 = u.uid
                                    WHERE m.uid2 = :uid2
                                    AND m.timestamp > :timestamp', array(':uid2' => $user->uid, ':timestamp' => $last_timestamp))->fetchField();
      _drupalchat_touch_user($user->uid);
      module_invoke_all('drupalchat_longpoll'); // Long poll hook
    } while (((REQUEST_TIME - $initial_time) < (ini_get('max_execution_time') - 5)) && ($message_count == 0) && (_drupalchat_buddylist_diff($buddylist_online_old, $buddylist_online)));

1. Wanted to bring to your attention that sleep(3) in the polling code does not count as execution_time on linux.

2. The code above, per my tests, will never really break out from exhausting the max_execution_time because of point #1 and because $initial_time never changes. I've modified it as follows:

      _drupalchat_touch_user($user->uid);
      module_invoke_all('drupalchat_longpoll'); // Long poll hook
      $initial_time -= 3;

Thoughts?

Comments

darklrd’s picture

Thanks a lot for bringing this up. I am sorry but I couldn't get your point #1. Could you please elaborate? Thanks.

darklrd’s picture

Do you mean to say that REQUEST_TIME and $initial_time will always remain same?

Ashraf Amayreh’s picture

In general, sleep doesn't count as part of the execution time on Linux servers. So let's assume that you have max_execution_time set to 30 and you try this code:

sleep(100);
print "Done";

You will get a Done because sleep is not counted as part of the execution time :) What I did to fix this is simply subtract 3 seconds from the $initial_time after every sleep:

      _drupalchat_touch_user($user->uid);
      module_invoke_all('drupalchat_longpoll'); // Long poll hook
      $initial_time -= 3;

Hope this is the correct fix for it.

Ashraf Amayreh’s picture

Right. That's what I'm saying. They will of course vary in some microseconds, but the function that you expect will finish in 30 secs will actually take... probably a year to finish without timing out :D

Ashraf Amayreh’s picture

Or to illustrate it even more:

print REQUEST_TIME;
sleep(10);
print REQUEST_TIME;  //same value
darklrd’s picture

OMG! This is scary! I didn't know this. Thanks a lot for pointing this out :)

I will commit it soon. Thanks again!

Ashraf Amayreh’s picture

Anytime. If you do insist on helping out, then check out my just created issue on chat room :) Why are the access rules so screwed up for chat nodes?

http://drupal.org/node/1413294

darklrd’s picture

Status: Active » Fixed

Hi,

I tested this again. sleep() is counted as part of the execution time. The problem was REQUEST_TIME variable. It doesn't give the current time. I have fixed the issue. Thanks a lot for your help :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.