From d291c50bd6c712f19641c21198ca0bfd737b5368 Mon Sep 17 00:00:00 2001
From: Tim Burke <tim.burke@gmail.com>
Date: Fri, 22 Jun 2018 12:26:48 -0700
Subject: [PATCH] Clean up KMIPProxy initialization and destruction

Fixes #381.
---
 kmip/services/kmip_client.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/kmip/services/kmip_client.py b/kmip/services/kmip_client.py
index 547190e..ea4da60 100644
--- a/kmip/services/kmip_client.py
+++ b/kmip/services/kmip_client.py
@@ -79,6 +79,10 @@ class KMIPProxy:
         self.logger = logging.getLogger(__name__)
         self.credential_factory = CredentialFactory()
         self.config = config
+        # Even partially-initialized objects need to be garbage collected, so
+        # make sure we have a socket attr before we go raising ValueErrors.
+        # Otherwise, we can hit AttributeErrors when __del__ is called.
+        self.socket = None
 
         if config_file:
             if not isinstance(config_file, six.string_types):
@@ -103,7 +107,6 @@ class KMIPProxy:
         self.authentication_suites = [
             AuthenticationSuite.BASIC,
             AuthenticationSuite.TLS12]
-        self.socket = None
 
     def get_supported_conformance_clauses(self):
         """
@@ -256,7 +259,7 @@ class KMIPProxy:
             try:
                 self.socket.shutdown(socket.SHUT_RDWR)
                 self.socket.close()
-            except OSError:
+            except (OSError, socket.error):
                 # Can be thrown if the socket is not actually connected to
                 # anything. In this case, ignore the error.
                 pass