Bug with threading, strace, microtime and usleep
This took some finding....
I was having other problems, and I was using strace to see what was going on with my PHP processes.
While doing this, I found that my threads kept on 'stopping' for what I could see as no reason.
Eventually, I found that running php with pthreads and using strace, seems to cause timing problems, under very specific situations.
If you have the following example code:
class gameThread extends Thread
{
private static $threadID;
public function __construct($th)
{
self::$threadID = $th;
}
public function run()
{
switch (self::$threadID) {
case 1 :
echo "starting replyThread\n";
while (true) {
echo "In the loop\n";
usleep(1000000);
//$this->synchronized(function(){
// $this->wait(1000000);
//});
}
break;
case 2 :
echo "starting game loop\n";
while(true) {
$currTime = microtime(true);
}
}
}
}
// Main
$replyThread = new gameThread(1);
$replyThread->start();
$gameThread = new gameThread(2);
$gameThread->start();
If you run with strace:
strace -f -o test.log php test01.php
Then the only output you get is the 2 'starting' messages, and then just 1 'In the loop'.
If you leave it running, every now and again it will pop up another 'In the loop' message, but certainly not any where near every 1 second, and quite random timings.
I've tried it with both a usleep()/time_nanosleep(), and a sync/wait, but have the same effect.
The example has to be using both microtime() and usleep() to make it happen. strace log seems to indicate that it goes into a loop issuing lots of gettimeofday() calls. In my actual real script, it seems a little different, and strace shows is that the usleep() never finishes.
The actual code I'm using this in is quite complicated, and involves threads that are waiting for socket replies, processing them and sending them back out.
Any ideas or comments, on why and if this can be fixed, and I use strace quite a bit, esp for performance testing?
Thanks,
Rob D.
I'm not able to reproduce with the latest version of pthreads.
What version of PHP and pthreads are you using, and on what OS ?
Sorry, forgot to say that: PHP 7.0.2, and pthreads 3.1.7dev, running on Ubuntu 3.13.0-66-generic (strace is v4.8)
Hmm, strange. I was working on a amazon AWS EC2 instance. Just tried it on a local VM in Virtual Box, and it worked fine in there.
Might be a bit more specific than I thought.
Give me a bit to look into it more, and see if I can see what the diff is...
Cool, thanks ...
Ok... interesting day...
So, on AWS EC2, I was getting the problem on an 'old' instance I created with Ubuntu 14.04 LTS some time ago.
I re-created a brand new instance, and reinstalled php and pthreads, and it still happened.
I also tested updating Ubuntu with all the standard patches (ie apt-get upgrade).
It happened all the time.
So, I then tried back on my local PC, with Virtual Box. Installing Ubuntu 14.04 LTS and also 14.04.1 LTS. But I could not get it to happen on these versions locally.
However, I went back to 12.04 LTS, and then it did happen locally.
So, it seems a bit strange. I'm not 100% convinced its to do with the version, it seems to maybe be a timing issue somewhere, that sometimes happens and sometimes doesnt.
So, I'm not sure how to proceed.
If you want, I can give you access to the AWS instance where it happens all the time, if you would like or have the time, up to you...
Rob.
Access would be great !
I may not have time to look before Monday ... I have a really busy weekend ahead, which is why I'm awake at 3am on Friday morning replying to emails and notifications ... but I will look ... sorry about the delay though ...
Ok, I've set you up with your key.
server: ec2-54-191-2-227.us-west-2.compute.amazonaws.com user: joe78231
I've given you full sudo access, so just sudo bash to run everything without permission problems.
Dont mind what you do/install/change in there, its just a quick instance that I setup to test this, and nothing else in there. I'll be deleting it once we are done anyway.
After creating the instance, this is what I did to install everything, incase it has any relevance:
cd /home/ubuntu
mkdir php
cd php
wget http://www.php.net/distributions/php-7.0.2.tar.gz
tar -xvf php-7.0.2.tar.gz
cd php-7.0.2
apt-get update
apt-get install libxml2-dev gcc libcurl4-gnutls-dev make autoconf git
./configure --prefix=/usr --with-config-file-path=/etc --enable-maintainer-zts --enable-shmop --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-opcache --enable-sockets --with-curl
make
make install
cp php.ini-development /etc/php.ini
echo "extension=pthreads.so" >> /etc/php.ini
echo "zend_extension=opcache.so" >> /etc/php.ini
vi /etc/php.ini (just changed date.timezone = UTC)
strip -s /usr/bin/php
git clone https://github.com/krakjoe/pthreads.git /home/ubuntu/php/pthreads-current
cd ../pthreads-current/
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
Currently, in this instance, I have not updated any patches for Ubuntu etc, so this is the 'bare' install from the AWS instance create.
I can be on stackoverflow chat if you need to: https://chat.stackoverflow.com/users/2154104/rob-donovan
Everything is under /home/ubuntu/php/
There is the test script there, test01.php, run with strace -f -o test.log php test01.php
Thanks for taking a look, whenever you get the chance.
Hi,
I have to remove this instance, as I need to free up resources, sorry.
When you get the time, give me a shout and I can setup another one quickly to show you the problem.
If the issue still exists, please reopen!