Skip to content

Commit 866741e

Browse files
committed
fix(opcua): prevent race condition during session reconnection
Move `__connected` flag clearing into `disconnect_if_connected()` to ensure connection state is properly synchronized. Add guard at monitor loop entry to skip server reads when not connected, preventing stale data access during reconnection sequences.
1 parent 1ab0c83 commit 866741e

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

thingsboard_gateway/connectors/opcua/opcua_connector.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ async def start_client(self):
453453
await self.disconnect_if_connected()
454454
except Exception:
455455
pass
456-
self.__connected = False
457456
await asyncio.sleep(.5)
458457

459458
def __is_certificate_based_auth(self):
@@ -462,6 +461,7 @@ def __is_certificate_based_auth(self):
462461

463462
async def disconnect_if_connected(self):
464463
if self.__connected:
464+
self.__connected = False
465465
try:
466466
if self.__enable_subscriptions:
467467
self.__client_recreation_required = True
@@ -531,12 +531,14 @@ def __set_auth_settings_by_username(self):
531531

532532
async def _monitor_server_loop(self):
533533
"""
534-
Checks if the server is alive
534+
Checks if the server is alive.
535535
"""
536536
timeout = min(self.__client.session_timeout / 1000 / 2, self.__client._watchdog_intervall)
537537
try:
538538
while not self.__client._closing and not self.__stopped:
539539
await asyncio.sleep(timeout)
540+
if not self.__connected:
541+
continue
540542
_ = await self.__client.nodes.server_state.read_value()
541543
self.__last_contact_time = monotonic()
542544
except ConnectionError as e:

0 commit comments

Comments
 (0)