Skip to content

Commit 5ed9ea4

Browse files
committed
Discard sockets in error state.
1 parent f6f269e commit 5ed9ea4

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

pymongo/replica_set_connection.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,21 @@ def __update_pools(self):
452452
"""
453453
secondaries = []
454454
for host in self.__hosts:
455+
mongo = None
455456
try:
456457
if host in self.__pools:
457-
sock = self.__socket(self.__pools[host])
458+
mongo = self.__pools[host]
459+
sock = self.__socket(mongo)
458460
res = self.__simple_command(sock, 'admin', {'ismaster': 1})
459461
else:
460-
res, mongo = self.__is_master(host)
462+
res, conn = self.__is_master(host)
461463
bson_max = res.get('maxBsonObjectSize', MAX_BSON_SIZE)
462-
self.__pools[host] = {'pool': mongo,
464+
self.__pools[host] = {'pool': conn,
463465
'last_checkout': time.time(),
464466
'max_bson_size': bson_max}
465467
except (ConnectionFailure, socket.error):
468+
if mongo:
469+
mongo['pool'].discard_socket()
466470
continue
467471
# Only use hosts that are currently in 'secondary' state
468472
# as readers.
@@ -482,13 +486,15 @@ def refresh(self):
482486
hosts = set()
483487

484488
for node in nodes:
489+
mongo = None
485490
try:
486491
if node in self.__pools:
487-
sock = self.__socket(self.__pools[node])
492+
mongo = self.__pools[node]
493+
sock = self.__socket(mongo)
488494
response = self.__simple_command(sock, 'admin',
489495
{'ismaster': 1})
490496
else:
491-
response, _ = self.__is_master(node)
497+
response, conn = self.__is_master(node)
492498

493499
# Check that this host is part of the given replica set.
494500
set_name = response.get('setName')
@@ -510,6 +516,8 @@ def refresh(self):
510516
hosts.update([_partition_node(h)
511517
for h in response["passives"]])
512518
except (ConnectionFailure, socket.error), why:
519+
if mongo:
520+
mongo['pool'].discard_socket()
513521
errors.append("%s:%d: %s" % (node[0], node[1], str(why)))
514522
if hosts:
515523
self.__hosts = hosts
@@ -524,18 +532,22 @@ def __check_is_primary(self, host):
524532
"""Checks if this host is the primary for the replica set.
525533
"""
526534
try:
535+
mongo = None
527536
if host in self.__pools:
528-
sock = self.__socket(self.__pools[host])
537+
mongo = self.__pools[host]
538+
sock = self.__socket(mongo)
529539
res = self.__simple_command(sock, 'admin', {'ismaster': 1})
530540
else:
531-
res, mongo = self.__is_master(host)
541+
res, conn = self.__is_master(host)
532542
bson_max = res.get('maxBsonObjectSize', MAX_BSON_SIZE)
533-
self.__pools[host] = {'pool': mongo,
543+
self.__pools[host] = {'pool': conn,
534544
'last_checkout': time.time(),
535545
'max_bson_size': bson_max}
536546
except (ConnectionFailure, socket.error), why:
547+
if mongo:
548+
mongo['pool'].discard_socket()
537549
raise ConnectionFailure("%s:%d: %s" % (host[0], host[1], str(why)))
538-
bson_max = res.get('maxBsonObjectSize') or MAX_BSON_SIZE
550+
539551
if res["ismaster"]:
540552
return host
541553
elif "primary" in res:

0 commit comments

Comments
 (0)