Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve transport close() problem (regression). #2098

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pymodbus/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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**).
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pymodbus/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 14 additions & 9 deletions pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down