Commit d781c453 authored by Christoph Ziebuhr's avatar Christoph Ziebuhr Committed by oroulet

Always close session

parent d2203fe7
......@@ -272,15 +272,20 @@ class Client:
await self.open_secure_channel()
try:
await self.create_session()
try:
await self.activate_session(username=self._username, password=self._password, certificate=self.user_certificate)
except Exception:
# clean up session
await self.close_session()
raise
except Exception:
# clean up secure channel
self.close_secure_channel()
await self.close_secure_channel()
raise
except Exception:
# clean up open socket
self.disconnect_socket()
raise
await self.activate_session(username=self._username, password=self._password, certificate=self.user_certificate)
async def disconnect(self):
"""
......@@ -301,8 +306,7 @@ class Client:
await self.uaclient.connect_socket(self.server_url.hostname, self.server_url.port)
def disconnect_socket(self):
if self.uaclient:
self.uaclient.disconnect_socket()
self.uaclient.disconnect_socket()
async def send_hello(self):
"""
......
......@@ -7,7 +7,7 @@ from typing import Dict, List, Optional, Union
from asyncua import ua
from ..ua.ua_binary import struct_from_binary, uatcp_to_binary, struct_to_binary, nodeid_from_binary, header_from_binary
from ..ua.uaerrors import BadTimeout, BadNoSubscription, BadSessionClosed, UaStructParsingError
from ..ua.uaerrors import BadTimeout, BadNoSubscription, BadSessionClosed, BadUserAccessDenied, UaStructParsingError
from ..common.connection import SecureConnection
......@@ -335,6 +335,9 @@ class UaClient:
# Alternatively we could make sure that there are no publish requests in flight when
# closing the session.
pass
except BadUserAccessDenied:
# Problem: older versions of asyncua didn't allow closing non-activated sessions. just ignore it.
pass
async def browse(self, parameters):
self.logger.info("browse")
......
......@@ -149,6 +149,7 @@ class UaProcessor:
async def _process_message(self, typeid, requesthdr, seqhdr, body):
if typeid in [ua.NodeId(ua.ObjectIds.CreateSessionRequest_Encoding_DefaultBinary),
ua.NodeId(ua.ObjectIds.CloseSessionRequest_Encoding_DefaultBinary),
ua.NodeId(ua.ObjectIds.ActivateSessionRequest_Encoding_DefaultBinary),
ua.NodeId(ua.ObjectIds.FindServersRequest_Encoding_DefaultBinary),
ua.NodeId(ua.ObjectIds.GetEndpointsRequest_Encoding_DefaultBinary)]:
......
......@@ -248,10 +248,6 @@ async def test_certificate_handling_failure(srv_crypto_one_cert):
async with clt:
assert await clt.get_objects_node().get_children()
with pytest.raises(ua.uaerrors.BadUserAccessDenied):
# disconnect manually to close the event loop and appease pytest
await clt.disconnect()
async def test_encrypted_private_key_handling_failure(srv_crypto_one_cert):
_, cert = srv_crypto_one_cert
......@@ -269,10 +265,6 @@ async def test_encrypted_private_key_handling_failure(srv_crypto_one_cert):
async with clt:
assert await clt.get_objects_node().get_children()
with pytest.raises(ua.uaerrors.BadUserAccessDenied):
# disconnect manually to close the event loop and appease pytest
await clt.disconnect()
async def test_certificate_handling_mismatched_creds(srv_crypto_one_cert):
_, cert = srv_crypto_one_cert
......@@ -413,9 +405,6 @@ async def test_anonymous_rejection():
cert,
mode=ua.MessageSecurityMode.SignAndEncrypt
)
with pytest.raises(ua.UaStatusCodeError) as exc_info:
with pytest.raises(ua.uaerrors.BadIdentityTokenRejected):
await clt.connect()
with pytest.raises(ua.UaStatusCodeError):
await clt.disconnect()
assert ua.StatusCodes.BadIdentityTokenRejected == exc_info.type.code
await srv.stop()
......@@ -125,8 +125,4 @@ async def test_permissions_anonymous(srv_crypto_one_cert):
)
await clt.connect()
await clt.get_endpoints()
with pytest.raises(ua.uaerrors.BadUserAccessDenied):
# currently CloseSessionRequest ist not allowed so excpetion is expected
# why CloseSession is not allowed if CreateSession works?
# but to prevent leaking tasks we do disconnect and get the exception
await clt.disconnect()
await clt.disconnect()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment