From b9b899b747ef62eafdf45400f77f8143dd5aea73 Mon Sep 17 00:00:00 2001 From: Andrew Weiss Date: Tue, 12 Jun 2018 15:14:15 -0700 Subject: [PATCH] requests.Session is not thread-safe, use thread local storage --- rollbar/lib/transport.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/rollbar/lib/transport.py b/rollbar/lib/transport.py index 1f19baaf..e19af625 100644 --- a/rollbar/lib/transport.py +++ b/rollbar/lib/transport.py @@ -1,14 +1,23 @@ import requests +import threading -_session = requests.Session() + +_local = threading.local() + + +def _session(): + if hasattr(_local, 'session'): + return _local.session + _local.session = requests.Session() + return _local.session def post(*args, **kw): - return _session.post(*args, **kw) + return _session().post(*args, **kw) def get(*args, **kw): - return _session.get(*args, **kw) + return _session().get(*args, **kw) __all__ = ['post', 'get']