title: PHP-FPM processes holding onto MongoDB connection states
body: <p>For the relevant part of our server stack, we're running:</p>
  
  <ul>
  <li>NGINX 1.2.3</li>
  <li>PHP-FPM 5.3.10 with PECL mongo 1.2.12</li>
  <li>MongoDB 2.0.7</li>
  <li>CentOS 6.2</li>
  </ul>
  
  <p>We're getting some strange, but predictable behavior when the MongoDB server goes away (crashes, gets killed, etc). Even with a <code>try/catch</code> block around the connection code, i.e:</p>
  
  <pre><code>try
  {
      $mdb = new Mongo('mongodb://localhost:27017');
  }
  catch (MongoConnectionException $e)
  {
      die( $e-&gt;getMessage() );
  }
  
  $db = $mdb-&gt;selectDB('collection_name');
  </code></pre>
  
  <p>Depending on which PHP-FPM workers have connected to mongo already, the connection state is cached, causing further exceptions to go unhandled, because the <code>$mdb</code> connection handler can't be used. The troubling thing is that the <code>try</code> does not consistently fail for a considerable amount of time, up to 15 minutes later, when -- I assume -- the php-fpm processes die/respawn. </p>
  
  <p>Essentially, the behavior is that when you hit a worker that hasn't connected to mongo yet, you get the die message above, and when you connect to a worker that has, you get an unhandled exception from <code>$mdb-&gt;selectDB('collection_name');</code> because catch does not run.</p>
  
  <p>When PHP is a single process, i.e. via Apache with mod_php, this behavior does not occur. Just for posterity, going back to Apache/mod_php is not an option for us at this time.</p>
  
  <p>Is there a way to fix this behavior? I don't want the connection state to be inconsistent between different php-fpm processes.</p>
  
  <p><strong>Edit:</strong>
  While I wait for the driver to be fixed in this regard, my current workaround is to do a quick polling to determine if the driver can handle requests and then load or not load the MongoDB library/run queries if it can't connect/query:</p>
  
  <pre><code>try
  {
      // connect
      $mongo = new Mongo("mongodb://localhost:27017");
  
      // try to do anything with connection handle
      try
      {
          $mongo-&gt;YOUR_DB-&gt;YOUR_COLLECTION-&gt;findOne();
          $mongo-&gt;close();
          define('MONGO_STATE', TRUE);
      }
      catch(MongoCursorException $e)
      {
          $mongo-&gt;close();
          error_log('Error connecting to MongoDB: ' . $e-&gt;getMessage() );
          define('MONGO_STATE', FALSE);
      }
  }
  catch(MongoConnectionException $e)
  {
      error_log('Error connecting to MongoDB: ' . $e-&gt;getMessage() );
      define('MONGO_STATE', FALSE);
  }
  </code></pre>