Merge pull request #416 from Unrud/patch-13

Always release lock
This commit is contained in:
Guillaume Ayoub 2016-05-30 11:08:48 +02:00
commit b0ed18af05

View File

@ -578,23 +578,26 @@ class Collection(BaseCollection):
except OSError: except OSError:
cls.logger.debug("Locking not supported") cls.logger.debug("Locking not supported")
cls._lock_file_locked = True cls._lock_file_locked = True
yield try:
with cls._lock: yield
if mode == "r": finally:
cls._readers -= 1 with cls._lock:
else: if mode == "r":
cls._writer = False cls._readers -= 1
if cls._readers == 0: else:
if os.name == "nt": cls._writer = False
handle = msvcrt.get_osfhandle(cls._lock_file.fileno()) if cls._readers == 0:
overlapped = Overlapped() if os.name == "nt":
if not unlock_file_ex(handle, 0, 1, 0, overlapped): handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
cls.logger.debug("Unlocking not supported") overlapped = Overlapped()
elif os.name == "posix": if not unlock_file_ex(handle, 0, 1, 0, overlapped):
try: cls.logger.debug("Unlocking not supported")
fcntl.lockf(cls._lock_file.fileno(), fcntl.LOCK_UN) elif os.name == "posix":
except OSError: try:
cls.logger.debug("Unlocking not supported") fcntl.lockf(cls._lock_file.fileno(),
cls._lock_file_locked = False fcntl.LOCK_UN)
if cls._waiters: except OSError:
cls._waiters[0].notify() cls.logger.debug("Unlocking not supported")
cls._lock_file_locked = False
if cls._waiters:
cls._waiters[0].notify()