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

Setting reconnect_delay=0 in AsyncModbusTcpClient causes infinite busy-loops on connection loss #1794

Closed
sthelen-enqs opened this issue Oct 6, 2023 · 5 comments · Fixed by #1795

Comments

@sthelen-enqs
Copy link

Versions

  • Python: 3.11.4
  • OS: Ubuntu 20.04
  • Pymodbus: 3.5.2
  • Modbus Hardware (if used): N/A

Pymodbus Specific

  • Server: tcp - async
  • Client: tcp - async

Description

I tried setting reconnect_delay=0 in the constructor for AsyncModbusTcpClient as documented in https://pymodbus.readthedocs.io/en/latest/source/client.html#pymodbus.client.ModbusBaseClient to prevent the client from reconnecting if the TCP connection drops, but whenever the connection dropped pymodbus would instead busy-loop at 100%CPU forever.

Code and Logs

Minimal Example:

import asyncio

import pymodbus
from pymodbus.client import AsyncModbusTcpClient

pymodbus.pymodbus_apply_logging_config()


async def main():
    client = AsyncModbusTcpClient(host="127.0.0.1", port=5502, reconnect_delay=0)
    r = await client.connect()

    while True:
        if not client.reconnect_task:
            resp = await client.read_input_registers(13, 2, slave=1)
        await asyncio.sleep(5)


if __name__ == "__main__":
    asyncio.run(main())

Logs (repeat ad-infimum):

2023-10-06 17:00:22,639 DEBUG logging:102 Connecting to 127.0.0.1:5502.
2023-10-06 17:00:22,639 DEBUG logging:102 Connecting comm
2023-10-06 17:00:22,640 DEBUG logging:102 Connected to comm
2023-10-06 17:00:22,640 DEBUG logging:102 callback_connected called
2023-10-06 17:00:22,640 DEBUG logging:102 send: 0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x4 0x0 0xd 0x0 0x2
2023-10-06 17:00:22,640 DEBUG logging:102 Adding transaction 1
2023-10-06 17:00:22,640 DEBUG logging:102 recv: 0x0 0x1 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0xe 0x0 0xf old_data:  addr=None
2023-10-06 17:00:22,640 DEBUG logging:102 Processing: 0x0 0x1 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0xe 0x0 0xf
2023-10-06 17:00:22,641 DEBUG logging:102 Factory Response[ReadInputRegistersResponse': 4]
2023-10-06 17:00:22,641 DEBUG logging:102 Getting transaction 1
2023-10-06 17:00:27,646 DEBUG logging:102 send: 0x0 0x2 0x0 0x0 0x0 0x6 0x1 0x4 0x0 0xd 0x0 0x2
2023-10-06 17:00:27,647 DEBUG logging:102 Adding transaction 2
2023-10-06 17:00:27,647 DEBUG logging:102 recv: 0x0 0x2 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0xe 0x0 0xf old_data:  addr=None
2023-10-06 17:00:27,647 DEBUG logging:102 Processing: 0x0 0x2 0x0 0x0 0x0 0x7 0x1 0x4 0x4 0x0 0xe 0x0 0xf
2023-10-06 17:00:27,648 DEBUG logging:102 Factory Response[ReadInputRegistersResponse': 4]
2023-10-06 17:00:27,648 DEBUG logging:102 Getting transaction 2
2023-10-06 17:00:28,814 DEBUG logging:102 -> transport: received eof
2023-10-06 17:00:28,815 DEBUG logging:102 Connection lost comm due to None
2023-10-06 17:00:28,815 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,815 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,816 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,816 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,816 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,817 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,817 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,817 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,818 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,818 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,818 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,819 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,819 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,819 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,820 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,820 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,820 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,821 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,821 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,821 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,822 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,823 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,823 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,823 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,823 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,824 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,824 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,824 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,824 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,825 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,825 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,825 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,826 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,826 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,826 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,826 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,826 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,826 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,827 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,827 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,827 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,827 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,827 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,827 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,828 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,828 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,828 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,828 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,828 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,828 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,828 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,828 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,828 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,829 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,829 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,829 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,829 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,829 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,829 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,830 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,830 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,830 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,830 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,830 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,830 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,830 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)
2023-10-06 17:00:28,830 DEBUG logging:102 Wait comm 0 ms before reconnecting.
2023-10-06 17:00:28,831 DEBUG logging:102 Connecting comm
2023-10-06 17:00:28,831 WARNING logging:108 Failed to connect [Errno 111] Connect call failed ('127.0.0.1', 5502)

For the server I used the following code and then just killed it with ^C after the first few successful reads:

import asyncio

from pymodbus.datastore import (
    ModbusSequentialDataBlock,
    ModbusServerContext,
    ModbusSlaveContext,
)
from pymodbus.server import StartAsyncTcpServer

datablock = ModbusSequentialDataBlock(0, list(range(25)))

ctx = ModbusServerContext(
    slaves=ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock),
    single=True,
)

asyncio.run(
    StartAsyncTcpServer(
        context=ctx, address=("127.0.0.1", 5502), ignore_missing_slaves=True
    )
)
@sthelen-enqs sthelen-enqs changed the title Setting reconnect_delay=0 in AsyncModbusTcpClient constructor causes infinite busy-loops on connection loss Setting reconnect_delay=0 in AsyncModbusTcpClient causes infinite busy-loops on connection loss Oct 6, 2023
@janiversen
Copy link
Collaborator

Sounds like you have found a bug, thanks for reporting it.

I have enough information to debug and hopefully find/repair the problem.

@janiversen
Copy link
Collaborator

Please give your program a go, with this patch installed:

#1795

It should work, but I do not have time to test it thoroughly right now.

@janiversen
Copy link
Collaborator

Sorry I was too fast, this broke something. I will make a new version tomorrow.

@janiversen
Copy link
Collaborator

If you want to test use #1795

It will be part of either 3.5.3 or 3.6.0, but there are no confirmed time at this moment.

@sthelen-enqs
Copy link
Author

Thanks for the quick fix, seems to look good so far in tests.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 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

Successfully merging a pull request may close this issue.

2 participants