Repository: tiagocoutinho/modbus-proxy (language: python)

Issue:
Crashing very often
* ModBus TCP proxy version: latest
* Python version: 3.5.3
* Operating System: Debian 9.13

### Description

It crashing very often. Now I made a script to start it again, so you can see how often starting it again because of crash:
2021-12-08 20:21:16,260     INFO modbus-proxy: Starting...
2021-12-09 04:02:58,291     INFO modbus-proxy: Starting...
2021-12-09 11:22:36,333     INFO modbus-proxy: Starting...
2021-12-09 11:23:09,014     INFO modbus-proxy: Starting...
2021-12-09 11:23:43,948     INFO modbus-proxy: Starting...
2021-12-09 11:23:53,006     INFO modbus-proxy: Starting...
2021-12-09 11:24:32,073     INFO modbus-proxy: Starting...
2021-12-09 11:25:01,598     INFO modbus-proxy: Starting...
2021-12-09 11:26:02,074     INFO modbus-proxy: Starting...
2021-12-09 11:26:10,315     INFO modbus-proxy: Starting...
2021-12-09 11:26:19,340     INFO modbus-proxy: Starting...
2021-12-09 11:27:28,007     INFO modbus-proxy: Starting...
2021-12-09 11:28:02,084     INFO modbus-proxy: Starting...
2021-12-09 11:28:36,598     INFO modbus-proxy: Starting...
2021-12-09 11:28:45,659     INFO modbus-proxy: Starting...
2021-12-09 11:29:32,091     INFO modbus-proxy: Starting...
2021-12-09 11:29:54,347     INFO modbus-proxy: Starting...
2021-12-09 11:31:02,118     INFO modbus-proxy: Starting...
2021-12-09 11:31:08,986     INFO modbus-proxy: Starting...
2021-12-09 11:32:17,690     INFO modbus-proxy: Starting...
2021-12-09 11:32:26,692     INFO modbus-proxy: Starting...
2021-12-09 11:32:29,820     INFO modbus-proxy: Starting...
2021-12-09 11:33:32,377     INFO modbus-proxy: Starting...
2021-12-09 11:34:02,104     INFO modbus-proxy: Starting...
2021-12-09 11:34:40,983     INFO modbus-proxy: Starting...
2021-12-09 11:34:50,063     INFO modbus-proxy: Starting...
2021-12-09 11:35:32,090     INFO modbus-proxy: Starting...
2021-12-09 11:35:58,748     INFO modbus-proxy: Starting...
2021-12-09 11:37:02,109     INFO modbus-proxy: Starting...
2021-12-09 11:37:05,172     INFO modbus-proxy: Starting...
2021-12-09 11:37:13,413     INFO modbus-proxy: Starting...
2021-12-09 11:38:22,082     INFO modbus-proxy: Starting...
2021-12-09 11:38:31,046     INFO modbus-proxy: Starting...
2021-12-09 11:39:36,747     INFO modbus-proxy: Starting...
2021-12-09 11:40:02,162     INFO modbus-proxy: Starting...
2021-12-09 11:40:45,386     INFO modbus-proxy: Starting...
2021-12-09 11:40:54,435     INFO modbus-proxy: Starting...
2021-12-09 11:41:32,117     INFO modbus-proxy: Starting...
2021-12-09 11:42:03,054     INFO modbus-proxy: Starting...
2021-12-09 11:43:02,114     INFO modbus-proxy: Starting...
2021-12-09 11:43:11,722     INFO modbus-proxy: Starting...
2021-12-09 11:43:20,764     INFO modbus-proxy: Starting...
2021-12-09 11:44:29,422     INFO modbus-proxy: Starting...
2021-12-09 11:45:38,015     INFO modbus-proxy: Starting...
2021-12-09 11:45:47,085     INFO modbus-proxy: Starting...
2021-12-09 11:46:02,148     INFO modbus-proxy: Starting...
2021-12-09 11:46:55,756     INFO modbus-proxy: Starting...
2021-12-09 11:47:32,133     INFO modbus-proxy: Starting...
2021-12-09 11:48:04,427     INFO modbus-proxy: Starting...
2021-12-09 11:48:13,454     INFO modbus-proxy: Starting...
2021-12-09 11:49:02,196     INFO modbus-proxy: Starting...
2021-12-09 11:49:22,140     INFO modbus-proxy: Starting...
2021-12-09 11:50:30,799     INFO modbus-proxy: Starting...
2021-12-09 11:50:39,766     INFO modbus-proxy: Starting...
2021-12-09 11:51:48,471     INFO modbus-proxy: Starting...
2021-12-09 11:52:02,148     INFO modbus-proxy: Starting...
2021-12-09 11:52:57,093     INFO modbus-proxy: Starting...
2021-12-09 11:53:06,117     INFO modbus-proxy: Starting...
2021-12-09 11:53:32,206     INFO modbus-proxy: Starting...
2021-12-09 11:54:14,733     INFO modbus-proxy: Starting...
2021-12-09 11:55:02,164     INFO modbus-proxy: Starting...
2021-12-09 11:55:23,488     INFO modbus-proxy: Starting...
2021-12-09 11:55:32,483     INFO modbus-proxy: Starting...
2021-12-09 11:56:32,122     INFO modbus-proxy: Starting...
2021-12-09 11:56:41,110     INFO modbus-proxy: Starting...
2021-12-09 11:57:49,733     INFO modbus-proxy: Starting...
2021-12-09 11:57:58,758     INFO modbus-proxy: Starting...
2021-12-09 11:58:32,156     INFO modbus-proxy: Starting...
2021-12-09 11:59:07,435     INFO modbus-proxy: Starting...
2021-12-09 12:00:02,185     INFO modbus-proxy: Starting...
2021-12-09 12:00:16,113     INFO modbus-proxy: Starting...
2021-12-09 12:00:25,125     INFO modbus-proxy: Starting...
2021-12-09 12:01:32,162     INFO modbus-proxy: Starting...
2021-12-09 12:01:39,774     INFO modbus-proxy: Starting...
2021-12-09 12:02:45,445     INFO modbus-proxy: Starting...
2021-12-09 12:03:02,144     INFO modbus-proxy: Starting...
2021-12-09 12:03:54,121     INFO modbus-proxy: Starting...
2021-12-09 12:04:03,164     INFO modbus-proxy: Starting...
2021-12-09 12:04:32,154     INFO modbus-proxy: Starting...
2021-12-09 12:05:11,819     INFO modbus-proxy: Starting...
2021-12-09 12:06:02,214     INFO modbus-proxy: Starting...
2021-12-09 12:06:20,492     INFO modbus-proxy: Starting...
2021-12-09 12:06:29,524     INFO modbus-proxy: Starting...
2021-12-09 12:07:32,177     INFO modbus-proxy: Starting...
2021-12-09 12:07:35,298     INFO modbus-proxy: Starting...
2021-12-09 12:07:44,161     INFO modbus-proxy: Starting...
2021-12-09 12:08:49,830     INFO modbus-proxy: Starting...
2021-12-09 12:09:02,218     INFO modbus-proxy: Starting...
2021-12-09 12:09:58,500     INFO modbus-proxy: Starting...
2021-12-09 12:10:07,478     INFO modbus-proxy: Starting...
2021-12-09 12:10:32,196     INFO modbus-proxy: Starting...
2021-12-09 12:11:16,172     INFO modbus-proxy: Starting...
2021-12-09 12:12:02,179     INFO modbus-proxy: Starting...
2021-12-09 12:12:24,808     INFO modbus-proxy: Starting...
2021-12-09 12:12:33,774     INFO modbus-proxy: Starting...
2021-12-09 12:13:32,180     INFO modbus-proxy: Starting...
2021-12-09 12:13:42,512     INFO modbus-proxy: Starting...
2021-12-09 12:14:51,124     INFO modbus-proxy: Starting...
2021-12-09 12:15:00,206     INFO modbus-proxy: Starting...
2021-12-09 12:16:08,865     INFO modbus-proxy: Starting...
2021-12-09 12:16:32,203     INFO modbus-proxy: Starting...
2021-12-09 12:17:17,522     INFO modbus-proxy: Starting...
2021-12-09 12:17:26,523     INFO modbus-proxy: Starting...
2021-12-09 12:18:02,176     INFO modbus-proxy: Starting...
2021-12-09 12:18:35,207     INFO modbus-proxy: Starting...
2021-12-09 12:19:32,222     INFO modbus-proxy: Starting...
2021-12-09 12:19:43,851     INFO modbus-proxy: Starting...
2021-12-09 12:19:52,828     INFO modbus-proxy: Starting...
2021-12-09 12:21:01,567     INFO modbus-proxy: Starting...
2021-12-09 12:22:10,245     INFO modbus-proxy: Starting...
2021-12-09 12:22:19,267     INFO modbus-proxy: Starting...
2021-12-09 12:22:32,184     INFO modbus-proxy: Starting...
2021-12-09 12:23:27,883     INFO modbus-proxy: Starting...
2021-12-09 12:24:02,198     INFO modbus-proxy: Starting...
2021-12-09 12:24:36,555     INFO modbus-proxy: Starting...
2021-12-09 12:24:45,609     INFO modbus-proxy: Starting...
2021-12-09 12:25:32,180     INFO modbus-proxy: Starting...
2021-12-09 12:25:54,219     INFO modbus-proxy: Starting...
2021-12-09 12:27:02,271     INFO modbus-proxy: Starting...
2021-12-09 12:27:08,868     INFO modbus-proxy: Starting...
2021-12-09 12:28:17,601     INFO modbus-proxy: Starting...
2021-12-09 12:28:26,555     INFO modbus-proxy: Starting...
2021-12-09 12:28:29,680     INFO modbus-proxy: Starting...
2021-12-09 12:29:32,224     INFO modbus-proxy: Starting...
2021-12-09 12:30:02,264     INFO modbus-proxy: Starting...
2021-12-09 12:30:40,822     INFO modbus-proxy: Starting...
2021-12-09 12:30:49,944     INFO modbus-proxy: Starting...
2021-12-09 12:31:32,258     INFO modbus-proxy: Starting...
2021-12-09 12:31:58,604     INFO modbus-proxy: Starting...
2021-12-09 12:33:02,209     INFO modbus-proxy: Starting...
2021-12-09 12:33:05,323     INFO modbus-proxy: Starting...
2021-12-09 12:33:13,265     INFO modbus-proxy: Starting...
2021-12-09 12:34:21,871     INFO modbus-proxy: Starting...
2021-12-09 12:34:30,975     INFO modbus-proxy: Starting...
2021-12-09 12:35:36,600     INFO modbus-proxy: Starting...
2021-12-09 12:36:02,273     INFO modbus-proxy: Starting...
2021-12-09 12:36:45,263     INFO modbus-proxy: Starting...
2021-12-09 12:36:54,230     INFO modbus-proxy: Starting...
2021-12-09 12:37:32,231     INFO modbus-proxy: Starting...
2021-12-09 12:38:02,973     INFO modbus-proxy: Starting...
2021-12-09 12:39:02,257     INFO modbus-proxy: Starting...
2021-12-09 12:39:11,628     INFO modbus-proxy: Starting...
2021-12-09 12:39:20,651     INFO modbus-proxy: Starting...
2021-12-09 12:40:29,347     INFO modbus-proxy: Starting...
2021-12-09 12:41:37,977     INFO modbus-proxy: Starting...
2021-12-09 12:41:46,988     INFO modbus-proxy: Starting...
2021-12-09 12:42:02,242     INFO modbus-proxy: Starting...
2021-12-09 12:42:55,628     INFO modbus-proxy: Starting...
2021-12-09 12:43:32,247     INFO modbus-proxy: Starting...
2021-12-09 12:44:04,218     INFO modbus-proxy: Starting...
2021-12-09 12:44:13,323     INFO modbus-proxy: Starting...
2021-12-09 12:45:02,281     INFO modbus-proxy: Starting...
2021-12-09 12:45:21,940     INFO modbus-proxy: Starting...
2021-12-09 12:46:30,659     INFO modbus-proxy: Starting...
2021-12-09 12:46:39,605     INFO modbus-proxy: Starting...
2021-12-09 12:47:48,326     INFO modbus-proxy: Starting...
2021-12-09 12:48:02,295     INFO modbus-proxy: Starting...
2021-12-09 12:48:56,998     INFO modbus-proxy: Starting...
2021-12-09 12:49:05,953     INFO modbus-proxy: Starting...
2021-12-09 12:49:32,262     INFO modbus-proxy: Starting...
2021-12-09 12:50:14,609     INFO modbus-proxy: Starting...
2021-12-09 12:51:02,280     INFO modbus-proxy: Starting...
2021-12-09 12:51:23,317     INFO modbus-proxy: Starting...
2021-12-09 12:51:32,288     INFO modbus-proxy: Starting...
2021-12-09 12:52:32,246     INFO modbus-proxy: Starting...
2021-12-09 12:52:40,998     INFO modbus-proxy: Starting...
2021-12-09 12:53:49,659     INFO modbus-proxy: Starting...
2021-12-09 12:53:58,645     INFO modbus-proxy: Starting...
2021-12-09 12:54:32,277     INFO modbus-proxy: Starting...
2021-12-09 12:55:07,332     INFO modbus-proxy: Starting...
2021-12-09 12:56:02,271     INFO modbus-proxy: Starting...
2021-12-09 12:56:15,961     INFO modbus-proxy: Starting...
2021-12-09 12:56:25,013     INFO modbus-proxy: Starting...
2021-12-09 12:57:32,272     INFO modbus-proxy: Starting...
2021-12-09 12:57:39,727     INFO modbus-proxy: Starting...
2021-12-09 12:58:45,359     INFO modbus-proxy: Starting...
2021-12-09 12:59:02,260     INFO modbus-proxy: Starting...
2021-12-09 12:59:53,987     INFO modbus-proxy: Starting...
2021-12-09 13:00:03,083     INFO modbus-proxy: Starting...
2021-12-09 13:00:32,285     INFO modbus-proxy: Starting...
2021-12-09 13:01:11,700     INFO modbus-proxy: Starting...
2021-12-09 13:02:02,300     INFO modbus-proxy: Starting...
2021-12-09 13:02:20,279     INFO modbus-proxy: Starting...
2021-12-09 13:02:29,409     INFO modbus-proxy: Starting...
2021-12-09 13:03:32,353     INFO modbus-proxy: Starting...
2021-12-09 13:03:35,427     INFO modbus-proxy: Starting...
2021-12-09 13:03:44,032     INFO modbus-proxy: Starting...
2021-12-09 13:04:49,676     INFO modbus-proxy: Starting...
2021-12-09 13:05:02,297     INFO modbus-proxy: Starting...
2021-12-09 13:05:58,253     INFO modbus-proxy: Starting...
2021-12-09 13:06:07,361     INFO modbus-proxy: Starting...
2021-12-09 13:06:32,284     INFO modbus-proxy: Starting...
2021-12-09 13:07:15,855     INFO modbus-proxy: Starting...
2021-12-09 13:08:02,307     INFO modbus-proxy: Starting...
2021-12-09 13:08:24,442     INFO modbus-proxy: Starting...
2021-12-09 13:08:33,455     INFO modbus-proxy: Starting...
2021-12-09 13:13:39,244     INFO modbus-proxy: Starting...
2021-12-09 13:13:59,325     INFO modbus-proxy: Starting...
2021-12-09 13:14:04,287     INFO modbus-proxy: Starting...
2021-12-09 23:09:40,330     INFO modbus-proxy: Starting...
2021-12-09 23:09:53,638     INFO modbus-proxy: Starting...
2021-12-10 09:03:11,321     INFO modbus-proxy: Starting...

