fix: preserve SDK 2.0 client value and lifetime contracts

This commit is contained in:
Joseph Doherty
2026-11-10 10:19:42 -05:00
parent 4400127810
commit 33c7dc4292
38 changed files with 591 additions and 186 deletions
@@ -70,7 +70,7 @@ internal sealed class DefaultApplicationConfigurationFactory : IApplicationConfi
ApplicationType = ApplicationType.Client,
ApplicationConfiguration = config
};
await app.CheckApplicationInstanceCertificatesAsync(false, ct: ct);
await app.CheckApplicationInstanceCertificatesAsync(false, lifeTimeInMonths: 2048, ct: ct);
}
}
catch
@@ -115,18 +115,14 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
{
// Pass the caller's token so a cancelled browse does not block on the continuation
// round-trip.
var response = await _session.BrowseNextAsync(null, false, [ByteString.From(continuationPoint)], ct);
if (response.Results.Count == 0)
throw new ServiceResultException(response.ResponseHeader.ServiceResult, "BrowseNext response contained no results.");
var result = response.Results[0];
if (StatusCode.IsBad(result.StatusCode)) throw new ServiceResultException(result.StatusCode);
return (result.ContinuationPoint.IsNull ? null : result.ContinuationPoint.ToArray(), result.References);
var (_, nextCp, nextRefs) = await _session.BrowseNextAsync(null, false, ByteString.From(continuationPoint), ct);
return (nextCp.IsNull ? null : nextCp.ToArray(), nextRefs);
}
/// <inheritdoc />
public async Task ReleaseBrowseContinuationPointAsync(byte[] continuationPoint, CancellationToken ct)
{
await _session.BrowseNextAsync(null, true, [ByteString.From(continuationPoint)], ct).ConfigureAwait(false);
await _session.BrowseNextAsync(null, true, ByteString.From(continuationPoint), ct).ConfigureAwait(false);
}
/// <inheritdoc />
@@ -276,9 +272,20 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
if (results.Count > 0)
{
var result = results[0];
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
if (result.HistoryData.TryGetValue(out HistoryData? historyData))
allValues.AddRange(historyData.DataValues);
try
{
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
if (result.HistoryData.TryGetValue(out HistoryData? historyData))
allValues.AddRange(historyData.DataValues);
}
finally
{
// Keep the existing one-shot processed read; it owns no outstanding server cursor on return.
if (!result.ContinuationPoint.IsEmpty)
await HistoryReadPaging.ReleaseAsync(
(cp, release, token) => ReadHistoryPageAsync(details, nodeId, cp, release, TimestampsToReturn.Source, token),
result.ContinuationPoint.ToArray(), nodeId).ConfigureAwait(false);
}
}
return allValues;
@@ -14,6 +14,8 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
private readonly Subscription _subscription;
private readonly Func<Subscription, CancellationToken, Task>? _detach;
private int _detached;
private int _finishing;
private readonly TaskCompletionSource _finished = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>
/// Wraps a live OPC UA subscription so client code can manage monitored items through a testable abstraction.
@@ -47,7 +49,7 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
item.Notification += (_, e) =>
{
if (e.NotificationValue is MonitoredItemNotification notification)
if (Volatile.Read(ref _finishing) == 0 && e.NotificationValue is MonitoredItemNotification notification)
onDataChange(nodeId.ToString(), notification.Value);
};
@@ -90,7 +92,7 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
item.Notification += (_, e) =>
{
if (e.NotificationValue is EventFieldList eventFields) onEvent(eventFields);
if (Volatile.Read(ref _finishing) == 0 && e.NotificationValue is EventFieldList eventFields) onEvent(eventFields);
};
_subscription.AddItem(item);
@@ -112,19 +114,28 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
/// <inheritdoc />
public async Task DeleteAsync(CancellationToken ct)
{
if (Interlocked.Exchange(ref _finishing, 1) != 0)
{
await _finished.Task.WaitAsync(ct).ConfigureAwait(false);
return;
}
try
{
// Forward the caller's token so the delete can be cancelled.
await _subscription.DeleteAsync(true, ct);
}
catch (Exception ex)
{
Logger.Warning(ex, "Error deleting subscription");
}
try { await _subscription.DeleteAsync(true, ct).ConfigureAwait(false); }
catch (Exception ex) { Logger.Warning(ex, "Error deleting subscription"); }
// Detach whether or not the server-side delete succeeded: the adapter is finished with it either way.
await DetachAsync(ct);
_monitoredItems.Clear();
// A cancelled delete still has local resources to release. Cleanup gets its own bounded token.
using var cleanup = new CancellationTokenSource(TimeSpan.FromSeconds(5));
try { await DetachAsync(cleanup.Token).WaitAsync(cleanup.Token).ConfigureAwait(false); }
catch (Exception ex) { Logger.Warning(ex, "Error detaching subscription"); }
finally
{
_monitoredItems.Clear();
try { await Task.Run(_subscription.Dispose).WaitAsync(cleanup.Token).ConfigureAwait(false); }
catch (Exception ex) { Logger.Warning(ex, "Error disposing subscription resources"); }
}
}
finally { _finished.TrySetResult(); }
}
private async Task DetachAsync(CancellationToken ct)
@@ -145,21 +156,6 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
/// </summary>
public void Dispose()
{
// One bounded bridge (5 s) for delete + detach, so a dispose never blocks longer than before (review m6).
// DetachAsync swallows its own failures; a failed delete must not skip the detach.
try
{
DisposeBridge.Run(async ct =>
{
try { await _subscription.DeleteAsync(true, ct); }
catch (Exception ex) { Logger.Warning(ex, "Error deleting subscription"); }
await DetachAsync(ct);
});
}
catch
{
}
_monitoredItems.Clear();
DisposeBridge.Run(DeleteAsync);
}
}