Skip to content

Commit

Permalink
Workaround for python2.7 not supporting lock timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
phodge authored and jamielennox committed Aug 30, 2022
1 parent b0a92ca commit 9264284
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions requests_mock/mocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.

import contextlib
import functools
import sys
import threading
import types

Expand All @@ -36,6 +38,22 @@
_send_lock = threading.RLock()


@contextlib.contextmanager
def threading_rlock(timeout):
kwargs = {}
if sys.version_info.major >= 3:
# python2 doesn't support the timeout argument
kwargs['timeout'] = timeout

if not _send_lock.acquire(**kwargs):
raise Exception("Could not acquire threading lock - possible deadlock scenario")

try:
yield
finally:
_send_lock.release()


def _is_bound_method(method):
"""
bound_method 's self is a obj
Expand Down Expand Up @@ -137,10 +155,7 @@ def _fake_send(session, request, **kwargs):
# are multiple threads running - one thread could restore the
# original get_adapter() just as a second thread is about to
# execute _original_send() below
if not _send_lock.acquire(timeout=10):
raise Exception("Could not acquire threading lock - possible deadlock scenario")

try:
with threading_rlock(timeout=10):
# mock get_adapter
#
# NOTE(phodge): requests.Session.send() is actually
Expand Down Expand Up @@ -175,8 +190,6 @@ def _fake_send(session, request, **kwargs):
finally:
# restore get_adapter
_set_method(session, "get_adapter", self._last_get_adapter)
finally:
_send_lock.release()

# if we are here it means we must run the real http request
# Or, with nested mocks, to the parent mock, that is why we use
Expand Down

0 comments on commit 9264284

Please sign in to comment.