thingsboard / thingsboard-gateway

Open-source IoT Gateway - integrates devices connected to legacy and third-party systems with ThingsBoard IoT Platform using Modbus, CAN bus, BACnet, BLE, OPC-UA, MQTT, ODBC and REST protocols
https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
Apache License 2.0
1.76k stars 846 forks source link

[BUG] Connector OPC-UA ASYNCIO creates too many sessions (BadTooManySessions) and do not release them when closing or disabling connector #1494

Closed ashdam closed 3 months ago

ashdam commented 3 months ago

Describe the bug

We have a compulsory "max number of sessions" within OPC-UA in our company which is 10 and a timeout of 20 minutes by default. Give this restriction, we get "BadTooManySessions" many like this image after investigating we noticed that OPC-UA ASYNCIO creates sessions but do not close them in the following reproducible scenarios:

Starting point:

2 connectors "disabled" from last session (where we couldn't work any more due to "BadTooManySessions") image

Scenarios/ not expected behaviours:

1) Once gateway is started:

Expected: Do not connect if connector is disabled.

2024-08-11 09:03:28 - |ERROR| - [tb_logger.py] - tb_logger - exception - 133 - Cannot put converted data!
Traceback (most recent call last):
  File "/home/admin/tb-iot-gateway-latest/thingsboard-gateway/venv/lib/python3.12/site-packages/thingsboard_gateway-3.5.1-py3.12.egg/thingsboard_gateway/gateway/tb_gateway_service.py", line 992, in send_to_storage
    filtered_data = self.__duplicate_detector.filter_data(connector_name, data)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TBGatewayService' object has no attribute '_TBGatewayService__duplicate_detector'. Did you mean: '_TBGatewayService__device_filter'?
2024-08-11 09:03:28 - |DEBUG| - [opcua_uplink_converter.py] - opcua_uplink_converter - convert - 112 - Converted data: {'deviceName': 'MB107_UDC1_A0000KAO8B', 'deviceType': 'default', 'attributes': [ <DELETED_DATA_TO SHORTEN_LOG> ], 'telemetry': [<DELETED_DATA_TO SHORTEN_LOG>]}
2024-08-11 09:03:28 - |DEBUG| - [opcua_connector.py] - opcua_connector - __send_data - 493 - Data to ThingsBoard {'deviceName': 'MB107_UDC1_A0000K9J76', 'deviceType': 'default', 'attributes': [<DELETED_DATA_TO SHORTEN_LOG>], 'telemetry': [<DELETED_DATA_TO SHORTEN_LOG>]}
2024-08-11 09:03:28 - |DEBUG| - [opcua_connector.py] - opcua_connector - __send_data - 490 - {'deviceName': 'MB107_UDC1_A0000KAO8B', 'deviceType': 'default', 'attributes': [<DELETED_DATA_TO SHORTEN_LOG>], 'telemetry': [<DELETED_DATA_TO SHORTEN_LOG>]}

Note that the duplicate_detector error we don't know why its produced and it was the first time to be seen.

2) Another 2 sessions are created once a connector is started. Maybe one per timeseries? Check the configurator Expected: 1 session per connector.

image image

{
  "server": {
    "name": "YYYYYYYYY",
    "url": "opc.tcp://xxxxxxxxx:59300/xxxxxxx/UnifiedDataCollector",
    "timeoutInMillis": 5000,
    "scanPeriodInMillis": 10000,
    "disableSubscriptions": true,
    "subCheckPeriodInMillis": 100,
    "showMap": false,
    "security": "Basic128Rsa15",
    "identity": {
      "type": "anonymous"
    },
    "mapping": [
      {
        "deviceNodePattern": "Root\\.Objects\\.Server",
        "deviceNamePattern": "MB107_UDC1_A0000K9J76",
        "attributes": [
          {
            "key": "server_uai",
            "path": "${ns=2;b=sbnFUIh4jWVrbbrxt463og==}"
          },
          {
            "key": "device_uai",
            "path": "${ns=2;b=sbnFUIh4jWVrbbrxt463og==}"
          },
          {
            "key": "site_code",
            "path": "${ns=2;b=zCGYLEQhEXoiQHQBVhcIoA==}"
          },
          {
            "key": "line",
            "path": "${ns=2;b=WrO0W3muBdo+gdCYlWooSQ==}"
          }
        ],
        "timeseries": [
          {
            "key": "/Server/ServerStatus/BuildInfo",
            "path": "${ns=0;i=2260}"
          },
          {
            "key": "/Server/UaiInfo/UaiCode",
            "path": "${ns=2;b=sbnFUIh4jWVrbbrxt463og==}"
          }
        ]
      }
    ]
  },
  "logLevel": "DEBUG",
  "name": "YYYYYYYYY_connector",
  "enableRemoteLogging": true,
  "id": "19cdae8c-1a85-4454-8892-3b40df28e650"
}

3) Sessions are not released after disabling the connector

image image

We also tested to disable->enable->disable and additional sessions are created without releasing the previous ones

Expected: Sessions are released after disabling the connector

4) Closing gateway do not release sessions.

image

Expected: Sessions are released after closing gateway

Connector name (If bug in the some connector): OPC-UA asyncio Connector

Error traceback (If available): Above

Versions (please complete the following information):