fix(dcl): dispose+detach previous OPC UA client on reconnect (S1) — stops session leak and stale ConnectionLost flapping

This commit is contained in:
Joseph Doherty
2026-07-08 23:16:27 -04:00
parent 5d5c747517
commit 11eb5157e6
2 changed files with 70 additions and 0 deletions
@@ -96,6 +96,24 @@ public class OpcUaDataConnection : IDataConnection, IBrowsableDataConnection, IA
_status = ConnectionHealth.Connecting;
// Reconnect reuses this adapter instance (DataConnectionActor re-calls
// ConnectAsync on auto-reconnect): detach + dispose the previous client so
// (a) its session/keep-alive/socket do not leak per flap and (b) its late
// ConnectionLost cannot flap the NEW healthy session after _disconnectFired
// is reset below. Also stop the previous heartbeat monitor so the old
// StaleTagMonitor cannot fire against the new session. Dispose is best-effort
// fire-and-forget (CloseAsync against a dead server can block for the
// operation timeout); faults are logged, never thrown into the connect path.
StopHeartbeatMonitor();
if (_client is { } previousClient)
{
previousClient.ConnectionLost -= OnClientConnectionLost;
_ = previousClient.DisposeAsync().AsTask().ContinueWith(
t => _logger.LogWarning(t.Exception?.GetBaseException(),
"Disposing previous OPC UA client failed for {Endpoint}", _endpointUrl),
TaskContinuationOptions.OnlyOnFaulted);
}
_client = _clientFactory.Create();
_client.ConnectionLost += OnClientConnectionLost;
await _client.ConnectAsync(_endpointUrl, options, cancellationToken);