fix(grpc-data): H2 review2-1 - a cancelled read never leaks its remote history slot

A pre-cancelled token throws before taking a slot, and the port delegate is dispatched with Task.Run(None) so its
finally always releases (a Task.Run on a cancelled token never runs the delegate).
This commit is contained in:
Joseph Doherty
2026-11-23 23:09:20 -05:00
parent d8712b871c
commit abc6a1b068
2 changed files with 36 additions and 1 deletions
@@ -98,6 +98,9 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
/// </summary>
public static int RemoteSlots(int batches) => Math.Max(1, batches - Math.Max(1, batches / 4));
/// <summary>Remote slots currently free (diagnostics/tests).</summary>
public int AvailableRemoteSlots => Slots().CurrentCount;
private SemaphoreSlim Slots()
{
if (_remoteSlots is { } slots) return slots;
@@ -139,6 +142,7 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
// ONE scope per request: it snapshots the serving incarnation at first use (H1 review note).
var scope = new WiringObservedScope(new PeerHistoryContinuationScope(cursors.Store, principal, servingIncarnation));
// Remote peers never take the slots reserved for local clients (H2 review): full => BadTooManyOperations, unsent.
cancellationToken.ThrowIfCancellationRequested();
var slots = Slots();
if (!slots.Wait(0))
return Task.FromResult<IReadOnlyList<OriginHistoryOutcome>>(items
@@ -155,7 +159,8 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
return new OriginHistoryOutcome(status, o.Values.Select(Sample).ToArray(), o.ContinuationPoint,
scope.Faulted && status == StatusCodes.BadInternalError);
}).ToArray();
}, cancellationToken);
}, CancellationToken.None); // NOT the request token: a cancelled Task.Run never runs the delegate, and the slot would leak.
// (The token still reaches ReadHistory, which ends in-flight nodes BadTimeout.)
}
private static OriginHistorySample Sample(Opc.Ua.DataValue value)
@@ -52,6 +52,36 @@ public sealed class OriginHistoryLimiterTests(HistoryReadPortServerFixture fx) :
}
}
[Fact]
public async Task A_pre_cancelled_read_returns_its_remote_slot()
{
await fx.Gate.WaitAsync(TestContext.Current.CancellationToken);
var source = new BlockingSource();
source.Release.TrySetResult(); // reads complete at once
try
{
fx.Nm.HistorianDataSource = source;
var port = new OriginHistoryPort(fx.Nm);
var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(5)), 1, false);
var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
var slots = OriginHistoryPort.RemoteSlots(fx.Nm.MaxConcurrentHistoryReadBatches);
using var cancelled = new CancellationTokenSource();
await cancelled.CancelAsync();
for (var i = 0; i < slots + 3; i++)
await Should.ThrowAsync<OperationCanceledException>(() => port.ReadAsync(new OriginHistoryCursors("peer", "c" + i, 4), "peer",
() => "inc", details, [item], cancelled.Token));
port.AvailableRemoteSlots.ShouldBe(slots, "every cancelled dispatch gave its slot back");
var served = (await port.ReadAsync(new OriginHistoryCursors("peer", "ok", 4), "peer", () => "inc", details, [item],
CancellationToken.None)).Single();
served.StatusCode.ShouldNotBe(StatusCodes.BadTooManyOperations.Code);
}
finally
{
fx.Nm.HistorianDataSource = NullHistorianDataSource.Instance;
fx.Gate.Release();
}
}
private sealed class BlockingSource : IHistorianDataSource
{
private int _calls;