You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Prevent sockets from being returned to the pool if they're in an inconsistent state due to an async exception from PyThreadState_SetAsyncExc(). Connection and ReplicaSetConnection always use sockets the same way: first sendall() with a request, then __receive_message_on_socket() to get the response. (Named __recv_msg in RSC.) A socket is vulnerable to async exceptions starting at sendall() and ending after __receive_message_on_socket(). If both are completed, then the socket can be returned to the pool. If an exception's raised during this period, the socket must be closed. I wrap each section comprising sendall() and __receive_message_on_socket() like: try: sock_info.sock.sendall(msg) response = self.__receive_message_on_socket(1, rqst_id, sock_info) except: sock_info.close() raise Since the socket's pool of origin isn't easily available in every function where the socket must be closed, I close it directly instead of calling pool.discard_socket(sock_info). This is safe, because Pool checks sock_info.closed everywhere that matters, so the socket won't be reused after it's closed. Calling close() instead of discard_socket() also keeps Connection and RSC from further divergence, which is good in case we ever refactor them.
0 commit comments