fix(client): release a capped HistoryRead cursor, read (not release) follow-up pages, detach deleted subscriptions
R08 1.4: raw and modified history reads share HistoryReadPaging. A read stopped by the caller's cap releases the server continuation point. The raw path also passed releaseContinuationPoints = (cp != null), which released page two instead of reading it, so every raw read was silently truncated to its first server page. R08 1.5: DefaultSubscriptionAdapter removes its Subscription from the SDK Session on delete/dispose (and a failed CreateAsync removes it too), so re-targeted alarm subscriptions no longer accumulate in Session.Subscriptions. Refs #747 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -164,46 +164,16 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
|
||||
ReturnBounds = false
|
||||
};
|
||||
|
||||
var nodesToRead = new HistoryReadValueIdCollection
|
||||
{
|
||||
new HistoryReadValueId { NodeId = nodeId }
|
||||
};
|
||||
|
||||
var allValues = new List<DataValue>();
|
||||
byte[]? continuationPoint = null;
|
||||
|
||||
do
|
||||
{
|
||||
if (continuationPoint != null)
|
||||
nodesToRead[0].ContinuationPoint = continuationPoint;
|
||||
|
||||
// Use the async overload so this method is genuinely asynchronous,
|
||||
// honors the cancellation token, and does not block the caller's thread
|
||||
// (which would block the UI dispatcher for client.ui consumers).
|
||||
var response = await _session.HistoryReadAsync(
|
||||
null,
|
||||
new ExtensionObject(details),
|
||||
TimestampsToReturn.Source,
|
||||
continuationPoint != null,
|
||||
nodesToRead,
|
||||
ct).ConfigureAwait(false);
|
||||
|
||||
var results = response.Results;
|
||||
if (results == null || results.Count == 0)
|
||||
break;
|
||||
|
||||
var result = results[0];
|
||||
// A refused read (BadHistoryOperationUnsupported, BadUserAccessDenied, ...) must surface; only
|
||||
// BadNoData is an empty result. Swallowing it printed "0 values returned" for a refusal.
|
||||
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
|
||||
|
||||
if (result.HistoryData is ExtensionObject ext && ext.Body is HistoryData historyData)
|
||||
allValues.AddRange(historyData.DataValues);
|
||||
|
||||
continuationPoint = result.ContinuationPoint;
|
||||
} while (continuationPoint != null && continuationPoint.Length > 0 && allValues.Count < maxValues);
|
||||
|
||||
return allValues;
|
||||
// Continuation pages are READ, never released, until the server finishes or the cap is reached; a read stopped
|
||||
// by the cap releases the server's cursor (HistoryReadPaging). This path used to pass
|
||||
// `releaseContinuationPoints: continuationPoint != null`, which RELEASED the second page instead of reading it,
|
||||
// silently truncating every raw read to its first server page.
|
||||
return await HistoryReadPaging.ReadAllAsync(
|
||||
(cp, release, token) => ReadHistoryPageAsync(details, nodeId, cp, release, TimestampsToReturn.Source, token),
|
||||
result => result.HistoryData is ExtensionObject { Body: HistoryData historyData }
|
||||
? historyData.DataValues
|
||||
: [],
|
||||
maxValues, nodeId, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -219,59 +189,41 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
|
||||
ReturnBounds = false
|
||||
};
|
||||
|
||||
return await HistoryReadPaging.ReadAllAsync(
|
||||
(cp, release, token) => ReadHistoryPageAsync(details, nodeId, cp, release, TimestampsToReturn.Both, token),
|
||||
ToModifiedEntries, maxValues, nodeId, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static IEnumerable<Models.HistoryModifiedEntry> ToModifiedEntries(HistoryReadResult result)
|
||||
{
|
||||
if (result.HistoryData is not ExtensionObject { Body: HistoryModifiedData modified }) yield break;
|
||||
for (var i = 0; i < modified.DataValues.Count; i++)
|
||||
{
|
||||
var info = i < modified.ModificationInfos.Count ? modified.ModificationInfos[i] : null;
|
||||
yield return new Models.HistoryModifiedEntry(
|
||||
modified.DataValues[i],
|
||||
info?.ModificationTime ?? DateTime.MinValue,
|
||||
info?.UpdateType ?? default,
|
||||
info?.UserName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Issues one single-node HistoryRead (or a release of its continuation point).</summary>
|
||||
private async Task<HistoryReadResult?> ReadHistoryPageAsync(
|
||||
ReadRawModifiedDetails details, NodeId nodeId, byte[]? continuationPoint, bool release,
|
||||
TimestampsToReturn timestamps, CancellationToken ct)
|
||||
{
|
||||
var nodesToRead = new HistoryReadValueIdCollection
|
||||
{
|
||||
new HistoryReadValueId { NodeId = nodeId }
|
||||
new HistoryReadValueId { NodeId = nodeId, ContinuationPoint = continuationPoint }
|
||||
};
|
||||
|
||||
var all = new List<Models.HistoryModifiedEntry>();
|
||||
byte[]? continuationPoint = null;
|
||||
// The async overload honours the token and does not block the caller's thread (the UI dispatcher for
|
||||
// client.ui consumers).
|
||||
var response = await _session.HistoryReadAsync(
|
||||
null, new ExtensionObject(details), timestamps, release, nodesToRead, ct).ConfigureAwait(false);
|
||||
|
||||
do
|
||||
{
|
||||
if (continuationPoint != null)
|
||||
nodesToRead[0].ContinuationPoint = continuationPoint;
|
||||
|
||||
var response = await _session.HistoryReadAsync(
|
||||
null,
|
||||
new ExtensionObject(details),
|
||||
TimestampsToReturn.Both,
|
||||
false,
|
||||
nodesToRead,
|
||||
ct).ConfigureAwait(false);
|
||||
|
||||
var results = response.Results;
|
||||
if (results == null || results.Count == 0)
|
||||
break;
|
||||
|
||||
var result = results[0];
|
||||
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
|
||||
|
||||
if (result.HistoryData is ExtensionObject ext && ext.Body is HistoryModifiedData modified)
|
||||
{
|
||||
for (var i = 0; i < modified.DataValues.Count; i++)
|
||||
{
|
||||
var info = i < modified.ModificationInfos.Count ? modified.ModificationInfos[i] : null;
|
||||
all.Add(new Models.HistoryModifiedEntry(
|
||||
modified.DataValues[i],
|
||||
info?.ModificationTime ?? DateTime.MinValue,
|
||||
info?.UpdateType ?? default,
|
||||
info?.UserName));
|
||||
}
|
||||
}
|
||||
|
||||
continuationPoint = result.ContinuationPoint;
|
||||
} while (continuationPoint != null && continuationPoint.Length > 0 && all.Count < maxValues);
|
||||
|
||||
// A read stopped by the client's own cap leaves a server cursor behind; release it.
|
||||
if (continuationPoint is { Length: > 0 })
|
||||
{
|
||||
nodesToRead[0].ContinuationPoint = continuationPoint;
|
||||
await _session.HistoryReadAsync(null, new ExtensionObject(details), TimestampsToReturn.Both, true,
|
||||
nodesToRead, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return all;
|
||||
return response.Results is { Count: > 0 } results ? results[0] : null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -327,9 +279,22 @@ internal sealed class DefaultSessionAdapter : ISessionAdapter
|
||||
};
|
||||
|
||||
_session.AddSubscription(subscription);
|
||||
await subscription.CreateAsync(ct);
|
||||
try
|
||||
{
|
||||
await subscription.CreateAsync(ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// A subscription the server never created must not linger in Session.Subscriptions either.
|
||||
try { await _session.RemoveSubscriptionAsync(subscription, CancellationToken.None); }
|
||||
catch (Exception ex) { Logger.Warning(ex, "Error removing a subscription that failed to create"); }
|
||||
throw;
|
||||
}
|
||||
|
||||
return new DefaultSubscriptionAdapter(subscription);
|
||||
// Deleting the adapter also removes the subscription from the SDK session, so a re-targeted alarm/data
|
||||
// subscription does not accumulate dead Subscription objects in Session.Subscriptions.
|
||||
return new DefaultSubscriptionAdapter(subscription,
|
||||
async (s, token) => await _session.RemoveSubscriptionAsync(s, token).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -12,14 +12,23 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
|
||||
private static readonly ILogger Logger = Log.ForContext<DefaultSubscriptionAdapter>();
|
||||
private readonly Dictionary<uint, MonitoredItem> _monitoredItems = new();
|
||||
private readonly Subscription _subscription;
|
||||
private readonly Func<Subscription, CancellationToken, Task>? _detach;
|
||||
private int _detached;
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a live OPC UA subscription so client code can manage monitored items through a testable abstraction.
|
||||
/// </summary>
|
||||
/// <param name="subscription">The underlying OPC UA subscription that owns monitored items for this client workflow.</param>
|
||||
public DefaultSubscriptionAdapter(Subscription subscription)
|
||||
/// <param name="detach">
|
||||
/// Removes the subscription from its owning SDK <c>Session</c> once it is deleted. Without it a deleted
|
||||
/// subscription stays in <c>Session.Subscriptions</c> until the session is disposed, so a UI that re-targets
|
||||
/// alarms repeatedly accumulates dead subscriptions the SDK still iterates (transfer / recreate on reconnect).
|
||||
/// </param>
|
||||
public DefaultSubscriptionAdapter(Subscription subscription,
|
||||
Func<Subscription, CancellationToken, Task>? detach = null)
|
||||
{
|
||||
_subscription = subscription;
|
||||
_detach = detach;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -113,9 +122,24 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
|
||||
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();
|
||||
}
|
||||
|
||||
private async Task DetachAsync(CancellationToken ct)
|
||||
{
|
||||
if (_detach == null || Interlocked.Exchange(ref _detached, 1) != 0) return;
|
||||
try
|
||||
{
|
||||
await _detach(_subscription, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warning(ex, "Error removing subscription from its session");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases the wrapped OPC UA subscription and clears tracked monitored items held by the adapter.
|
||||
/// </summary>
|
||||
@@ -129,6 +153,14 @@ internal sealed class DefaultSubscriptionAdapter : ISubscriptionAdapter
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
DisposeBridge.Run(DetachAsync);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
_monitoredItems.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using Opc.Ua;
|
||||
using Serilog;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// The client-side HistoryRead paging loop shared by the raw and modified reads. Continuation pages are
|
||||
/// READ (<c>releaseContinuationPoints = false</c>) while the caller still wants values; a read stopped by the
|
||||
/// caller's own cap while the server still holds a cursor releases that cursor so it does not occupy one of the
|
||||
/// session's continuation-point slots until eviction.
|
||||
/// </summary>
|
||||
internal static class HistoryReadPaging
|
||||
{
|
||||
private static readonly ILogger Logger = Log.ForContext(typeof(HistoryReadPaging));
|
||||
|
||||
/// <summary>Reads (or, with <paramref name="releaseContinuationPoints"/>, releases) one page.</summary>
|
||||
/// <param name="continuationPoint">The cursor to resume, or <see langword="null"/> for the first page.</param>
|
||||
/// <param name="releaseContinuationPoints"><see langword="true"/> to release the cursor instead of reading.</param>
|
||||
/// <param name="ct">The caller's cancellation token.</param>
|
||||
/// <returns>The per-node result, or <see langword="null"/> when the response carried none.</returns>
|
||||
public delegate Task<HistoryReadResult?> PageReader(
|
||||
byte[]? continuationPoint, bool releaseContinuationPoints, CancellationToken ct);
|
||||
|
||||
/// <summary>Reads pages until the server finishes or the caller's cap is reached.</summary>
|
||||
/// <typeparam name="T">The element type extracted from each page.</typeparam>
|
||||
/// <param name="readPage">Issues one HistoryRead for the single node.</param>
|
||||
/// <param name="extract">Extracts the page's elements from a result.</param>
|
||||
/// <param name="maxValues">The caller's cap; <c><= 0</c> means no cap (follow every continuation point).</param>
|
||||
/// <param name="nodeId">The node being read, for refusal messages.</param>
|
||||
/// <param name="ct">The caller's cancellation token.</param>
|
||||
/// <returns>Every element read.</returns>
|
||||
/// <exception cref="ServiceResultException">A page was refused (see <see cref="HistoryReadStatus"/>).</exception>
|
||||
public static async Task<List<T>> ReadAllAsync<T>(
|
||||
PageReader readPage, Func<HistoryReadResult, IEnumerable<T>> extract, int maxValues, NodeId nodeId,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var all = new List<T>();
|
||||
byte[]? continuationPoint = null;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var result = await readPage(continuationPoint, false, ct).ConfigureAwait(false);
|
||||
if (result == null) return all;
|
||||
|
||||
HistoryReadStatus.ThrowIfRefused(result.StatusCode, nodeId);
|
||||
all.AddRange(extract(result));
|
||||
|
||||
continuationPoint = result.ContinuationPoint;
|
||||
if (continuationPoint is not { Length: > 0 }) return all;
|
||||
if (maxValues > 0 && all.Count >= maxValues) break;
|
||||
}
|
||||
|
||||
// Stopped by the client's own cap: the server still holds a cursor for this session. Release it; failing to
|
||||
// is not worth failing a read whose values are already in hand.
|
||||
try
|
||||
{
|
||||
await readPage(continuationPoint, true, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
Logger.Warning(ex, "Releasing the HistoryRead continuation point for {NodeId} failed", nodeId);
|
||||
}
|
||||
|
||||
return all;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user