From c98c2f63df4f3b9fe49381c649b049836be05edf Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 30 May 2024 23:12:18 +0200 Subject: [PATCH] Delay callback_disconnected() until reconnect_delay_max. --- pymodbus/transport/transport.py | 9 +++++---- test/transport/test_reconnect.py | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 265d98e1d..e6d32cb9e 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -473,10 +473,11 @@ async def do_reconnect(self) -> None: await asyncio.sleep(self.reconnect_delay_current) if await self.connect(): break - self.reconnect_delay_current = min( - 2 * self.reconnect_delay_current, - self.comm_params.reconnect_delay_max, - ) + self.reconnect_delay_current = 2 * self.reconnect_delay_current + if self.reconnect_delay_current > self.comm_params.reconnect_delay_max: + self.callback_disconnected(Exception("Retry connect timeout.")) + self.reconnect_delay_current = self.comm_params.reconnect_delay_max + break except asyncio.CancelledError: pass self.reconnect_task = None diff --git a/test/transport/test_reconnect.py b/test/transport/test_reconnect.py index 31d9960c6..e97a62119 100644 --- a/test/transport/test_reconnect.py +++ b/test/transport/test_reconnect.py @@ -52,9 +52,9 @@ async def test_multi_reconnect_call(self, client): assert client.reconnect_delay_current == client.comm_params.reconnect_delay * 2 await asyncio.sleep(client.reconnect_delay_current * 1.8) assert client.call_create.call_count == 3 - assert client.reconnect_delay_current == client.comm_params.reconnect_delay_max + assert client.reconnect_delay_current >= client.comm_params.reconnect_delay_max await asyncio.sleep(client.reconnect_delay_current * 1.8) - assert client.call_create.call_count >= 4 + assert client.call_create.call_count >= 3 assert client.reconnect_delay_current == client.comm_params.reconnect_delay_max client.close()