Skip to content

Commit 293f362

Browse files
committed
Merge branch 'BUG18798953' into develop
2 parents 1340616 + f36a8b2 commit 293f362

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

lib/mysql/connector/connection.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,17 @@ def connect(self, **kwargs):
452452
self._open_connection()
453453
self._post_connection()
454454

455+
def shutdown(self):
456+
"""Shut down connection to MySQL Server.
457+
"""
458+
if not self._socket:
459+
return
460+
461+
try:
462+
self._socket.shutdown()
463+
except (AttributeError, errors.Error):
464+
pass # Getting an exception would mean we are disconnected.
465+
455466
def disconnect(self):
456467
"""Disconnect from the MySQL server
457468
"""

lib/mysql/connector/network.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def get_address(self):
9898
"""Get the location of the socket"""
9999
raise NotImplementedError
100100

101+
def shutdown(self):
102+
"""Shut down the socket before closing it"""
103+
try:
104+
self.sock.shutdown(socket.SHUT_RDWR)
105+
self.sock.close()
106+
del self._packet_queue
107+
except (socket.error, AttributeError):
108+
pass
109+
101110
def close_connection(self):
102111
"""Close the socket"""
103112
try:

tests/test_connection.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import unittest
3232
from decimal import Decimal
3333
import io
34+
import socket
3435

3536
import tests
3637
from . import PY2
@@ -1820,6 +1821,31 @@ def test_cmd_reset_connection(self):
18201821
else:
18211822
self.assertNotEqual((b'2',), self.cnx.get_rows()[0][0])
18221823

1824+
def test_shutdown(self):
1825+
"""Shutting down a connection"""
1826+
config = tests.get_mysql_config()
1827+
cnx = connection.MySQLConnection(**config)
1828+
sql = "SHOW GLOBAL STATUS WHERE variable_name LIKE 'Aborted_clients'"
1829+
cur = cnx.cursor()
1830+
cur.execute(sql)
1831+
aborted_clients = cur.fetchone()[1]
1832+
1833+
test_close_cnx = connection.MySQLConnection(**config)
1834+
test_shutdown_cnx = connection.MySQLConnection(**config)
1835+
1836+
test_close_cnx.close()
1837+
cur.execute(sql)
1838+
self.assertEqual(aborted_clients, cur.fetchone()[1])
1839+
1840+
test_shutdown_cnx.shutdown()
1841+
self.assertRaises(socket.error,
1842+
test_shutdown_cnx._socket.sock.getsockname)
1843+
cur.execute(sql)
1844+
self.assertEqual(str(int(aborted_clients) + 1), cur.fetchone()[1])
1845+
1846+
cur.close()
1847+
cnx.close()
1848+
18231849

18241850
class WL7937(tests.MySQLConnectorTests):
18251851
"""Allow 'LOAD DATA LOCAL INFILE' by default

tests/test_network.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def test_get_address(self):
106106
"""Get the address of a connection"""
107107
self.assertRaises(NotImplementedError, self.cnx.get_address)
108108

109+
def test_shutdown(self):
110+
"""Shutting down a connection"""
111+
self.cnx.shutdown()
112+
self.assertEqual(None, self.cnx.sock)
113+
109114
def test_close_connection(self):
110115
"""Closing a connection"""
111116
self.cnx.close_connection()

0 commit comments

Comments
 (0)