fix(abcip): #983 RMW lock keying + whole-word lock, bit selector 0-63, interval floors, cycle guard on struct type, browse surfaces transport faults (P-23 a-e)
This commit is contained in:
@@ -1602,7 +1602,22 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
// Serialise Encode → Write → GetStatus against the shared runtime — the same cached
|
||||
// Tag handle may be in use by a concurrent ReadAsync or poll loop.
|
||||
var opLock = device.GetRuntimeLock(def.Name);
|
||||
await opLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
// P-23a: a whole-word write to an integer tag shares the read-modify-write lock of its bit writers
|
||||
// (`Flags.3`): both address the same controller word through DIFFERENT runtimes, so without it a bit
|
||||
// RMW's read-then-write could land over a whole-word write made between them (lost update).
|
||||
var hostLock = arrayElements is null && parsedPath is { BitIndex: null } && IsIntegerHostType(def.DataType)
|
||||
? device.GetRmwLock(parsedPath.ToLibplctagName())
|
||||
: null;
|
||||
if (hostLock is not null) await hostLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await opLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
hostLock?.Release();
|
||||
throw;
|
||||
}
|
||||
int status;
|
||||
uint? refusal;
|
||||
int size;
|
||||
@@ -1633,6 +1648,7 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
finally
|
||||
{
|
||||
opLock.Release();
|
||||
hostLock?.Release();
|
||||
}
|
||||
|
||||
if (refusal is { } refused)
|
||||
@@ -1823,6 +1839,11 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>True for the integer types a bit selector (<c>Tag.n</c>) can address, i.e. a possible RMW host.</summary>
|
||||
private static bool IsIntegerHostType(AbCipDataType type) => type is AbCipDataType.SInt or AbCipDataType.USInt
|
||||
or AbCipDataType.Int or AbCipDataType.UInt or AbCipDataType.DInt or AbCipDataType.UDInt
|
||||
or AbCipDataType.LInt or AbCipDataType.ULInt;
|
||||
|
||||
/// <summary>
|
||||
/// Read-modify-write one bit within an integer parent (SINT / INT / DINT / LINT — Logix allows <c>.n</c> on
|
||||
/// each). Creates / reuses a parallel parent runtime (distinct from the bit-selector handle) + serialises
|
||||
@@ -2228,7 +2249,7 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
() => capturedDiscoveredFolder.Folder(fullName, discovered.Name));
|
||||
var anyEmitted = await FanOutDiscoveredUdtMembersAsync(
|
||||
udtFolder, device.HostAddress, fullName, shape, discovered.ReadOnly,
|
||||
depth: 1, emitted, visited: [discovered.Name], cancellationToken)
|
||||
depth: 1, emitted, visited: [StructKey(discovered.TemplateInstanceId, discovered.Name)], cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (anyEmitted) continue; // expanded — don't emit the placeholder Variable
|
||||
// Empty shape (no usable atomic leaf): degrade to a single Variable below.
|
||||
@@ -2341,6 +2362,18 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
return size == width * Math.Max(1, def.ElementCount) ? null : AbCipStatusMapper.BadTypeMismatch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cycle-guard key of a struct on the fan-out path: its template instance id when the controller supplied
|
||||
/// one (identity of the struct TYPE), else its name (seeded / test shapes carry none). Keying on the member
|
||||
/// NAME alone skipped a legitimate member whose nested type happened to contain a member of the same name
|
||||
/// (P-23d) — only a true re-entry of the same struct type is cyclic.
|
||||
/// </summary>
|
||||
/// <param name="templateInstanceId">The struct's CIP template instance id, if known.</param>
|
||||
/// <param name="name">The member / tag name, the fallback key.</param>
|
||||
/// <returns>The guard key.</returns>
|
||||
private static string StructKey(uint? templateInstanceId, string name) =>
|
||||
templateInstanceId is { } id ? $"tpl:{id}" : $"name:{name}";
|
||||
|
||||
/// <summary>
|
||||
/// Recursively fan a controller-discovered UDT shape out into Variable nodes — one per
|
||||
/// ATOMIC leaf — under the folder produced by <paramref name="folder"/>. Mirrors the
|
||||
@@ -2416,7 +2449,8 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
// live a level deeper, so the cap is checked against depth + 1.
|
||||
if (member.ArrayLength > 1) continue;
|
||||
if (depth + 1 > MaxUdtDepth) continue;
|
||||
if (!visited.Add(member.Name)) continue; // cyclic UDT reference — stop here
|
||||
var structKey = StructKey(member.NestedTemplateId, member.Name);
|
||||
if (!visited.Add(structKey)) continue; // cyclic UDT reference (same struct TYPE already on this path) — stop here
|
||||
if (nested is not null)
|
||||
{
|
||||
// Materialise the nested sub-folder lazily under the parent folder (itself lazy),
|
||||
@@ -2430,7 +2464,7 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
.ConfigureAwait(false);
|
||||
anyEmitted |= nestedEmitted;
|
||||
}
|
||||
visited.Remove(member.Name);
|
||||
visited.Remove(structKey);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -2638,7 +2672,10 @@ public sealed class AbCipDriver : IDriver, IReadable, IWritable, ITagDiscovery,
|
||||
public System.Collections.Concurrent.ConcurrentDictionary<string, IAbCipTagRuntime> ParentRuntimes { get; } =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, SemaphoreSlim> _rmwLocks = new();
|
||||
// OrdinalIgnoreCase like ParentRuntimes: `Flags.3` and `FLAGS.4` share one parent runtime, so they must share
|
||||
// its lock (P-23a — a case-sensitive key gave them two locks over one handle: a lost update).
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, SemaphoreSlim> _rmwLocks =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>Gets or creates a semaphore for coordinating RMW (read-modify-write) operations on a parent tag.</summary>
|
||||
/// <param name="parentTagName">The name of the parent tag.</param>
|
||||
|
||||
@@ -98,7 +98,7 @@ public static class AbCipDriverFactoryExtensions
|
||||
Probe = new AbCipProbeOptions
|
||||
{
|
||||
Enabled = dto.Probe?.Enabled ?? true,
|
||||
Interval = TimeSpan.FromMilliseconds(dto.Probe?.IntervalMs ?? 5_000),
|
||||
Interval = IntervalOrDefault(dto.Probe?.IntervalMs, 5_000),
|
||||
Timeout = PositiveTimeoutOrDefault(dto.Probe?.TimeoutMs, 2_000),
|
||||
ProbeTagPath = dto.Probe?.ProbeTagPath,
|
||||
},
|
||||
@@ -111,7 +111,7 @@ public static class AbCipDriverFactoryExtensions
|
||||
EnableControllerBrowse = dto.EnableControllerBrowse ?? false,
|
||||
BrowsePageDelay = TimeSpan.FromMilliseconds(Math.Max(0, dto.BrowsePageDelayMs ?? 5)),
|
||||
EnableAlarmProjection = dto.EnableAlarmProjection ?? false,
|
||||
AlarmPollInterval = TimeSpan.FromMilliseconds(dto.AlarmPollIntervalMs ?? 1_000),
|
||||
AlarmPollInterval = IntervalOrDefault(dto.AlarmPollIntervalMs, 1_000),
|
||||
EnableDeclarationOnlyUdtGrouping = dto.EnableDeclarationOnlyUdtGrouping ?? false,
|
||||
MaxConcurrentReads = dto.MaxConcurrentReads ?? 8,
|
||||
};
|
||||
@@ -131,6 +131,19 @@ public static class AbCipDriverFactoryExtensions
|
||||
private static TimeSpan PositiveTimeoutOrDefault(int? ms, int defaultMs) =>
|
||||
TimeSpan.FromMilliseconds(ms is int v and > 0 ? v : defaultMs);
|
||||
|
||||
/// <summary>Floor on a loop interval (probe / alarm poll): 0 would hot-loop against the PLC, a negative value makes <c>Task.Delay</c> throw out of the loop (P-23c).</summary>
|
||||
internal const int MinIntervalMs = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Maps an operator-supplied loop interval (ms): null ⇒ <paramref name="defaultMs"/>, anything below
|
||||
/// <see cref="MinIntervalMs"/> (zero, negative) is clamped up to it.
|
||||
/// </summary>
|
||||
/// <param name="ms">The operator-supplied interval in milliseconds, or null.</param>
|
||||
/// <param name="defaultMs">The default applied when <paramref name="ms"/> is null.</param>
|
||||
/// <returns>An interval of at least <see cref="MinIntervalMs"/>.</returns>
|
||||
private static TimeSpan IntervalOrDefault(int? ms, int defaultMs) =>
|
||||
TimeSpan.FromMilliseconds(Math.Max(MinIntervalMs, ms ?? defaultMs));
|
||||
|
||||
// tagName null = a driver-level (or device-level) config field: the error text names the driver config,
|
||||
// not a tag. Never pass a placeholder like "device" as tagName — it renders as "tag 'device'".
|
||||
private static T ParseEnum<T>(string? raw, string? tagName, string driverInstanceId, string field,
|
||||
|
||||
@@ -87,7 +87,7 @@ public sealed record AbCipTagPath(
|
||||
|
||||
int? bitIndex = null;
|
||||
if (parts.Count >= 2 && int.TryParse(parts[^1], out var maybeBit)
|
||||
&& maybeBit is >= 0 and <= 31
|
||||
&& maybeBit is >= 0 and <= 63
|
||||
&& !parts[^1].Contains('['))
|
||||
{
|
||||
bitIndex = maybeBit;
|
||||
|
||||
@@ -57,10 +57,13 @@ internal sealed class SymbolListingEnumerator : IAbCipTagEnumerator
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception)
|
||||
catch (Exception ex) when (!AbCipRefusals.IsConnectionFault(ex))
|
||||
{
|
||||
// A refusal (the controller answered "no" for this program) or a listing that cannot be read: skip it.
|
||||
continue;
|
||||
}
|
||||
// A connection-level fault (timeout, closed session, I/O) is NOT "this program is unreadable" — the rest of
|
||||
// the listing would be silently missing with no log (P-23e) — so it propagates and the browse reports it.
|
||||
|
||||
foreach (var tag in CipSymbolObjectDecoder.Decode(programBuffer, programScope: program))
|
||||
yield return tag;
|
||||
|
||||
Reference in New Issue
Block a user