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

Critical HA error on socket teardown #1

Closed
minix1234 opened this issue Mar 6, 2021 · 4 comments
Closed

Critical HA error on socket teardown #1

minix1234 opened this issue Mar 6, 2021 · 4 comments
Assignees
Labels
Investigation issue needs further investigation

Comments

@minix1234
Copy link
Owner

appears that a critical error occurs while opcua attempts to connect to an opc ua server and it is rejected. in this case due to incorrect security configuration. first reported at HA Community Forum

That said it shouldn't crash HA or prevent further loading. additional investigation required.

Home-assistant.log file :

2021-03-03 14:00:26 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.


2021-03-03 14:00:34 CRITICAL (Thread-3) [opcua.client.ua_client.Socket] Received an error: MessageAbort(error:StatusCode(BadSecurityPolicyRejected), reason:None)
2021-03-03 14:00:34 ERROR (Thread-3) [opcua.client.ua_client.Socket] Protocol Error
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 101, in _run
    self._receive()
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 121, in _receive
    self._call_callback(0, ua.UaStatusCodeError(msg.Error.value))
  File "/usr/local/lib/python3.8/site-packages/opcua/client/ua_client.py", line 129, in _call_callback
    raise ua.UaError(
opcua.ua.uaerrors._base.UaError: No future object found for request: 0, callbacks in list are dict_keys([2])
@minix1234 minix1234 added the Investigation issue needs further investigation label Mar 6, 2021
@minix1234 minix1234 self-assigned this Mar 6, 2021
@minix1234
Copy link
Owner Author

could be an issue with base client class implementation of socket connector.

    def connect(self):
        """
        High level method
        Connect, create and activate session
        """
        self.connect_socket()
        try:
            self.send_hello()
            self.open_secure_channel()
            try:
                self.create_session()
                try:
                    self.activate_session(username=self._username, password=self._password, certificate=self.user_certificate)
                except Exception:
                    # clean up the session
                    self.close_session()
                    raise
            except Exception:
                # clean up the secure channel
                self.close_secure_channel()
                raise
        except Exception:
            self.disconnect_socket()  # clean up open socket
            raise

and this connector

def connect_socket(self):
        """
        connect to socket defined in url
        """
        self.uaclient.connect_socket(self.server_url.hostname, self.server_url.port)

we may need to wrap our own try method to catch socket errors.

we should find out common causes of socket errors and determine best path forward

@minix1234
Copy link
Owner Author

So when attempting to connect to a port that doesn't exist on the end-point you get the following error. In this case the component fails to load (all hubs and sensors), but home assistant continues to load.

Logger: homeassistant.setup
Source: custom_components/opcua/__init__.py:229
First occurred: 1:49:48 PM (1 occurrences)
Last logged: 1:49:48 PM

Error during setup of component opcua
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 213, in _async_setup_component
    result = await task
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/opcua/__init__.py", line 141, in setup
    client.setup()
  File "/config/custom_components/opcua/__init__.py", line 221, in setup
    self.connect()
  File "/config/custom_components/opcua/__init__.py", line 229, in connect
    self._client.connect()
  File "/usr/local/lib/python3.8/site-packages/opcua/client/client.py", line 277, in connect
    self.create_session()
  File "/usr/local/lib/python3.8/site-packages/opcua/client/client.py", line 401, in create_session
    ep = Client.find_endpoint(response.ServerEndpoints, self.security_policy.Mode, self.security_policy.URI)
  File "/usr/local/lib/python3.8/site-packages/opcua/client/client.py", line 158, in find_endpoint
    raise ua.UaError("No matching endpoints: {0}, {1}".format(security_mode, policy_uri))
opcua.ua.uaerrors._base.UaError: No matching endpoints: 1, http://opcfoundation.org/UA/SecurityPolicy#None

@minix1234
Copy link
Owner Author

minix1234 commented Mar 8, 2021

If we wrap the setup connect in a try (maybe best to put the try in the connect directive) then we get an error and other hubs continue to connect.

image

When trying to connect to a UA server where security policy is not "none" then another warning is generated but HA continues to load.

image

image

Both messages are not overly useful in describing which UA sever connection was to fault for the error. maybe we can intercept and add to the error message to make it more useful.

minix1234 added a commit that referenced this issue Mar 8, 2021
@minix1234
Copy link
Owner Author

provided a commit to wrap the connect methods in a try and it will raise an error and hopefully not crash HA

errors do still lead to all OPC UA targets and the component not being loaded. we should address that as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigation issue needs further investigation
Projects
None yet
Development

No branches or pull requests

1 participant