Skip to content

Commit

Permalink
Improve close behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
lmazuel committed Apr 18, 2018
1 parent 0e96eee commit 4f3f7fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions msrest/service_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,15 @@ def send(self, request, headers=None, content=None, **config):
msg = "Error occurred in request."
raise_with_traceback(ClientRequestError, msg, err)
finally:
if not response or not kwargs['stream']:
session.close()
self._close_local_session_if_necessary(response, session, kwargs['stream'])

def _close_local_session_if_necessary(self, response, session, stream):
# Do NOT close session if session is self._session. No exception.
if self._session is session:
return
# Here, it's a local session, I might close it.
if not response or not stream:
session.close()

def stream_download(self, data, callback):
"""Generator for streaming request body data.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ def signed_session(self, session=None):
client.send(req) # Will fail, I don't care, that's not the point of the test
except Exception:
pass
assert client._session # Still alive

assert not cfg.keep_alive
assert creds.called == 2
assert client._session is None # Dead

def test_keep_alive(self):

Expand Down Expand Up @@ -168,6 +170,10 @@ def signed_session(self, session=None):
pass

assert creds.called == 2
assert client._session # Still alive
# Manually close the client in "keep_alive" mode
client.close()
assert client._session is None # Dead

def test_max_retries_on_default_adapter(self):
# max_retries must be applied only on the default adapters of requests
Expand Down

0 comments on commit 4f3f7fc

Please sign in to comment.