fix(opcuaclient): #1039 subscribe-time refusals ride the skipped-tag report (SkippedTagLedger.Append/Withdraw); probe preflight keeps an IPv6 zone

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 15:20:27 -05:00
parent d8cf9723d1
commit eff2b8e573
7 changed files with 287 additions and 6 deletions
@@ -124,6 +124,16 @@ public sealed class SkippedTagLedger
private int _stagedAuthored;
private int _stagedSkipped;
private List<SkippedTag>? _stagedSample;
private HashSet<string>? _stagedPaths;
// What the VISIBLE report was built from, kept so a late Append / Withdraw can recompute it (#1039).
private int _builtAuthored;
private int _builtSkipped;
private List<SkippedTag> _builtSample = [];
private HashSet<string> _builtPaths = [];
/// <summary>Skips noticed AFTER the build (a subscribe-time refusal), in encounter order. Reset by every rebuild.</summary>
private readonly List<SkippedTag> _appended = [];
/// <summary>
/// Starts staging a new tag-table build. The visible snapshot is left alone until
@@ -137,6 +147,7 @@ public sealed class SkippedTagLedger
_stagedAuthored = authoredCount;
_stagedSkipped = 0;
_stagedSample = new List<SkippedTag>(Math.Min(authoredCount, MaxSample));
_stagedPaths = new HashSet<string>(StringComparer.Ordinal);
}
}
@@ -154,11 +165,75 @@ public sealed class SkippedTagLedger
{
if (_stagedSample is null) return;
_stagedSkipped++;
_stagedPaths?.Add(rawPath ?? "");
if (_stagedSample.Count < MaxSample)
_stagedSample.Add(new SkippedTag(rawPath ?? "", reason ?? ""));
}
}
/// <summary>
/// Records a skip that was only discovered AFTER the build, against the already-visible report (#1039) — e.g. the
/// upstream server refusing a monitored item at subscribe time. The build-time skips are authored-config
/// problems; this is the same operator signal for a tag that only fails once the driver dials the device. Idempotent
/// per raw path (a subscribe retried every health tick must not inflate the count); a path the build already
/// skipped is not counted twice. A no-op before the first <see cref="Commit"/> ("no answer yet" is never turned
/// into a report), and cleared by the next <see cref="BeginRebuild"/>/<see cref="Commit"/> — a re-initialise
/// re-subscribes and re-appends what is still refused.
/// </summary>
/// <param name="rawPath">The authored raw path that was refused.</param>
/// <param name="reason">Operator-facing reason; should name the offending status.</param>
/// <returns><see langword="true"/> when the report changed.</returns>
public bool Append(string rawPath, string reason)
{
lock (_gate)
{
if (_visible is null) return false;
rawPath ??= "";
if (_builtPaths.Contains(rawPath)) return false;
var existing = _appended.FindIndex(s => string.Equals(s.RawPath, rawPath, StringComparison.Ordinal));
if (existing >= 0)
{
if (_appended[existing].Reason == (reason ?? "")) return false;
_appended[existing] = new SkippedTag(rawPath, reason ?? "");
}
else
{
_appended.Add(new SkippedTag(rawPath, reason ?? ""));
}
Republish();
return true;
}
}
/// <summary>
/// Withdraws a skip recorded by <see cref="Append"/> — the ref now has its upstream item (a later subscribe
/// succeeded), so it must stop being reported. Build-time skips are never withdrawn.
/// </summary>
/// <param name="rawPath">The raw path that is no longer refused.</param>
/// <returns><see langword="true"/> when the report changed.</returns>
public bool Withdraw(string rawPath)
{
lock (_gate)
{
if (_appended.Count == 0) return false;
var removed = _appended.RemoveAll(s => string.Equals(s.RawPath, rawPath ?? "", StringComparison.Ordinal)) > 0;
if (removed) Republish();
return removed;
}
}
/// <summary>Rebuilds the visible report from the committed build + the appended skips. Caller holds the gate.</summary>
private void Republish()
{
var sample = new List<SkippedTag>(_builtSample);
foreach (var a in _appended)
{
if (sample.Count >= MaxSample) break;
sample.Add(a);
}
_visible = new SkippedTagReport(_builtAuthored, _builtSkipped + _appended.Count, sample);
}
/// <summary>
/// Publishes the staged build as the visible snapshot, atomically. A call with no staged build
/// is a no-op.
@@ -168,8 +243,14 @@ public sealed class SkippedTagLedger
lock (_gate)
{
if (_stagedSample is null) return;
_visible = new SkippedTagReport(_stagedAuthored, _stagedSkipped, _stagedSample);
_builtAuthored = _stagedAuthored;
_builtSkipped = _stagedSkipped;
_builtSample = _stagedSample;
_builtPaths = _stagedPaths ?? [];
_appended.Clear();
_visible = new SkippedTagReport(_stagedAuthored, _stagedSkipped, _builtSample);
_stagedSample = null;
_stagedPaths = null;
}
}
@@ -46,7 +46,7 @@ public sealed class OpcUaClientDriver : IDriver, ITagDiscovery, IReadable, IWrit
/// type stays free of driver bookkeeping.
/// </summary>
/// <param name="map">The map just built from the live session.</param>
private void RecordSkippedTags(NamespaceMap map)
internal void RecordSkippedTags(NamespaceMap map)
{
var authored = _options.RawTags;
_skippedTags.BeginRebuild(authored.Count);
@@ -2128,6 +2128,16 @@ public sealed class OpcUaClientDriver : IDriver, ITagDiscovery, IReadable, IWrit
foreach (var pair in itemHandlers)
if (RejectedMonitoredItemStatus(pair.Item) is { } refused)
rejected.Add(new RejectedItem(RefOf(pair.Item), refused, $"the server refused the monitored item (0x{refused:X8})"));
// #1039: a ref a LATER subscribe now serves stops being reported skipped (the report is re-asserted from the
// refusals of each subscribe, so a fixed upstream node drops off it).
if (rejected.Count == 0) { foreach (var fullRef in fullReferences) _skippedTags.Withdraw(fullRef); }
else
{
var refused = rejected.Select(r => r.Ref).ToHashSet(StringComparer.Ordinal);
foreach (var fullRef in fullReferences)
if (!refused.Contains(fullRef)) _skippedTags.Withdraw(fullRef);
}
}
}
@@ -2160,6 +2170,8 @@ public sealed class OpcUaClientDriver : IDriver, ITagDiscovery, IReadable, IWrit
internal void ReportRejectedItems(ISubscriptionHandle handle, IReadOnlyList<RejectedItem> rejected)
{
var now = Clock.GetUtcNow().UtcDateTime;
// #1039: the same refusals ride the skipped-tag report (/hosts), so an operator sees them without the log.
foreach (var r in rejected) _skippedTags.Append(r.Ref, r.Why);
lock (_deliveryLock)
{
foreach (var r in rejected)
@@ -143,7 +143,7 @@ public sealed class OpcUaClientDriverProbe : IDriverProbe
DisableHiResClock = true,
};
private static (string host, int port, string endpointUrl) ExtractTarget(OpcUaClientDriverOptions opts)
internal static (string host, int port, string endpointUrl) ExtractTarget(OpcUaClientDriverOptions opts)
{
// EndpointUrls wins over the convenience EndpointUrl when both are set.
var endpointUrl = opts.EndpointUrls.FirstOrDefault()
@@ -154,7 +154,11 @@ public sealed class OpcUaClientDriverProbe : IDriverProbe
if (!Uri.TryCreate(endpointUrl, UriKind.Absolute, out var uri))
return (string.Empty, 0, string.Empty);
var host = uri.Host;
// IdnHost, not Host: Uri.Host drops an IPv6 zone index (#819), so a link-local endpoint was dialled zone-less and
// failed the preflight as "Connect failed" before any OPC UA traffic. IdnHost is what the SDK's own transport
// dials (and it carries no brackets, which Socket.ConnectAsync's host overload does not want). An RFC 6874 "%25"
// delimiter is normalised to the "%" the socket layer expects.
var host = uri.IdnHost.Replace("%25", "%", StringComparison.Ordinal);
var port = uri.IsDefaultPort ? 4840 : uri.Port;
return (host, port, endpointUrl);
}