title: Apache PHP/OSX Mavericks: - failed to open stream: Too many open files
body: <p>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 <a href="http://www.yiiframework.com/" rel="nofollow noreferrer">Yii</a> sample application). What has happened as part of upgrade to Mavericks is:</p>
  
  <ol>
  <li>PHP was upgraded from 5.2.x which bundled with OSX Lion to 5.4.x.</li>
  <li>I had to get a Zend Debugger for PHP 5.4 by installing <a href="http://www.zend.com/en/products/server/" rel="nofollow noreferrer">Zend Server</a>, 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).</li>
  </ol>
  
  <p>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.</p>
  
  <p>I've seen several <a href="https://www.google.co.in/#q=failed%20to%20open%20stream%20too%20many%20open%20files%20php" rel="nofollow noreferrer">threads</a> 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 <a href="http://forums.zend.com/viewtopic.php?f=8&amp;t=110823" rel="nofollow noreferrer">thread</a> which seems to suggest this might be a zend debugger issue. There's also a <a href="https://bugs.php.net/bug.php?id=45133" rel="nofollow noreferrer">bug report</a> filed for php 5.2.x. Following the thread <a href="http://krypted.com/mac-os-x/maximum-files-in-mac-os-x/" rel="nofollow noreferrer">here</a>, I tried the following:</p>
  
  <pre><code>$ ulimit -a
  </code></pre>
  
  <p>which reports:</p>
  
  <pre><code>open files (-n) 256
  </code></pre>
  
  <p>Also,</p>
  
  <pre><code>sysctl -a | grep files
  </code></pre>
  
  <p>returns,</p>
  
  <pre><code>kern.maxfiles = 12288
  kern.maxfilesperproc = 10240
  kern.maxfiles: 12288
  kern.maxfilesperproc: 10240
  kern.num_files: 3248
  </code></pre>
  
  <p>Another interesting <a href="https://stackoverflow.com/questions/14748499/fatal-error-too-many-open-files">thread</a> suggests to raise this limit (currently 256) using:</p>
  
  <pre><code>ulimit -n 1024
  </code></pre>
  
  <p>I've tried everything, but nothing seems to be working. The problem is also not consistently reproducible. </p>
  
  <p>I am wondering is using <code>ulimit -n 1024</code> is going to affect apache, since from what I've read, it affects the number of files shell can have open.</p>
  
  <p>Any help is appreciated.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <ol>
  <li>Restarting <code>apache</code> helps for a bit, till the error is encountered again.</li>
  <li>Leaving the web server idle for a bit (no definite interval) also helps.</li>
  </ol>