From 600f634f9a3425618323ab7adfe32e41397f913a Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 29 Jul 2020 15:58:41 -0700 Subject: [PATCH 1/3] PYTHON-2334 Add regression test for gevent.Timeout compatibility --- test/test_client.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/test_client.py b/test/test_client.py index 5333fc35ff..d0a6cc5afd 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -1944,6 +1944,36 @@ def poller(): task.kill() self.assertTrue(task.dead) + def test_gevent_timeout(self): + if not gevent_monkey_patched(): + raise SkipTest("Must be running monkey patched by gevent") + from gevent import spawn, Timeout + client = rs_or_single_client(maxPoolSize=1) + coll = client.test.test + coll.insert_one({}) + + def contentious_task(): + # The 10 second timeout causes this test to fail without blocking + # forever if a bug like PYTHON-2334 is reintroduced. + with Timeout(10): + coll.find_one({'$where': delay(1)}) + + def timeout_task(): + with Timeout(.5): + try: + coll.find_one({}) + except Timeout: + pass + + ct = spawn(contentious_task) + tt = spawn(timeout_task) + tt.join(15) + ct.join(15) + self.assertTrue(tt.dead) + self.assertTrue(ct.dead) + self.assertIsNone(tt.get()) + self.assertIsNone(ct.get()) + if __name__ == "__main__": unittest.main() From abb415e142fcf5bc31bc59a86367e6754a4d63bb Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 29 Jul 2020 16:01:05 -0700 Subject: [PATCH 2/3] PYTHON-2334 Use with statement in Semaphore.release --- pymongo/thread_util.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pymongo/thread_util.py b/pymongo/thread_util.py index 0cf0a127f2..3dac4e25fa 100644 --- a/pymongo/thread_util.py +++ b/pymongo/thread_util.py @@ -60,10 +60,9 @@ def acquire(self, blocking=True, timeout=None): __enter__ = acquire def release(self): - self._cond.acquire() - self._value = self._value + 1 - self._cond.notify() - self._cond.release() + with self._cond: + self._value = self._value + 1 + self._cond.notify() def __exit__(self, t, v, tb): self.release() From 852381ffca99fc016a514c190517e24bc24ce85a Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 29 Jul 2020 17:38:28 -0700 Subject: [PATCH 3/3] Use the pymongo_test database to avoid clashing with test_send_hedge --- test/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_client.py b/test/test_client.py index d0a6cc5afd..19ea1375c2 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -1949,7 +1949,7 @@ def test_gevent_timeout(self): raise SkipTest("Must be running monkey patched by gevent") from gevent import spawn, Timeout client = rs_or_single_client(maxPoolSize=1) - coll = client.test.test + coll = client.pymongo_test.test coll.insert_one({}) def contentious_task():