From: Victor Stinner Date: Sun, 24 Apr 2011 21:41:33 +0000 (+0200) Subject: Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the X-Git-Tag: v3.3.0a1~2518 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c2824d41c32c3be5f100acdb1ff9f71ba7336b60;p=python Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the lock was not acquired. --- diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index ff30d41fb8..3ed61f3b93 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -247,6 +247,7 @@ class RLockTests(BaseLockTests): # Cannot release an unacquired lock lock = self.locktype() self.assertRaises(RuntimeError, lock.release) + self.assertRaises(RuntimeError, lock._release_save) lock.acquire() lock.acquire() lock.release() @@ -254,6 +255,7 @@ class RLockTests(BaseLockTests): lock.release() lock.release() self.assertRaises(RuntimeError, lock.release) + self.assertRaises(RuntimeError, lock._release_save) def test_different_thread(self): # Cannot release from a different thread diff --git a/Lib/threading.py b/Lib/threading.py index eb3cb626c3..28c2146671 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -157,6 +157,8 @@ class _RLock(_Verbose): def _release_save(self): if __debug__: self._note("%s._release_save()", self) + if self._count == 0: + raise RuntimeError("cannot release un-acquired lock") count = self._count self._count = 0 owner = self._owner diff --git a/Misc/NEWS b/Misc/NEWS index 8aac765c92..62aaea4dcb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -113,6 +113,9 @@ Core and Builtins Library ------- +- Issue #11915: threading.RLock()._release_save() raises a RuntimeError if the + lock was not acquired. + - Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor of 5 to 10. Initial patch by Jonas H. diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 914d671d6a..1aee77b5f7 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -414,6 +414,12 @@ rlock_release_save(rlockobject *self) long owner; unsigned long count; + if (self->rlock_count == 0) { + PyErr_SetString(PyExc_RuntimeError, + "cannot release un-acquired lock"); + return NULL; + } + owner = self->rlock_owner; count = self->rlock_count; self->rlock_count = 0;