192.168.1.49:1502 Is a SolarEdge inverter!

### What I Did

2021-12-09 04:02:27,102    DEBUG modbus-proxy.ModBus(192.168.1.49:1502): sending b'\x02\xbd\x00\x00\x00\x06\x01\x03\x9c\x87\x00&'
2021-12-09 04:02:27,128    DEBUG modbus-proxy.ModBus(192.168.1.49:1502): received b"\x02\xbd\x00\x00\x00O\x01\x03L\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x0f\xd9\x0f\xd1\x0f\xd9\t5\t\x12\t'\xff\xff\x00\x00\x00\x00\x13\x89\xff\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02&\xa1\xe4\x00\x00\xff\xff\x80\x00\x00\x00\xff\xff\x00\x00\x00\x00\x80\x00\x00\x00\x80\x00\x80\x00\xff\xfe\x00\x02\x00\x00"
2021-12-09 04:02:27,129    DEBUG modbus-proxy.Client(127.0.0.1:48351): sending b"\x02\xbd\x00\x00\x00O\x01\x03L\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x0f\xd9\x0f\xd1\x0f\xd9\t5\t\x12\t'\xff\xff\x00\x00\x00\x00\x13\x89\xff\xfe\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02&\xa1\xe4\x00\x00\xff\xff\x80\x00\x00\x00\xff\xff\x00\x00\x00\x00\x80\x00\x00\x00\x80\x00\x80\x00\xff\xfe\x00\x02\x00\x00"
2021-12-09 04:02:57,103    DEBUG modbus-proxy.Client(127.0.0.1:48351): received b'\x02\xbe\x00\x00\x00\x06\x01\x03\x9c\x87\x00&'
2021-12-09 04:02:57,103    DEBUG modbus-proxy.ModBus(192.168.1.49:1502): sending b'\x02\xbe\x00\x00\x00\x06\x01\x03\x9c\x87\x00&'
2021-12-09 04:02:57,116    ERROR modbus-proxy.ModBus(192.168.1.49:1502): write_read error: ConnectionResetError(104, 'Connection reset by peer')
2021-12-09 04:02:57,116     INFO modbus-proxy.ModBus(192.168.1.49:1502): closing connection...
2021-12-09 04:02:57,116     INFO modbus-proxy.ModBus(192.168.1.49:1502): failed to close: ConnectionResetError(104, 'Connection reset by peer')
2021-12-09 04:02:57,117     INFO modbus-proxy.ModBus(192.168.1.49:1502): connecting to modbus...
2021-12-09 04:02:57,117    ERROR modbus-proxy.ModBus(192.168.1.49:1502): write_read error: CancelledError()
2021-12-09 04:02:57,118     INFO modbus-proxy.Client(127.0.0.1:48351): closing connection...
2021-12-09 04:02:57,118    ERROR modbus-proxy.Client(127.0.0.1:39943): reading error: CancelledError()
2021-12-09 04:02:57,118     INFO modbus-proxy.Client(127.0.0.1:39943): closing connection...
2021-12-09 04:02:57,118     INFO modbus-proxy.Client(127.0.0.1:48351): connection closed
2021-12-09 04:02:57,118     INFO modbus-proxy.Client(127.0.0.1:39943): connection closed
Traceback (most recent call last):
  File "/usr/local/bin/modbus-proxy", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/modbus_proxy.py", line 343, in main
    asyncio.run(run())
  File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
