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
@@ -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;