Matt
Matt

Reputation: 23789

Intermittent "too many open files" when including/requiring PHP scripts

On my development box (thank goodness it's not happening in production—that I know of—yet), as I'm working on a PHP site, I get this occasional error:

Warning: require_once(filename.php): failed to open stream: Too many open files in path/functions.php on line 502

Fatal error: require_once(): Failed opening required 'filename.php' (include_path='/my/include/path') in path/functions.php on line 502

Line 502 of functions.php is my "autoload" function which automatically requires PHP files containing classes I use:

function autoload($className)
{
    require_once $className . ".php";   // <-- Line 502
}

By an "occasional" error, I mean that it'll work fine for about a day of development, then when I first see this, I can refresh the page and it'll be okay again, then refreshing gives me the same error. This happens just a few times before it starts to show it every time. And sometimes the name of the file it's requiring (I have a script split out into several PHP files) is different... it's not always the first or last or middle files that it bombs on.

Restarting php-fpm seems to solve the symptoms, but not the problem in the long run.

I'm running PHP 5.5.3 on my Mac (OS X 10.8) with nginx 1.4.2 via php-fpm.

Running lsof | grep php-fpm | wc -l tells me that php-fpm has 824 files open. When I examined the actual output, I saw that, along with some .so and .dylib files, the vast majority of lines were like this:

php-fpm    4093 myuser  69u   unix 0x45bc1a64810eb32b    0t0   ->(none)

The segment "69u" and the 0x45bc1a6481... number are different on each row. What could this mean? Is this the problem? (ulimit is "unlimited")

Incidentally (though perhaps un-related), there's also one or two of these:

php-fpm  4093 myuser  8u  IPv4 0x45bc1a646b0f97b3   0t0   TCP 192.168.1.2:59611->rest.nexmo.com:https (CLOSE_WAIT)

(I have some pages which use HttpRequest (PECL libraries) to call out to the Nexmo API. Are these not being closed properly or something? How can I crack down on those?)

Upvotes: 1

Views: 1088

Answers (1)

trogwar
trogwar

Reputation: 120

Try to set php-fpm (they're set to infinite by default) to more appropriate values on your needs.

For example:

emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s

Maybe, set this too if your app works with lots of files:

rlimit_files = 1024

Upvotes: 1

Related Questions