Apache PHP/OSX Mavericks: - failed to open stream: Too many open files

Code Poet picture Code Poet · Nov 4, 2013 · Viewed 19.9k times · Source

I've recently upgraded to OSX Mavericks and since then, I've started getting the aforementioned error on my development machine. There is no obvious problem in the code (it's an auto generated Yii sample application). What has happened as part of upgrade to Mavericks is:

  1. PHP was upgraded from 5.2.x which bundled with OSX Lion to 5.4.x.
  2. I had to get a Zend Debugger for PHP 5.4 by installing Zend Server, picking up the ZendDebugger.so and uninstalling the Zend Server (all this because Zend doesn't provide a standalone version of their debugger for php 5.4.x).

Ever since, I'm getting this problem after maybe loading and reloading the website a few time. After this error occurs, my web server keeps returning the same error for any other application hosted on localhost. I have to mention that static web pages are served up fine.

I've seen several threads on this topic. Most point out to issues in code where file handles are not being closed properly, thereby crossing the open file limit threshold. I also found this thread which seems to suggest this might be a zend debugger issue. There's also a bug report filed for php 5.2.x. Following the thread here, I tried the following:

$ ulimit -a

which reports:

open files (-n) 256

Also,

sysctl -a | grep files

returns,

kern.maxfiles = 12288
kern.maxfilesperproc = 10240
kern.maxfiles: 12288
kern.maxfilesperproc: 10240
kern.num_files: 3248

Another interesting thread suggests to raise this limit (currently 256) using:

ulimit -n 1024

I've tried everything, but nothing seems to be working. The problem is also not consistently reproducible.

I am wondering is using ulimit -n 1024 is going to affect apache, since from what I've read, it affects the number of files shell can have open.

Any help is appreciated.

EDIT:

  1. Restarting apache helps for a bit, till the error is encountered again.
  2. Leaving the web server idle for a bit (no definite interval) also helps.

Answer

okket picture okket · Nov 5, 2013

Shamelessly stolen from http://docs.basho.com/riak/latest/ops/tuning/open-files-limit/#Mac-OS-X

To check the current limits on your Mac OS X system, run:

$ launchctl limit maxfiles

The last two columns are the soft and hard limits, respectively.

To adjust the maximum open file limits in OS X 10.7 (Lion) or newer, edit /etc/launchd.conf and increase the limits for both values as appropriate.

For example, to set the soft limit to 16384 files, and the hard limit to 32768 files, perform the following steps:

Verify current limits:

$ launchctl limit

    cpu         unlimited      unlimited
    filesize    unlimited      unlimited
    data        unlimited      unlimited
    stack       8388608        67104768
    core        0              unlimited
    rss         unlimited      unlimited
    memlock     unlimited      unlimited
    maxproc     709            1064
    maxfiles    10240          10240

Edit (or create) /etc/launchd.conf and increase the limits. Add lines that look like the following (using values appropriate to your environment):

limit maxfiles 16384 32768

Save the file, and restart the system for the new limits to take effect. After restarting, verify the new limits with the launchctl limit command:

$ launchctl limit

    cpu         unlimited      unlimited
    filesize    unlimited      unlimited
    data        unlimited      unlimited
    stack       8388608        67104768
    core        0              unlimited
    rss         unlimited      unlimited
    memlock     unlimited      unlimited
    maxproc     709            1064
    maxfiles    16384          32768