Merge "Fix glance_http HTTPS response issues"
diff --git a/tempest/common/glance_http.py b/tempest/common/glance_http.py
index e72cd9e..2ce05ee 100644
--- a/tempest/common/glance_http.py
+++ b/tempest/common/glance_http.py
@@ -218,6 +218,8 @@
         return getattr(self.connection, name)
 
     def makefile(self, *args, **kwargs):
+        # Ensure the socket is closed when this file is closed
+        kwargs['close'] = True
         return socket._fileobject(self.connection, *args, **kwargs)
 
 
@@ -345,6 +347,15 @@
         self.sock = OpenSSLConnectionDelegator(self.context, sock)
         self.sock.connect((self.host, self.port))
 
+    def close(self):
+        if self.sock:
+            # Remove the reference to the socket but don't close it yet.
+            # Response close will close both socket and associated
+            # file. Closing socket too soon will cause response
+            # reads to fail with socket IO error 'Bad file descriptor'.
+            self.sock = None
+        super(VerifiedHTTPSConnection, self).close()
+
 
 class ResponseBodyIterator(object):
     """A class that acts as an iterator over an HTTP response."""