From b5bfe1cad90788e6635d00565b1f5f98c3ecd2c5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 6 Jan 2015 17:09:23 +0100 Subject: [PATCH] Fix threading.Condition with monkey-patching For the Python implementation of threading.RLock because the new C implementation of threading.RLock of Python 3.3 is not compatible with eventlet monkey patching. Fix the issue #185. --- eventlet/patcher.py | 7 ++++++ tests/isolated/patcher_threading_condition.py | 23 +++++++++++++++++++ tests/patcher_test.py | 4 ++++ 3 files changed, 34 insertions(+) create mode 100644 tests/isolated/patcher_threading_condition.py diff --git a/eventlet/patcher.py b/eventlet/patcher.py index 9c94b60..eb09f9a 100644 --- a/eventlet/patcher.py +++ b/eventlet/patcher.py @@ -304,6 +304,13 @@ def monkey_patch(**on): # importlib must use real thread locks, not eventlet.Semaphore importlib._bootstrap._thread = thread + # Issue #185: Since Python 3.3, threading.RLock is implemented in C and + # so call a C function to get the thread identifier, instead of calling + # threading.get_ident(). Force the Python implementation of RLock which + # calls threading.get_ident() and so is compatible with eventlet. + import threading + threading.RLock = threading._PyRLock + def is_monkey_patched(module): """Returns True if the given module is monkeypatched currently, False if diff --git a/tests/isolated/patcher_threading_condition.py b/tests/isolated/patcher_threading_condition.py new file mode 100644 index 0000000..5bce0f2 --- /dev/null +++ b/tests/isolated/patcher_threading_condition.py @@ -0,0 +1,23 @@ +# Issue #185: test threading.Condition with monkey-patching +import eventlet + +# no standard tests in this file, ignore +__test__ = False + + +if __name__ == '__main__': + eventlet.monkey_patch() + + import threading + + def func(c): + with c: + c.notify() + + c = threading.Condition() + with c: + t = threading.Thread(target=func, args=(c,)) + t.start() + c.wait() + + print('pass') diff --git a/tests/patcher_test.py b/tests/patcher_test.py index cd6296f..2a42e3c 100644 --- a/tests/patcher_test.py +++ b/tests/patcher_test.py @@ -498,3 +498,7 @@ t2.join() def test_importlib_lock(): tests.run_isolated('patcher_importlib_lock.py') + + +def test_threading_condition(): + tests.run_isolated('patcher_threading_condition.py')