Skip to content

Commit

Permalink
Reset connection pool after fork PYTHON-166
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed Oct 19, 2010
1 parent a19fd9b commit 463a7e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pymongo/connection.py
Expand Up @@ -135,22 +135,30 @@ class _Pool(threading.local):
"""

# Non thread-locals
__slots__ = ["sockets", "socket_factory", "pool_size"]
__slots__ = ["sockets", "socket_factory", "pool_size", "pid"]

# thread-local default
sock = None

def __init__(self, socket_factory):
self.pid = os.getpid()
self.pool_size = 10
self.socket_factory = socket_factory
if not hasattr(self, "sockets"):
self.sockets = []


def socket(self):
# we store the pid here to avoid issues with fork /
# multiprocessing - see
# test.test_connection:TestConnection.test_fork for an example
# of what could go wrong otherwise
# We use the pid here to avoid issues with fork / multiprocessing.
# See test.test_connection:TestConnection.test_fork for an example of
# what could go wrong otherwise
pid = os.getpid()

if pid != self.pid:
self.sock = None
self.sockets = []
self.pid = pid

if self.sock is not None and self.sock[0] == pid:
return self.sock[1]

Expand Down
1 change: 1 addition & 0 deletions test/test_connection.py
Expand Up @@ -320,6 +320,7 @@ def test_fork(self):

# Failure occurs if the connection is used before the fork
db.test.find_one()
db.connection.end_request()

def loop(pipe):
while True:
Expand Down

0 comments on commit 463a7e4

Please sign in to comment.