Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-2167

the driver guide after_fork recommendation is incorrect (the driver does not support it)

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.11.3
    • Component/s: Docs
    • Labels:
      None

      The driver guide recommends the following:

       

      It is recommended to not create any Mongo::Client instances prior to the fork. If the parent process needs to perform operations on the MongoDB database, reset the client in an after_fork handler which is defined in unicorn.rb:
      after_fork do |server, worker|
        $client.close
        $client.reconnect
      end
      

       

       

      Calling close on an existing client after fork is wrong because close actually uses the client to send commands to the server, which is precisely what you are trying to avoid (using the client after forking). This results in warnings/errors:

      WARN -- : MONGODB | Detected PID change - Mongo client should have been reconnected
      WARN -- : Retrying ismaster in monitor for server:port due to: Mongo::Error::SocketError EOFError: end of file reached
      

       

      There are two possible things you can do after fork:

      1. create a new client, letting the old one be quietly garbage collected
      2. #1 can be a problem if references to the original client and/or its child objects (databases, collections) have spread throughout the app. In this case you really do want to reconnect the existing client, but instead of close, a different operation is needed, that resets the clients internal state to closed (as if a new client), without talking to the server. 

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            bughit.github@gmail.com bug hit
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: