Skip to content

Commit dc7d71a

Browse files
committed
BUG24342757: Fix unclosed socket on connection failure
Creating a Connection instance with use_pure=True and specifying an invalid password causes a ResourceWarning, when using Python 3.5.2 with -b -Wall, as the underlying socket is not closed in such a case. This patch fixes this issue by closing the socket upon exception raised.
1 parent 1015a88 commit dc7d71a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/mysql/connector/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ def __init__(self, *args, **kwargs):
9191
self._pool_config_version = None
9292

9393
if len(kwargs) > 0:
94-
self.connect(**kwargs)
94+
try:
95+
self.connect(**kwargs)
96+
except:
97+
# Tidy-up underlying socket on failure
98+
self.close()
99+
raise
95100

96101
def _do_handshake(self):
97102
"""Get the handshake from the MySQL server"""
@@ -224,6 +229,7 @@ def shutdown(self):
224229
self._socket.shutdown()
225230
except (AttributeError, errors.Error):
226231
pass # Getting an exception would mean we are disconnected.
232+
self._socket = None
227233

228234
def close(self):
229235
"""Disconnect from the MySQL server"""
@@ -235,6 +241,8 @@ def close(self):
235241
self._socket.close_connection()
236242
except (AttributeError, errors.Error):
237243
pass # Getting an exception would mean we are disconnected.
244+
self._socket = None
245+
238246
disconnect = close
239247

240248
def _send_cmd(self, command, argument=None, packet_number=0, packet=None,

0 commit comments

Comments
 (0)