From 26f6feebb7971db6d1867c5632e218e27dc1aa35 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 6 May 2020 00:24:00 +0200 Subject: [PATCH] Implemented retry+sleep mechanism in Switchbot plugin in case of errors This should prevent race conditions in case of command timeout+quick disconnection and retry that can result in race conditions and double free corruptions. --- platypush/plugins/switch/switchbot/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/platypush/plugins/switch/switchbot/__init__.py b/platypush/plugins/switch/switchbot/__init__.py index 60690f0623..ac33444286 100644 --- a/platypush/plugins/switch/switchbot/__init__.py +++ b/platypush/plugins/switch/switchbot/__init__.py @@ -1,4 +1,5 @@ import enum +import time from platypush.message.response.bluetooth import BluetoothScanResponse from platypush.plugins import action @@ -65,8 +66,18 @@ class SwitchSwitchbotPlugin(SwitchPlugin, BluetoothBlePlugin): def _run(self, device: str, command: Command): if device in self.configured_devices_by_name: device = self.configured_devices_by_name[device] + n_tries = 1 + + try: + self.write(device, command.value, handle=self.handle, channel_type='random', binary=True) + except Exception as e: + self.logger.exception(e) + n_tries -= 1 + + if n_tries == 0: + raise e + time.sleep(5) - self.write(device, command.value, handle=self.handle, channel_type='random', binary=True) return self.status(device) @action