100% CPU load
After a few days of operations (roughly a few hundred connections in total, rarely more than 5 concurrent ones) the ratchet task locks up one CPU thread permanently until restarted. It keeps working though.
I installed the libevent PHP extension, but it didn't solve the problem. It doesn't even seem to be related to real workload.
I wasn't able to diagnose this problem in any way. Is there some sort of debug output one can enable?
This is the startup script: http://pastebin.com/UnFGhD5u
This is the application class: http://pastebin.com/BTBDNLxd
Both are really rather simple, I don't see anything that could end up in a loop ...
Any ideas/suggestions how to debug this?
P.S.: This is a Ubuntu 12.04 distro PHP installation.
In addition to installing the PECL libevent extension, did you install libevent-dev on your system?
I've seen two things that make the cpu go to 100%: stream loop instead of libevent and connections that didn't properly close so the loop constantly ticks on them. That fix is implementing your own heartbeat or putting a reverse proxy with a timeout in front of Ratchet.
libevent-dev is installed and PHP uses it:
root@78:~# php -i | grep libevent
libevent
libevent support => enabled
libevent version => 2.0.16-stable
There doesn't seem to be a buildup of connections on the tcp level (Ratchet listens on 78.137.117.211:443):
root@78:~# netstat -np
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 78.137.117.211:443 82.69.37.159:56853 ESTABLISHED 21298/php
tcp 0 0 127.0.0.1:34452 127.0.0.1:11211 ESTABLISHED 21298/php
tcp 0 440 78.137.117.136:2020 200.86.228.16:64568 ESTABLISHED 26270/0
tcp 0 0 78.137.117.211:443 98.119.198.217:57453 ESTABLISHED 21298/php
tcp 0 0 127.0.0.1:11211 127.0.0.1:34452 ESTABLISHED 1188/memcached
However, there IS something strange:
unix 3 [ ] STREAM CONNECTED 8805 1188/memcached
unix 3 [ ] STREAM CONNECTED 8804 1188/memcached
unix 3 [ ] STREAM CONNECTED 8802 1188/memcached
unix 3 [ ] STREAM CONNECTED 8801 1188/memcached
unix 3 [ ] STREAM CONNECTED 8799 1188/memcached
unix 3 [ ] STREAM CONNECTED 8798 1188/memcached
unix 3 [ ] STREAM CONNECTED 8796 1188/memcached
unix 3 [ ] STREAM CONNECTED 8795 1188/memcached
unix 3 [ ] STREAM CONNECTED 8793 1188/memcached
unix 3 [ ] STREAM CONNECTED 8792 1188/memcached
Only Ratchet uses memcached, not sure if there should be so many sockets, but I admit to being clueless about it. :-)
Is there any way to check how many connections Ratchet thinks it is currently serving? But even if no connection is properly closed, the traffic on the box is so low, I don't think it can saturate a CPU in a couple of days. It also tends to go from near 0% to 100% rather quickly if not instantly - haven't particularly observed it, but every time I have checked it was either all or nothing.
Also is there a way to check if Ratchet REALLY uses libevent?
var_dump($server->loop)
See what class that is. It should hopefully be an instance of LibEventLoop.
Off the top of my head I can't think of a good way to check connections on production. In development using xdebug and GDBp could let you check what connections React is holding.
It seems libEventLoop is being used:
object(React\EventLoop\LibEventLoop)#20 (7)
Now that the server is seeing a few more connections, it usually locks up daily. I'm struggling with the remote connection of xdebug a bit. Would a cachegrind file help you identify the problem?
glad i read this before i started working on implementing this.
gregtampa: To my knowledge this is a rare problem. There are a lot of people happily using Ratchet without issues.
thank you for the note. i seen other issues and have a few questions.. ill find out where i can ask these.
I have same problem. I upgraded PHP 5.5.8, libevent but cpu usage is still 100%.
A couple related links: https://groups.google.com/forum/#!topic/ratchet-php/c_C7kzJF38E https://groups.google.com/forum/#!topic/ratchet-php/dKtjEPx1zTo
I have a better idea of what might be causing this issue. Even though I have seen this happen on my own servers before I'm still not able to replicate it so it's difficult to test any possible fixes I might try. If anyone has steps to reproduce please post them here.
I tried to get a cachegrind file for the 100% CPU situation, but failed, because the server crashed before getting there. I didn't notice that before, because it automatically restarts.
Maybe the two are related. Would a (huge) cachegrind file of a crash be of any help?
It may help. If you want to send it to me I'll check it out. XDebug+profiling would cause huge performance issues, I can see why you'd have a hard time capturing the issue.
I can just confirm that I am facing the same issues. I am on Ubuntu 12.04, PHP 5.5.5 and using Libevent. Managing my processes through Supervisor. I have just looked at my logs from Munin and from what I can see the problem is very random but happens often. Even though the CPU utilization hits 100% the socket still accepts connections.
If someone can tell me how to log this I can give it a try?
@aborjesson You create a cachegrind file using XDebug's Profiler.
If you can send me a cachegrind file from when your CPU is spiking I may be able to determine the issue. Also, if you have steps to reproduce the issue that would be of great help to me.
@cboden I think you have got the files now. Sent to your about.me email. Please let me know if there were any troubles
@aborjesson Yup, got them, thank you! I should have time to review them on the weekend.
@cboden : Any news on this?
@aborjesson I spent a bit of time trying to replicate this on the weekend with no success (dropping a network connection without sending shutdown signals then attempting to send data). I will have much more time this weekend to look in to this issue as well as review your cachegrind file.
A possible fix for this has been submitted to React for review: reactphp/react#273
@cboden : I have now been running this for about 2 days without a single issue.