Jeremy DeGroot
Jeremy DeGroot

Reputation: 4506

How to avoid or fix PHP Warning: Too many open files

I'm running PHP 5.2 on Fedora, and I keep getting this warning after about 1000 iterations of my loop, which means the program has stopped working and needs to be restarted. I could set this up to exit after 1000 iterations and restart via a cron shortly thereafter, but that feels like the coward's way out. The loop follows; I should add that get_load() preforms a file_get_contents() call.

while ($row = select_row($sql))
{
    while (($load = get_load()) > 10)
    {
        echo "Going to sleep (load: ".$load.")\n";
        sleep(60*3);
    }
    $id = $row['id'];
    foreach ($sizes as $abbr=>$size)
    {
        if($row[$size] != "yes")
        {
            continue;
        }
        $filename = "/images/".$abbr."/".$id.".jpg";
        $tmp_file = "/tmp/".$id.".jpg";
        if ($size == "large")
        {
            //We want to progressively interlace our large bookcovers because it saves on filesave above 10K.
            $cmd = "convert -strip -interlace Plane ".$filename." ".$tmp_file;
        }
        else
        {
            $cmd = "convert -strip ".$filename." ".$tmp_file;
        }
        $convert = popen($cmd." 2>&1", "r");
        if (is_resource($convert))
        {
            echo fgets($convert);
            if(pclose($convert) == 0)
            {
                 //Upload converted file to remote server
            }
            unlink($tmp_file);
        }
    }

Edit: After reading the first two answers, I realized that in taking out the file uploading code that wasn't relevant to my problem, I took out my pclose() statement. Put in the pclose() as it appears in my code.

Further edit: Posted get_load() as requested

function get_load()
{
    $load = explode(" ", file_get_contents("/proc/loadavg"));
    return $load[0];
}

Upvotes: 3

Views: 5183

Answers (5)

Steve Tauber
Steve Tauber

Reputation: 10179

I've had this problem recently when using Xdebug and PhpStorm (on Mac). There is an open bug here:

http://bugs.xdebug.org/view.php?id=1070

and

https://youtrack.jetbrains.com/issue/WI-25307

Upvotes: 1

Michael
Michael

Reputation: 35351

I know you said get_load() uses file_get_contents(), but to make sure...does it properly close all the files it opens? Could you post the code from that function?

Edit: Even though it's a built-in function, try using something other than file_get_contents() to read the file and see what happens.

Upvotes: 0

Paul Tomblin
Paul Tomblin

Reputation: 182850

popen can only return two things, either a resource or FALSE. Maybe you should test against FALSE instead of is_resource? You're leaking file handles, so the obvious thing to check for is making sure you're always closing a file once you open it, and the obvious place where you're opening file handles is the popen call. Trace through your logic and make sure you're not somehow skipping closing those pipes, either through a logic error or an exception.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655697

You should close the pointer after using it with pclose.

Upvotes: 4

tkotitan
tkotitan

Reputation: 3009

Try closing the process each time after you write to it with pclose().

Upvotes: 2

Related Questions