Skip to content

Commit ed7855e

Browse files
committed
Locks release on exception
If a lock is acquired using a `with` block, and an exception occurs within that block, it should still be released. This commit uses a `try:` `finally:` structure to do this.
1 parent cdeb2d0 commit ed7855e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/labthings/sync/lock.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ def _owner(self):
5353
@contextmanager
5454
def __call__(self, timeout=sentinel, blocking: bool = True):
5555
result = self.acquire(timeout=timeout, blocking=blocking)
56-
yield result
57-
if result:
58-
self.release()
56+
try:
57+
yield result
58+
finally:
59+
if result:
60+
self.release()
5961

6062
def locked(self):
6163
""" """
@@ -122,9 +124,11 @@ def _owner(self):
122124
@contextmanager
123125
def __call__(self, timeout=sentinel, blocking: bool = True):
124126
result = self.acquire(timeout=timeout, blocking=blocking)
125-
yield result
126-
if result:
127-
self.release()
127+
try:
128+
yield result
129+
finally:
130+
if result:
131+
self.release()
128132

129133
def acquire(self, blocking: bool = True, timeout=sentinel):
130134
"""

0 commit comments

Comments
 (0)