Better exception handling in locks
authorJack Miller <jack@codezen.org>
Sun, 14 Sep 2014 22:48:11 +0000 (17:48 -0500)
committerJack Miller <jack@codezen.org>
Sun, 14 Sep 2014 22:48:11 +0000 (17:48 -0500)
canto_next/feed.py
canto_next/rwlock.py

index 70bec6b..bb0764e 100644 (file)
@@ -99,9 +99,10 @@ def wunlock_all():
 def wlock_feeds(fn):
     def _fl(*args):
         wlock_all()
-        r = fn(*args)
-        wunlock_all()
-        return r
+        try:
+            return fn(*args)
+        finally:
+            wunlock_all()
     return _fl
 
 def rlock_all():
@@ -117,9 +118,10 @@ def runlock_all():
 def rlock_feeds(fn):
     def _fl(*args):
         rlock_all()
-        r = fn(*args)
-        runlock_all()
-        return r
+        try:
+            return fn(*args)
+        finally:
+            runlock_all()
     return _fl
 
 def stop_feeds():
index d0fa4d8..7796329 100644 (file)
@@ -107,9 +107,10 @@ def read_lock(lock):
     def _rlock_fn(fn):
         def _rlock(*args, **kwargs):
             lock.acquire_read()
-            r = fn(*args, **kwargs)
-            lock.release_read()
-            return r
+            try:
+                return fn(*args, **kwargs)
+            finally:
+                lock.release_read()
         return _rlock
     return _rlock_fn
 
@@ -117,8 +118,9 @@ def write_lock(lock):
     def _wlock_fn(fn):
         def _wlock(*args, **kwargs):
             lock.acquire_write()
-            r = fn(*args, **kwargs)
-            lock.release_write()
-            return r
+            try:
+                return fn(*args, **kwargs)
+            finally:
+               lock.release_write()
         return _wlock
     return _wlock_fn