-
Type: Bug
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: None
-
None
_RttMonitor very rarely keeps its connection alive after MongoClient.close() which leads to this rare test failure:
[2020/07/02 02:31:25.893] FAIL [0.027s]: test_close_closes_sockets (test_client.TestClient) [2020/07/02 02:31:25.893] ---------------------------------------------------------------------- [2020/07/02 02:31:25.893] Traceback (most recent call last): [2020/07/02 02:31:25.893] File "/data/mci/8c8a2ace2ab093d7cf40fb6263df2b44/src/test/test_client.py", line 830, in test_close_closes_sockets [2020/07/02 02:31:25.893] self.assertFalse(server._monitor._rtt_monitor._pool.sockets) [2020/07/02 02:31:25.893] AssertionError: deque([SocketInfo(<socket.socket fd=15, family=AddressFamily.AF_INET, type=526337, proto=6, laddr=('127.0.0.1', 51635), raddr=('127.0.0.1', 27017)>) at 140695887119808]) is not false
The problem happens because _RttMonitor.close() can race with _RttMonitor._ping(). A solution would be to check for close() after checking out a connection:
def _ping(self): """Run an "isMaster" command and return the RTT.""" with self._pool.get_socket({}) as sock_info: if self._closed: raise Exception("closed")
Note that I've marked this "minor" because this race is rare and the connection will still be closed when the client is garbage collected.