Commit 8294f8a5 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Fix for locking processes due to race conditions.

parent 1e39f7ad
...@@ -7,6 +7,7 @@ from redis.exceptions import ConnectionError as RedisConnectionError ...@@ -7,6 +7,7 @@ from redis.exceptions import ConnectionError as RedisConnectionError
import logging import logging
import functools import functools
import re import re
import random
from ..misc.logging import GMS_logger from ..misc.logging import GMS_logger
from ..options.config import GMS_config as CFG from ..options.config import GMS_config as CFG
...@@ -34,10 +35,10 @@ class MultiSlotLock(Lock): ...@@ -34,10 +35,10 @@ class MultiSlotLock(Lock):
if allowed_threads and redis_conn: if allowed_threads and redis_conn:
logged = False logged = False
while True: while True:
time.sleep(random.uniform(0, 0.5)) # avoids race conditions in case multiple tasks are waiting
name_free_slot = self.get_free_slot_name() name_free_slot = self.get_free_slot_name()
if not name_free_slot:
time.sleep(0.2)
if not name_free_slot:
if not logged: if not logged:
self.logger.info("Waiting for free '%s' lock." % self.name) self.logger.info("Waiting for free '%s' lock." % self.name)
logged = True logged = True
...@@ -68,7 +69,9 @@ class MultiSlotLock(Lock): ...@@ -68,7 +69,9 @@ class MultiSlotLock(Lock):
while not self._acquired: while not self._acquired:
try: try:
# print('Trying to acquire %s.' % self.final_name.split('GMS_%s__' % CFG.ID)[1])
self._acquired = super(MultiSlotLock, self).acquire(blocking=blocking, timeout=timeout) self._acquired = super(MultiSlotLock, self).acquire(blocking=blocking, timeout=timeout)
# print("Acquired lock '%s'." % self.final_name.split('GMS_%s__' % CFG.ID)[1])
except AlreadyAcquired: except AlreadyAcquired:
# this happens in case the lock has already been acquired by another instance of MultiSlotLock due # this happens in case the lock has already been acquired by another instance of MultiSlotLock due
# to a race condition (time gap between finding the free slot and the call of self.acquire()) # to a race condition (time gap between finding the free slot and the call of self.acquire())
...@@ -76,6 +79,8 @@ class MultiSlotLock(Lock): ...@@ -76,6 +79,8 @@ class MultiSlotLock(Lock):
self.__init__(self.name, allowed_threads=self.allowed_threads, logger=self.logger, self.__init__(self.name, allowed_threads=self.allowed_threads, logger=self.logger,
**self.kwargs) **self.kwargs)
# print(self.final_name.split('GMS_%s__' % CFG.ID)[1], self._acquired)
if self._acquired: if self._acquired:
self.logger.info("Acquired lock '%s'." % self.final_name.split('GMS_%s__' % CFG.ID)[1]) self.logger.info("Acquired lock '%s'." % self.final_name.split('GMS_%s__' % CFG.ID)[1])
else: else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment