pymodbus-dev / pymodbus

A full modbus protocol written in python
Other
2.18k stars 895 forks source link

Client on_reconnect_callback is never executed #1956

Closed LukasJanavicius closed 5 months ago

LukasJanavicius commented 5 months ago

Versions

Pymodbus Specific

Description

The client parameter/method on_reconnect_callback suggested in #1354 and implemented in #1425 is never executed in the current version of pymodbus. I believe this client parameter is unimplemented in the current version and failed to find any execution of the client.on_reconnect_callback method in the current pymodbus codebase or test suite. I attempted to use this callback to notify another async application of the failed connection, however, I could not trigger the callback in simulated/physical testing.

Code

from pymodbus.client import AsyncModbusTcpClient
from pymodbus import pymodbus_apply_logging_config
import asyncio

COMMAND_REGISTER = 2006
RESET_COMMAND = 0x8F00

HOST = "w.x.y.z"
PORT = 502
CALLBACK_USED = False

def reconnect_callback():
    CALLBACK_USED = True
    print("Reconnect callback called!")

async def call_device_restart(client: AsyncModbusTcpClient):
    """ Executes a reset command for a Phoenix-contact AXL-F device.
    Simulates disconnection, on_reconnect_callback is never called.
    """
    await client.write_register(COMMAND_REGISTER, value=RESET_COMMAND)

async def run_client():
    client = AsyncModbusTcpClient(HOST, PORT, on_reconnect_callback=reconnect_callback)
    await client.connect()
    assert client.connected
    print(f"Client Connected to {HOST}:{PORT}")
    print("Restarting Client")
    await call_device_restart(client)
    await asyncio.sleep(10)
    if not CALLBACK_USED:
        print("ERROR: Reconnect callback never called.")

if __name__ == "__main__":
    pymodbus_apply_logging_config("DEBUG")
    print("Starting Client.")
    asyncio.run(run_client(), debug=True)
    print("Script Done.")

Logs

Starting Client.
2024-01-30 20:54:41,492 DEBUG logging:103 Connecting to w.x.y.z:502.
2024-01-30 20:54:41,493 DEBUG logging:103 Connecting comm
2024-01-30 20:54:41,500 DEBUG logging:103 Connected to comm
2024-01-30 20:54:41,500 DEBUG logging:103 callback_connected called
Client Connected to 169.254.0.13:502
Restarting Client
2024-01-30 20:54:41,503 DEBUG logging:103 send: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x6 0x7 0xd6 0x8f 0x0
2024-01-30 20:54:41,506 DEBUG logging:103 Adding transaction 1
2024-01-30 20:54:41,508 DEBUG logging:103 recv: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x6 0x7 0xd6 0x8f 0x0 old_data:  addr=None
2024-01-30 20:54:41,508 DEBUG logging:103 Processing: 0x0 0x1 0x0 0x0 0x0 0x6 0x0 0x6 0x7 0xd6 0x8f 0x0
2024-01-30 20:54:41,509 DEBUG logging:103 Factory Response[WriteSingleRegisterResponse': 6]
2024-01-30 20:54:41,509 DEBUG logging:103 Getting transaction 1
2024-01-30 20:54:46,788 DEBUG logging:103 Connection lost comm due to [WinError 1236] The network connection was aborted by the local system
2024-01-30 20:54:46,790 DEBUG logging:103 Wait comm 100.0 ms before reconnecting.
2024-01-30 20:54:46,892 DEBUG logging:103 Connecting comm
2024-01-30 20:54:46,896 WARNING logging:109 Failed to connect [WinError 1232] The network location cannot be reached. For information about network troubleshooting, see Windows Help
2024-01-30 20:54:46,897 DEBUG logging:103 Wait comm 200.0 ms before reconnecting.  
2024-01-30 20:54:47,100 DEBUG logging:103 Connecting comm
2024-01-30 20:54:47,104 WARNING logging:109 Failed to connect [WinError 1232] The network location cannot be reached. For information about network troubleshooting, see Windows Help
2024-01-30 20:54:47,105 DEBUG logging:103 Wait comm 400.0 ms before reconnecting.  
2024-01-30 20:54:47,508 DEBUG logging:103 Connecting comm
2024-01-30 20:54:47,512 WARNING logging:109 Failed to connect [WinError 1232] The network location cannot be reached. For information about network troubleshooting, see Windows Help
2024-01-30 20:54:47,513 DEBUG logging:103 Wait comm 800.0 ms before reconnecting.
2024-01-30 20:54:48,314 DEBUG logging:103 Connecting comm
2024-01-30 20:54:48,318 WARNING logging:109 Failed to connect [WinError 1232] The network location cannot be reached. For information about network troubleshooting, see Windows Help
2024-01-30 20:54:48,319 DEBUG logging:103 Wait comm 1600.0 ms before reconnecting.
2024-01-30 20:54:49,920 DEBUG logging:103 Connecting comm
2024-01-30 20:54:49,924 WARNING logging:109 Failed to connect [WinError 1232] The network location cannot be reached. For information about network troubleshooting, see Windows Help
2024-01-30 20:54:49,925 DEBUG logging:103 Wait comm 3200.0 ms before reconnecting.
ERROR: Reconnect callback never called.
Script Done.
janiversen commented 5 months ago

Seems like a bug, let me have a look.