Bug #72691 C/Py does not provide any means to abruptly close/shutdown a connection
Submitted: 20 May 2014 11:50 Modified: 27 Aug 2014 18:27
Reporter: Alfranio Tavares Correia Junior Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:1.2.1 OS:Any
Assigned to: CPU Architecture:Any

[20 May 2014 11:50] Alfranio Tavares Correia Junior
Description:
C/Py provides the "close" or "disconnect" functions that gracefully close a connection to a server by sending the QUIT command to the server and then closing the associated socket. This is not useful though when the connection is blocked due to any reason (e.g. network partition).

How to repeat:
Check the code.

Suggested fix:
Implement a shutdown function that does not try to access the server and simply
shuts down the connection to the server.

=== modified file 'python2/mysql/connector/connection.py'
--- python2/mysql/connector/connection.py       revid:peeyush.x.gupta@oracle.com-20140514064049-fbztjsjgrk0ln15x
+++ python2/mysql/connector/connection.py       2014-05-19 11:52:34 +0000
@@ -430,6 +430,17 @@
         self._open_connection()
         self._post_connection()

+    def shutdown(self):
+        """Shut down connection to MySQL Server.
+        """
+        if not self._socket:
+            return
+
+        try:
+            self._socket.shutdown()
+        except (AttributeError, errors.Error):
+            pass  # Getting an exception would mean we are disconnected.
+
     def disconnect(self):
         """Disconnect from the MySQL server
         """

=== modified file 'python2/mysql/connector/network.py'
--- python2/mysql/connector/network.py  revid:peeyush.x.gupta@oracle.com-20140514064049-fbztjsjgrk0ln15x
+++ python2/mysql/connector/network.py  2014-05-19 11:54:52 +0000
@@ -102,6 +102,14 @@
         except (socket.error, AttributeError):
             pass

+    def shutdown(self):
+        """Shut down the socket."""
+        try:
+            self.sock.shutdown(socket.SHUT_RDWR)
+            del self._packet_queue
+        except (socket.error, AttributeError):
+            pass
+
     def send_plain(self, buf, packet_number=None):
         """Send packets to the MySQL server"""
         if packet_number is None:
[27 Aug 2014 18:27] Paul DuBois
Noted in 2.0.1 changelog.

Connector/Python now supports a shutdown() method that, unlike
disconnect(), closes the client connection without attempting to send
a quit command to the server first. Thus, it will not block if the
connection is disrupted for some reason such as network failure.