Skip to content

Commit

Permalink
Locks release on exception
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rwb27 committed Aug 23, 2021
1 parent cdeb2d0 commit ed7855e
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/labthings/sync/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def _owner(self):
@contextmanager
def __call__(self, timeout=sentinel, blocking: bool = True):
result = self.acquire(timeout=timeout, blocking=blocking)
yield result
if result:
self.release()
try:
yield result
finally:
if result:
self.release()

def locked(self):
""" """
Expand Down Expand Up @@ -122,9 +124,11 @@ def _owner(self):
@contextmanager
def __call__(self, timeout=sentinel, blocking: bool = True):
result = self.acquire(timeout=timeout, blocking=blocking)
yield result
if result:
self.release()
try:
yield result
finally:
if result:
self.release()

def acquire(self, blocking: bool = True, timeout=sentinel):
"""
Expand Down

0 comments on commit ed7855e

Please sign in to comment.