From c7decd81f22a927aec874d40479f0ebdc81d5bd1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 20 Jan 2019 00:29:58 +0100 Subject: [PATCH] Lazy inizialitation for the Redis object in light.hue to prevent race conditions on start/stop animation --- platypush/plugins/light/hue/__init__.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/platypush/plugins/light/hue/__init__.py b/platypush/plugins/light/hue/__init__.py index 526653a37..149bd831b 100644 --- a/platypush/plugins/light/hue/__init__.py +++ b/platypush/plugins/light/hue/__init__.py @@ -575,7 +575,8 @@ class LightHuePlugin(LightPlugin): """ if self.animation_thread and self.animation_thread.is_alive(): - self.redis.rpush(self.ANIMATION_CTRL_QUEUE_NAME, 'STOP') + redis = self._get_redis() + redis.rpush(self.ANIMATION_CTRL_QUEUE_NAME, 'STOP') @action def animate(self, animation, duration=None, @@ -659,7 +660,8 @@ class LightHuePlugin(LightPlugin): def _should_stop(): try: - self.redis.blpop(self.ANIMATION_CTRL_QUEUE_NAME) + redis = self._get_redis() + redis.blpop(self.ANIMATION_CTRL_QUEUE_NAME) return True except QueueTimeoutError: return False @@ -707,10 +709,6 @@ class LightHuePlugin(LightPlugin): self.animation_thread = None self.redis = None - redis_args = get_backend('redis').redis_args - redis_args['socket_timeout'] = transition_seconds - self.redis = Redis(**redis_args) - if groups: groups = [g for g in self.bridge.groups if g.name in groups] lights = lights or [] @@ -725,6 +723,13 @@ class LightHuePlugin(LightPlugin): args=(lights,)) self.animation_thread.start() + def _get_redis(self): + if not self.redis: + redis_args = get_backend('redis').redis_args + redis_args['socket_timeout'] = transition_seconds + self.redis = Redis(**redis_args) + return self.redis + # vim:sw=4:ts=4:et: