FreeOpcUa / opcua-asyncio

OPC UA library for python >= 3.7
GNU Lesser General Public License v3.0
1.04k stars 345 forks source link

Infinite Read Requests? #1642

Closed imaginaryBuddy closed 1 month ago

imaginaryBuddy commented 1 month ago

Describe the bug
When I intend to only send a read request once, using the synchronous version, it tends to cause an infinite read and I have no idea how to fix it.

To Reproduce

from asyncua.sync import Client, sync_uaclient_method, sync_async_client_method
from asyncua.client import Client as AsyncClient
import time

try:
        client = Client(<ip>)
        client.connect() 
        node_to_read = [client.get_node("ns=2;i=2")]
        print(node_to_read)
        read = sync_async_client_method(AsyncClient.read_values)(client)
        read(nodes=node_to_read)
        time.sleep(10)
        client.disconnect()
except:
        client.disconnect()
        raise 

Expected behavior
I expect only 1 packet of read request sent from Client

Screenshots
This is the Wireshark capture after sending 1 read request, and setting a time.sleep(10) before disconnecting. image

Version
Python-Version: 3.12.1 opcua-asyncio Version: 1.1.0

schroeder- commented 1 month ago

Does this work?

try:
        client = Client(<ip>)
        client.connect() 
        node_to_read = [client.get_node("ns=2;i=2")]
        print(node_to_read)
        vals = client.read_values(node_to_read)
        print(vals)
        time.sleep(10)
        client.disconnect()
except:
        client.disconnect()
        raise 
imaginaryBuddy commented 1 month ago

@schroeder- It doesn't work. There's still infinite read requests being sent

schroeder- commented 1 month ago

Does read a single value work?

client.get_node("ns=2;i=2").read_value()

schroeder- commented 1 month ago

I checked it this is not a bug. We have a background task that reads every second to keep the connection alive. As i read it correct you wait 10 seconds, so 11 readrequests are generated, 1 from your request and 10 from the watchdog.

imaginaryBuddy commented 1 month ago

@schroeder- is there a way to disable this background task ?

schroeder- commented 1 month ago

Not in the syncclient.

oroulet commented 1 month ago

you want that task otherwise the client will die at some point. All UA clients do that. you can reduce and change behaviour if you want but you have to read code and override some stuff

imaginaryBuddy commented 1 month ago

@oroulet, I see, but my current project aims to replay captured packets from Wireshark for debugging purposes, so I do not want any extra read requests as I will ensure that my client stays alive based on the replay. could I get some guidance on how to change the behaviour?

Thank you!

oroulet commented 1 month ago

Grep code for keepAlive

imaginaryBuddy commented 1 month ago

I just set watchdog_intervall in the async version of Client to math.inf, and it works for my particular case: self._watchdog_intervall = math.inf