48b0173ae9
Found by the H13 stress test (200 re-inits under a concurrent reader): a read that resolved the device just before the table swap could cold-create a runtime into the retired state after its teardown had disposed it (leaked native handle / lease), and could hit a per-tag SemaphoreSlim the teardown had disposed, which the generic catch reported as a comms fault (Degraded). - DeviceState.Retire marks the state retired before disposing handles; each handle is removed-then-disposed exactly once. A creator re-checks the flag after its TryAdd (fenced, Dekker-paired with Retire) and disposes the orphan. - A failure on a retired device is the teardown's doing: BadCommunicationError for that one call, no evict, no health signal. - Per-tag/RMW gates are no longer disposed (async-only waits hold no handle), matching the ConnectGate rationale. Refs #737 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
196 lines
8.6 KiB
C#
196 lines
8.6 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;
|
|
|
|
/// <summary>
|
|
/// Lifecycle overlap the runtime can produce (review 07 H13/F10, Lane A): the final stop runs
|
|
/// <c>ShutdownAsync</c> then <c>DisposeAsync</c> under one bounded budget, so a slow shutdown is abandoned and
|
|
/// the dispose runs CONCURRENTLY with it; an abandoned <c>InitializeAsync</c> can overlap the next attempt's
|
|
/// shutdown. Device tables are build-and-swapped, so each teardown owns a disjoint set of device states.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class AbCipLifecycleConcurrencyTests
|
|
{
|
|
private const string Host = "ab://10.0.0.5/1,0";
|
|
|
|
/// <summary>A fake whose reads ignore cancellation for a while, so a teardown waits on the probe loop.</summary>
|
|
private sealed class SlowTag(AbCipTagCreateParams p, TimeSpan readDelay) : FakeAbCipTag(p)
|
|
{
|
|
private int _disposeCount;
|
|
|
|
public int DisposeCount => Volatile.Read(ref _disposeCount);
|
|
|
|
public override async Task ReadAsync(CancellationToken cancellationToken)
|
|
{
|
|
await base.ReadAsync(cancellationToken);
|
|
if (readDelay > TimeSpan.Zero) await Task.Delay(readDelay, CancellationToken.None);
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
Interlocked.Increment(ref _disposeCount);
|
|
base.Dispose();
|
|
}
|
|
}
|
|
|
|
private static (AbCipDriver Driver, FakeAbCipTagFactory Factory) Build(TimeSpan probeReadDelay)
|
|
{
|
|
var factory = new FakeAbCipTagFactory
|
|
{
|
|
Customise = p => new SlowTag(p, p.TagName == "ProbeTag" ? probeReadDelay : TimeSpan.Zero) { Value = 1 },
|
|
};
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions(Host)],
|
|
RawTags = AbCipRawTags.From(new AbCipTagDefinition("Speed", Host, "Motor1.Speed", AbCipDataType.DInt)),
|
|
Probe = new AbCipProbeOptions
|
|
{
|
|
Enabled = true,
|
|
ProbeTagPath = "ProbeTag",
|
|
Interval = TimeSpan.FromMilliseconds(10),
|
|
Timeout = TimeSpan.FromMilliseconds(200),
|
|
},
|
|
}, "abcip-lifecycle", factory);
|
|
return (drv, factory);
|
|
}
|
|
|
|
private static async Task WaitUntilAsync(Func<bool> condition, string what)
|
|
{
|
|
var deadline = DateTime.UtcNow.AddSeconds(5);
|
|
while (!condition())
|
|
{
|
|
if (DateTime.UtcNow > deadline) throw new TimeoutException(what);
|
|
await Task.Delay(5);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shutdown and Dispose racing each other tear the device down exactly once: the data runtime is
|
|
/// disposed once (not once per teardown), neither call throws, and the probe loop has exited.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Dispose_concurrent_with_a_running_Shutdown_tears_each_state_down_exactly_once()
|
|
{
|
|
var (drv, factory) = Build(probeReadDelay: TimeSpan.FromMilliseconds(300));
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
(await drv.ReadAsync(["Speed"], CancellationToken.None))[0].StatusCode.ShouldBe(AbCipStatusMapper.Good);
|
|
await WaitUntilAsync(() => factory.Tags.ContainsKey("ProbeTag"), "probe never started");
|
|
var state = drv.GetDeviceState(Host)!;
|
|
var probeLoop = state.ProbeTask!;
|
|
var data = (SlowTag)factory.Tags["Motor1.Speed"];
|
|
|
|
var shutdown = drv.ShutdownAsync(CancellationToken.None);
|
|
var dispose = drv.DisposeAsync().AsTask();
|
|
await Task.WhenAll(shutdown, dispose);
|
|
|
|
data.DisposeCount.ShouldBe(1);
|
|
probeLoop.IsCompleted.ShouldBeTrue();
|
|
drv.GetDeviceState(Host).ShouldBeNull();
|
|
}
|
|
|
|
/// <summary>
|
|
/// An InitializeAsync that a ShutdownAsync overtakes publishes nothing: no device state and no probe
|
|
/// loop survive the shutdown. Positive control: an init that nothing overtakes still publishes.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task An_init_overtaken_by_Shutdown_publishes_no_device_and_starts_no_probe()
|
|
{
|
|
var (drv, factory) = Build(probeReadDelay: TimeSpan.FromMilliseconds(300));
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
await WaitUntilAsync(() => factory.Tags.ContainsKey("ProbeTag"), "probe never started");
|
|
var firstProbe = factory.Tags["ProbeTag"];
|
|
|
|
// The second init's own release waits on the slow first probe loop; the shutdown overtakes it there.
|
|
var init = drv.InitializeAsync("{}", CancellationToken.None);
|
|
await Task.Delay(50);
|
|
await drv.ShutdownAsync(CancellationToken.None);
|
|
await Should.ThrowAsync<OperationCanceledException>(() => init);
|
|
|
|
drv.GetDeviceState(Host).ShouldBeNull();
|
|
drv.GetHostStatuses().ShouldBeEmpty();
|
|
await Task.Delay(100);
|
|
factory.Tags["ProbeTag"].ShouldBeSameAs(firstProbe); // no second probe loop ever created a runtime
|
|
drv.GetHealth().State.ShouldNotBe(DriverState.Healthy);
|
|
|
|
// Positive control: a later init that nothing overtakes publishes the device again.
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
drv.GetDeviceState(Host).ShouldNotBeNull();
|
|
drv.GetHealth().State.ShouldBe(DriverState.Healthy);
|
|
await drv.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Readers of the device/routing tables (the retained poll engine, the actor's host-status pull) never
|
|
/// see a table mid-mutation while the driver re-initialises underneath them, never trip over a per-tag lock a
|
|
/// retire disposed, and never leak a runtime they cold-created into a device state a retire already tore down.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Readers_survive_repeated_reinitialisation_and_leak_no_runtime()
|
|
{
|
|
var created = new System.Collections.Concurrent.ConcurrentBag<FakeAbCipTag>();
|
|
var factory = new FakeAbCipTagFactory
|
|
{
|
|
Customise = p =>
|
|
{
|
|
var t = new FakeAbCipTag(p) { Value = 1 };
|
|
created.Add(t);
|
|
return t;
|
|
},
|
|
};
|
|
var drv = new AbCipDriver(new AbCipDriverOptions
|
|
{
|
|
Devices = [new AbCipDeviceOptions(Host)],
|
|
RawTags = AbCipRawTags.From(new AbCipTagDefinition("Speed", Host, "Motor1.Speed", AbCipDataType.DInt)),
|
|
Probe = new AbCipProbeOptions { Enabled = false },
|
|
}, "abcip-lifecycle", factory);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
using var stop = new CancellationTokenSource();
|
|
var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var badHealth = 0;
|
|
var reader = Task.Run(async () =>
|
|
{
|
|
var reads = 0;
|
|
while (!stop.IsCancellationRequested)
|
|
{
|
|
_ = drv.GetHostStatuses();
|
|
_ = drv.ResolveHost("Speed");
|
|
await drv.ReadAsync(["Speed"], CancellationToken.None);
|
|
if (drv.GetHealth().State == DriverState.Degraded) Interlocked.Increment(ref badHealth);
|
|
reads++;
|
|
started.TrySetResult();
|
|
}
|
|
return reads;
|
|
});
|
|
await started.Task.WaitAsync(TimeSpan.FromSeconds(5));
|
|
|
|
for (var i = 0; i < 200; i++)
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
stop.Cancel();
|
|
|
|
(await reader).ShouldBeGreaterThan(0); // positive control: the reader actually ran
|
|
Volatile.Read(ref badHealth).ShouldBe(0, "a re-init is not a comms fault");
|
|
await drv.DisposeAsync();
|
|
created.Where(t => !t.Disposed).ShouldBeEmpty("every runtime a reader created must be disposed by some teardown");
|
|
}
|
|
|
|
/// <summary>F10: the driver exposes only an async dispose; a sync <c>Dispose()</c> that blocked on the async
|
|
/// teardown (sync-over-async) is gone, and the runtime takes the <see cref="IAsyncDisposable"/> path.</summary>
|
|
[Fact]
|
|
public async Task Driver_is_async_disposable_only_and_DisposeAsync_stops_the_probe()
|
|
{
|
|
typeof(IDisposable).IsAssignableFrom(typeof(AbCipDriver)).ShouldBeFalse();
|
|
var (drv, factory) = Build(probeReadDelay: TimeSpan.Zero);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
await WaitUntilAsync(() => factory.Tags.ContainsKey("ProbeTag"), "probe never started");
|
|
var loop = drv.GetDeviceState(Host)!.ProbeTask!;
|
|
|
|
await drv.DisposeAsync();
|
|
await drv.DisposeAsync(); // idempotent
|
|
|
|
loop.IsCompleted.ShouldBeTrue();
|
|
factory.Tags["ProbeTag"].Disposed.ShouldBeTrue();
|
|
}
|
|
}
|