concurrent.futures._base.CancelledError

Thanks


Discussion / hints:
Thanks for the detailed report.

Could this be related with #4?

Try launching with a delay between connection and first request. Example:

```bash
$ modbus-proxy -b tcp://0:5020 --modbus tcp://192.168.1.49:1502 --modbus-connection-time=2
```
Thanks for the answer. My config is now:
```yaml
devices:
- modbus:
    url: 192.168.1.49:1502     # device url (mandatory)
    timeout: 10                # communication timeout (s) (optional, default: 10)
    connection_time: 0.1       # delay after connection (s) (optional, default: 0)
  listen:
    bind: 0:502               # listening address (mandatory)
logging:
  version: 1
  formatters:
    standard:
      format: "%(asctime)s %(levelname)8s %(name)s: %(message)s"
  handlers:
    console:
      class: logging.StreamHandler
      formatter: standard
  root:
    handlers: ['console']
    level: DEBUG
```
So I changed connection_time: 0.1 to connection_time: 2 . Let's ses if it is helping ;) .
Unfortunately I still have the problem :(

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 509a523..7110685 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -10,7 +10,7 @@ jobs:
     strategy:
       matrix:
         os: [ubuntu-latest, macos-latest, windows-latest]
-        python-version: [3.7, 3.8, 3.9, pypy3]
+        python-version: [3.7, 3.8, 3.9]
     steps:
     - name: Checkout
       uses: actions/checkout@v2
diff --git a/src/modbus_proxy.py b/src/modbus_proxy.py
index ffcbbfa..bbf0599 100644
--- a/src/modbus_proxy.py
+++ b/src/modbus_proxy.py
@@ -135,10 +135,6 @@ def __init__(self, config):
         self.server = None
         self.lock = asyncio.Lock()
 
-    async def close(self):
-        await self.stop()
-        await super().close()
-
     @property
     def address(self):
         if self.server is not None:
@@ -166,7 +162,9 @@ async def write_read(self, data, attempts=2):
                     coro = self._write_read(data)
                     return await asyncio.wait_for(coro, self.timeout)
                 except Exception as error:
-                    self.log.error("write_read error: %r", error)
+                    self.log.error(
+                        "write_read error [%s/%s]: %r", i + 1, attempts, error
+                    )
                     await self.close()
 
     async def _write_read(self, data):
@@ -195,6 +193,7 @@ async def stop(self):
         if self.server is not None:
             self.server.close()
             await self.server.wait_closed()
+        await self.close()
 
     async def serve_forever(self):
         if self.server is None:


Test patch:
diff --git a/tests/test_modbus_proxy.py b/tests/test_modbus_proxy.py
index 0aa9e92..6f92e07 100644
--- a/tests/test_modbus_proxy.py
+++ b/tests/test_modbus_proxy.py
@@ -183,7 +183,7 @@ async def test_run(modbus_device):
         await make_request(modbus)
     finally:
         for bridge in ready.data:
-            await bridge.close()
+            await bridge.stop()
         try:
             await task
         except asyncio.CancelledError: