From 1738be273f0fb118d2b1a7ae4d94514754ce8846 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Fri, 6 Oct 2023 23:13:37 +0200 Subject: [PATCH] Reconect_delay == 0, do not reconnect. (#1795) --- pymodbus/client/base.py | 3 --- pymodbus/client/serial.py | 4 ---- pymodbus/client/tcp.py | 5 ----- pymodbus/client/tls.py | 5 ----- pymodbus/client/udp.py | 4 ---- pymodbus/transport/transport.py | 6 +++++- test/sub_transport/test_comm.py | 2 ++ 7 files changed, 7 insertions(+), 22 deletions(-) diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index e89078ea8..8f9ff853b 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -112,8 +112,6 @@ def __init__( # pylint: disable=too-many-arguments self.params.close_comm_on_error = bool(close_comm_on_error) self.params.strict = bool(strict) self.params.broadcast_enable = bool(broadcast_enable) - self.params.reconnect_delay = int(reconnect_delay) - self.reconnect_delay_max = int(reconnect_delay_max) self.on_reconnect_callback = on_reconnect_callback self.retry_on_empty: int = 0 self.no_resend_on_retry = no_resend_on_retry @@ -124,7 +122,6 @@ def __init__( # pylint: disable=too-many-arguments self.transaction = DictTransactionManager( self, retries=retries, retry_on_empty=retry_on_empty, **kwargs ) - self.reconnect_delay = self.params.reconnect_delay self.reconnect_delay_current = self.params.reconnect_delay self.use_udp = False self.state = ModbusTransactionState.IDLE diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index b9eacc85b..3f92858b3 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -75,11 +75,7 @@ def connected(self): async def connect(self) -> bool: """Connect Async client.""" - # if reconnect_delay_current was set to 0 by close(), we need to set it back again - # so this instance will work self.reset_delay() - - # force reconnect if required: Log.debug("Connecting to {}.", self.comm_params.host) return await self.transport_connect() diff --git a/pymodbus/client/tcp.py b/pymodbus/client/tcp.py index c964db342..f73769539 100644 --- a/pymodbus/client/tcp.py +++ b/pymodbus/client/tcp.py @@ -60,12 +60,7 @@ def __init__( async def connect(self) -> bool: """Initiate connection to start client.""" - - # if reconnect_delay_current was set to 0 by close(), we need to set it back again - # so this instance will work self.reset_delay() - - # force reconnect if required: Log.debug( "Connecting to {}:{}.", self.comm_params.host, diff --git a/pymodbus/client/tls.py b/pymodbus/client/tls.py index f70be6c09..b823745ea 100644 --- a/pymodbus/client/tls.py +++ b/pymodbus/client/tls.py @@ -67,12 +67,7 @@ def __init__( async def connect(self) -> bool: """Initiate connection to start client.""" - - # if reconnect_delay_current was set to 0 by close(), we need to set it back again - # so this instance will work self.reset_delay() - - # force reconnect if required: Log.debug( "Connecting to {}:{}.", self.comm_params.host, diff --git a/pymodbus/client/udp.py b/pymodbus/client/udp.py index 0cef556c5..d0fa84da5 100644 --- a/pymodbus/client/udp.py +++ b/pymodbus/client/udp.py @@ -66,11 +66,7 @@ async def connect(self) -> bool: :meta private: """ - # if reconnect_delay_current was set to 0 by close(), we need to set it back again - # so this instance will work self.reset_delay() - - # force reconnect if required: Log.debug( "Connecting to {}:{}.", self.comm_params.host, diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 745cbfae3..297272b96 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -298,7 +298,11 @@ def connection_lost(self, reason: Exception): return Log.debug("Connection lost {} due to {}", self.comm_params.comm_name, reason) self.transport_close(intern=True) - if not self.is_server and not self.listener: + if ( + not self.is_server + and not self.listener + and self.comm_params.reconnect_delay + ): self.reconnect_task = asyncio.create_task(self.do_reconnect()) self.callback_disconnected(reason) diff --git a/test/sub_transport/test_comm.py b/test/sub_transport/test_comm.py index 4fc3f8ac7..87cb964a3 100644 --- a/test/sub_transport/test_comm.py +++ b/test/sub_transport/test_comm.py @@ -136,6 +136,7 @@ async def test_connected(self, client, server, use_comm_type): ) async def test_connected_multiple(self, client, server): """Test connection and data exchange.""" + client.comm_params.reconnect_delay = 0.0 assert await server.transport_listen() assert await client.transport_connect() await asyncio.sleep(0.5) @@ -174,6 +175,7 @@ async def test_connected_multiple(self, client, server): assert server2_connected.recv_buffer == test2_data + test_data client2.transport_close() server.transport_close() + await asyncio.sleep(0.5) assert not server.active_connections