Skip to content

Commit

Permalink
Fix session initialization for multi-threaded environments
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jan 30, 2019
1 parent 7c79758 commit 5acc9d9
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions stripe/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ class RequestsClient(HTTPClient):
def __init__(self, timeout=80, session=None, **kwargs):
super(RequestsClient, self).__init__(**kwargs)
self._timeout = timeout
self._thread_local.session = session or requests.Session()

def request(self, method, url, headers, post_data=None):
kwargs = {}
Expand All @@ -206,6 +205,9 @@ def request(self, method, url, headers, post_data=None):
if self._proxy:
kwargs["proxies"] = self._proxy

if getattr(self._thread_local, "session", None) is None:
self._thread_local.session = requests.Session()

try:
try:
result = self._thread_local.session.request(
Expand Down Expand Up @@ -288,7 +290,7 @@ def _handle_request_error(self, e):
raise error.APIConnectionError(msg, should_retry=should_retry)

def close(self):
if self._thread_local.session is not None:
if getattr(self._thread_local, "session", None) is not None:
self._thread_local.session.close()


Expand Down

0 comments on commit 5acc9d9

Please sign in to comment.