FreeOpcUa / opcua-asyncio

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

Error while renewing session: Invalid security token id 18, expected one of: [16, 17, 0] #313

Closed slokhorst closed 1 year ago

slokhorst commented 4 years ago

I have a running client with subscription that does just fine. After a while, I get the following error and the client disconnects:

Exception raised while parsing message from server
Traceback (most recent call last):
  File "/lib/python3.8/site-packages/asyncua/client/ua_client.py", line 79, in _process_received_data
    msg = self._connection.receive_from_header_and_body(header, buf)
  File "/lib/python3.8/site-packages/asyncua/common/connection.py", line 323, in receive_from_header_and_body
    self._check_sym_header(security_header)
  File "/lib/python3.8/site-packages/asyncua/common/connection.py", line 282, in _check_sym_header
    raise ua.UaError(f"Invalid security token id {security_hdr.TokenId}, expected one of: {expected_tokens}")
asyncua.ua.uaerrors._base.UaError: Invalid security token id 18, expected one of: [16, 17, 0]
Error while renewing session
Traceback (most recent call last):
  File "/lib/python3.8/site-packages/asyncua/client/client.py", line 387, in _renew_channel_loop
    val = await self.nodes.server_state.read_value()
  File "/lib/python3.8/site-packages/asyncua/common/node.py", line 167, in read_value
    result = await self.read_data_value()
  File "/lib/python3.8/site-packages/asyncua/common/node.py", line 178, in read_data_value
    return await self.read_attribute(ua.AttributeIds.Value)
  File "/lib/python3.8/site-packages/asyncua/common/node.py", line 285, in read_attribute
    result = await self.server.read(params)
  File "/lib/python3.8/site-packages/asyncua/client/ua_client.py", line 353, in read
    data = await self.protocol.send_request(request)
  File "/lib/python3.8/site-packages/asyncua/client/ua_client.py", line 141, in send_request
    self._send_request(request, timeout, message_type),
  File "/lib/python3.8/site-packages/asyncua/client/ua_client.py", line 130, in _send_request
    self.transport.write(msg)
AttributeError: 'NoneType' object has no attribute 'write'

I'm connecting to a Siemens S7-1200 PLC's built-in OPC-UA server.

oroulet commented 4 years ago

@Randelung there is also a pr called " fix race condition or similar" Which of these or fix your issue?

Randelung commented 4 years ago

As far as I can tell both #321 and #324 fix the issue. So far #317 also appears so, too.

slokhorst commented 3 years ago

I too have not seen the issue with recent versions of this lib (after #324?), so it seems to be fixed. (sorry for my long unresponsiveness but I did not have access to the device for a while)

Ilyazyk commented 3 years ago

Is my error I get periodically connected to the issue or needs to open new one?

ERROR:asyncua.client.client:Error while renewing session Traceback (most recent call last): File "C:\Users\Labuser\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncua\client\client.py", line 399, in _renew_channel_loop await self.open_secure_channel(renew=True) File "C:\Users\Labuser\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncua\client\client.py", line 292, in open_secure_channel result = await self.uaclient.open_secure_channel(params) File "C:\Users\Labuser\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncua\client\ua_client.py", line 278, in open_secure_channel return await self.protocol.open_secure_channel(params) File "C:\Users\Labuser\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncua\client\ua_client.py", line 213, in open_secure_channel await asyncio.wait_for(self._send_request(request, message_type=ua.MessageType.SecureOpen), self.timeout) File "C:\Users\Labuser\AppData\Local\Programs\Python\Python39\lib\site-packages\asyncua\client\ua_client.py", line 134, in _send_request self.transport.write(msg) AttributeError: 'NoneType' object has no attribute 'write'

Randelung commented 3 years ago

I don't think that it's immediately connected. The original issue was a race condition for token renewal and packet reception already with a new, at that point unrecognized token. I'd say your issue is sufficiently different to warrant a new ticket in any case (especially since the original issue has technically been resolved for six months). I'd recommend giving more info, though, just the error won't help much. What is your setup, what are you doing, can you reproduce the issue, and so on.