diff --git a/pymodbus/client/base.py b/pymodbus/client/base.py index 50456908f..972ec7ef6 100644 --- a/pymodbus/client/base.py +++ b/pymodbus/client/base.py @@ -124,12 +124,12 @@ def register(self, custom_response_class: ModbusResponse) -> None: """ self.framer.decoder.register(custom_response_class) - def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override] + def close(self, reconnect: bool = False) -> None: """Close connection.""" if reconnect: self.connection_lost(asyncio.TimeoutError("Server not responding")) else: - super().close(intern) + super().close() def idle_time(self) -> float: """Time before initiating next transaction (call **sync**). diff --git a/pymodbus/client/serial.py b/pymodbus/client/serial.py index 98e1bffbf..665f032fb 100644 --- a/pymodbus/client/serial.py +++ b/pymodbus/client/serial.py @@ -101,9 +101,9 @@ async def connect(self) -> bool: Log.debug("Connecting to {}.", self.comm_params.host) return await self.base_connect() - def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override] + def close(self, reconnect: bool = False) -> None: """Close connection.""" - super().close(reconnect=reconnect, intern=intern) + super().close(reconnect=reconnect) class ModbusSerialClient(ModbusBaseSyncClient): diff --git a/pymodbus/client/tcp.py b/pymodbus/client/tcp.py index 6a5747fac..cc6ce6c84 100644 --- a/pymodbus/client/tcp.py +++ b/pymodbus/client/tcp.py @@ -87,9 +87,9 @@ async def connect(self) -> bool: ) return await self.base_connect() - def close(self, reconnect: bool = False, intern: bool = False) -> None: # type: ignore[override] + def close(self, reconnect: bool = False) -> None: """Close connection.""" - super().close(reconnect=reconnect, intern=intern) + super().close(reconnect=reconnect) class ModbusTcpClient(ModbusBaseSyncClient): diff --git a/pymodbus/transport/transport.py b/pymodbus/transport/transport.py index 1d7f60d80..e643f0185 100644 --- a/pymodbus/transport/transport.py +++ b/pymodbus/transport/transport.py @@ -260,7 +260,7 @@ async def listen(self) -> bool: self.transport = self.transport[0] except OSError as exc: Log.warning("Failed to start server {}", exc) - # self.close(intern=True) + self.__close() return False return True @@ -285,7 +285,7 @@ def connection_lost(self, reason: Exception | None) -> None: if not self.transport or self.is_closing: return Log.debug("Connection lost {} due to {}", self.comm_params.comm_name, reason) - self.close(intern=True) + self.__close() if ( not self.is_server and not self.listener @@ -390,16 +390,11 @@ def send(self, data: bytes, addr: tuple | None = None) -> None: else: self.transport.write(data) # type: ignore[attr-defined] - def close(self, intern: bool = False, reconnect: bool = False) -> None: - """Close connection. + def __close(self, reconnect: bool = False) -> None: + """Close connection (internal). - :param intern: (default false), True if called internally (temporary close) :param reconnect: (default false), try to reconnect """ - if self.is_closing: - return - if not intern: - self.is_closing = True if self.transport: self.transport.close() self.transport = None # type: ignore[assignment] @@ -418,6 +413,16 @@ def close(self, intern: bool = False, reconnect: bool = False) -> None: if self.listener: self.listener.active_connections.pop(self.unique_id) + def close(self, reconnect: bool = False) -> None: + """Close connection (external). + + :param reconnect: (default false), try to reconnect + """ + if self.is_closing: + return + self.is_closing = True + self.__close(reconnect=reconnect) + def reset_delay(self) -> None: """Reset wait time before next reconnect to minimal period.""" self.reconnect_delay_current = self.comm_params.reconnect_delay or 0.0