--- pymongo/master_slave_connection.py.old 2010-04-01 17:29:13.000000000 +0300 +++ pymongo/master_slave_connection.py 2010-04-01 19:22:21.000000000 +0300 @@ -21,6 +21,7 @@ from pymongo.connection import Connection from pymongo.database import Database +from pymongo.errors import AutoReconnect class MasterSlaveConnection(object): @@ -129,7 +130,27 @@ ._send_message_with_response(message, _sock)) # for now just load-balance randomly among slaves only... - connection_id = random.randrange(0, len(self.__slaves)) + while self.__slaves: + connection_id = random.randrange(0, len(self.__slaves)) + try: + # Test connection + self.__slaves[connection_id].server_info() + except AutoReconnect: + try: + # Try to reconnect + self.__slaves[connection_id]._reset() + break + except AutoReconnect: + # No more chances to restore connection: + # remove it from pool + self.__slaves.pop(connection_id) + else: + # Found live connection + break + else: + # self.__slaves is empty + connection_id = -1 + # _must_use_master is set for commands, which must be sent to the # master instance. any queries in a request must be sent to the