PHP5-FPM randomly starts consuming a lot of CPU
It's easy to assume that when top shows a sudden spike in CPU usage for your PHP processes, you have an infinite loop or a particularly heavy calculation running in memory. However, in high-load scenarios involving complex frameworks, the culprit is often far less elegant: a process stuck in uninterruptible sleep waiting on I/O due to resource exhaustion.
Consider a setup with NGINX, PHP5-FPM, and APCu (or legacy APC) running on Ubuntu. The request flow involves loading large classes, instantiating main objects, starting sessions, fetching configuration from Memcached, parsing XML templates, and finally rendering HTML. Under a load of 20 requests per second using http_load, the system appears stable initially. Multiple PHP-FPM workers are active, each consuming a few percent of CPU, with plenty of idle processes in the pool to handle incoming connections immediately.
Then, after an unpredictable interval ranging from one second to several minutes, two specific PHP-FPM processes suddenly jump to 45-48% CPU. Crucially, they enter the R state (Running) or quickly cycle through it while blocking other work. Within seconds, these processes hang. New HTTP requests stop receiving responses and eventually time out.
The tell-tale sign of this specific failure mode is found in the FPM error log:
WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 58 total children
This warning indicates that the worker processes are not returning control to the supervisor fast enough. The supervisor attempts to spawn more children to handle the backlog, but they cannot execute because the existing workers are blocked. If you attempt to stop the service (php5-fpm stop) or even restart it while this state persists, the kill signal may fail to terminate the processes immediately because they are stuck in a state where they do not respond to standard termination signals.
A deeper investigation into the system logs often reveals the root cause: segmentation faults occurring within the dynamic linker (ld-2.13.so). The kernel log (dmesg) might report entries like php5-fpm[PID]: segfault at b6ec8ff4 ... error 4 in ld-2.13.so. This suggests that the process is attempting to access invalid memory addresses, often triggered by corrupted shared objects or race conditions during the loading of large class hierarchies.
Interestingly, stopping the PHP-FPM service does not immediately restore filesystem responsiveness. SSH sessions may hang, and file downloads can stall for minutes. This behavior occurs because the systemd (or traditional init) reclamation of resources is delayed while zombie processes or hung threads continue to hold file descriptors open. The OS reaches a state where it cannot allocate new file handles, leading