Timeouts behave differently depending on host?

84 views
Skip to first unread message

Alex Railean

unread,
Jul 12, 2021, 12:59:26 PM7/12/21
to pymodbus
Hi everyone,

I'm dealing with a timeout-related issue that I cannot yet explain, I hope you can provide some troubleshooting hints.

- I'm using pymodbus 2.5.2
- with the asyncio TCP client
- and Python 3.8.10 on Linux

Here's a minimal example that replicates the problem:

```
import asyncio
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient as ModbusClient
from pymodbus.client.asynchronous import schedulers

event_loop = asyncio.get_event_loop()
event_loop.set_debug(True)

print('Before')
_, client = ModbusClient(schedulers.ASYNC_IO, host="192.168.1.230", loop=event_loop, timeout=10)
print('After')

async def read():
    if client.connected:
        result = await client.protocol.read_holding_registers(address=8001, count=1)
    print('Read %s', result)
    await asyncio.sleep(10)

event_loop.create_task(read())
event_loop.run_forever()
```


The address 192.168.1.230 doesn't exist on my network, so I am expecting the program to raise a timeout exception after 10 seconds (as defined in the constructor of ModbusClient).

However, you can see in the output that the timeout occurred after 2m10s:

18:45 $ python test.py 
2021-07-12 18:46:16,403 asyncio - Using selector: EpollSelector
2021-07-12 18:46:16,404  root - Before
2021-07-12 18:46:16,410 pymodbus.client.asynchronous.async_io - Connecting to 192.168.1.230:502.
2021-07-12 18:46:16,410 pymodbus.client.asynchronous.async_io - Connecting.
2021-07-12 18:48:26,927 pymodbus.client.asynchronous.async_io - Failed to connect: [Errno 110] Connect call failed ('192.168.1.230', 502)
2021-07-12 18:48:26,927 pymodbus.client.asynchronous.async_io - Waiting 100 ms before next connection attempt.
2021-07-12 18:48:26,928  root - After
2021-07-12 18:48:27,029 pymodbus.client.asynchronous.async_io - Connecting.

You can also try it with google.com or any other real address, where there's no listening Modbus server at the given port.

Moreover, if I replace the host address with 127.0.0.1 (where I do not run a Modbus server), the timeout error occurs right away, before the 10s interval passes.

This behaviour occurs consistently, no matter how many times I run the program


I'm trying to understand
1. why the timeout value is not obeyed
2. why the program behaves differently with these 2 addresses


Any tips will be greatly appreciated,
Alex
Reply all
Reply to author
Forward
0 new messages