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

AsyncModbusTcpClient ignores reconnect_delay == 0 #1203

Closed
Cavaler opened this issue Nov 28, 2022 · 3 comments
Closed

AsyncModbusTcpClient ignores reconnect_delay == 0 #1203

Cavaler opened this issue Nov 28, 2022 · 3 comments

Comments

@Cavaler
Copy link
Contributor

Cavaler commented Nov 28, 2022

pymodbus 3.0.2

Documentation says:

reconnect_delay doubles automatically with each unsuccessful connect. Set reconnect_delay=0 to avoid automatic reconnection.

However, creating AsyncModbusTcpClient(reconnect_delay=0) just causes it to reconnect immediately.

I would suggest checking for delay_ms in protocol_lost_connection() instead of host. Same in connect() on error.
And also set delay_ms to 0 in close(), this would allow calling connect() again on previously close()'s client.

@Cavaler
Copy link
Contributor Author

Cavaler commented Nov 28, 2022

--- tcp.old	2022-11-28 12:22:03.442917900 +0200
+++ tcp.py	2022-11-28 18:58:22.041898200 +0200
@@ -69,7 +69,7 @@
         """Stop client."""
 
         # prevent reconnect.
-        self.params.host = None
+        self.delay_ms = 0
 
         if self.connected and self.protocol and self.protocol.transport:
             self.protocol.transport.close()
@@ -104,7 +104,8 @@
         except Exception as exc:  # pylint: disable=broad-except
             txt = f"Failed to connect: {exc}"
             _logger.warning(txt)
-            asyncio.ensure_future(self._reconnect())
+            if self.delay_ms > 0:
+                asyncio.ensure_future(self._reconnect())
         else:
             txt = f"Connected to {self.params.host}:{self.params.port}."
             _logger.info(txt)
@@ -130,7 +131,7 @@
 
         self.connected = False
         self.protocol = None
-        if self.params.host:
+        if self.delay_ms > 0:
             asyncio.ensure_future(self._reconnect())
 
     async def _reconnect(self):

@janiversen
Copy link
Collaborator

Nice enhancement, would you mind submitting a pull request ?

@janiversen
Copy link
Collaborator

Thanks again for the PR, its merged to dev, and will be available in version 3.1

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants