-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.11.3
-
Component/s: Docs
-
None
-
(copied to CRM)
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:
- create a new client, letting the old one be quietly garbage collected
- #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.
- duplicates
-
RUBY-2194 Provide forking guidance relevant to the driver
- Closed