From 1f26c5736949e1c3b57c024a315e33fc419f126e Mon Sep 17 00:00:00 2001 From: Alex Schultz <aschultz@redhat.com> Date: Fri, 2 Aug 2019 08:20:56 -0600 Subject: [PATCH] Cleanup session on delete If an external http connection was not passed into the client, we create one with a requests.Session() on our own. Once this is used, it may still have an open socket when the connection is closed. We need to handle the closing of the requests.Session() ourselves if we created one. If you do not close it, a ResourceWarning may be reported about the socket that is left open. Change-Id: I200ad0cdc8b7999c3f5521b9a822122bd18714bf Closes-Bug: #1838775 --- swiftclient/client.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/swiftclient/client.py b/swiftclient/client.py index 4be2e2d6..448bb468 100644 --- a/swiftclient/client.py +++ b/swiftclient/client.py @@ -438,6 +438,15 @@ class HTTPConnection(object): if timeout: self.requests_args['timeout'] = timeout + def __del__(self): + """Cleanup resources other than memory""" + if self.request_session: + # The session we create must be closed to free up file descriptors + try: + self.request_session.close() + finally: + self.request_session = None + def _request(self, *arg, **kwarg): """Final wrapper before requests call, to be patched in tests""" return self.request_session.request(*arg, **kwarg)