fix(historian): drain gateway event streams past the cap; cancellation is not a fault (lmxopcua#746)

A capped Gateway HistoryReadEvents read broke out of the gRPC stream as soon
as the page was full. Abandoning a gateway stream cancels the RPC, which the
gateway treats as a connection fault: it evicts its pooled historian session,
so the next read reconnects and concurrent reads on that session fail.

The read now keeps consuming the stream past the cap without collecting it,
bounded by ServerHistorian:Gateway:EventDrainBudget (default 10000 rows; 0 =
the old abandon-at-cap). Rows of a foreign source never count toward the
budget before the "more events" signal is seen, so an abandoned stream never
answers a page as complete. A paged-vs-unpaged fuzz (random tie series,
foreign rows, caps 1..500, budgets 0..10000) pins the page contents and the
drain; it found that abandon-before-signal case.

ReadEventsAsync and the native AtTime arm now rethrow a caller cancellation
instead of recording it as a backend failure.

Rebased from the unpushed remed/lane-H2 branch (originally authored 2026-10-22
in session_01Ub9hGmYLQvn9XETzw7CMsT) onto current master.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-10-22 16:26:57 -04:00
parent c86c7cbf35
commit 24609ececa
6 changed files with 431 additions and 11 deletions
@@ -71,6 +71,13 @@ public sealed class FakeHistorianGatewayClient : IHistorianGatewayClient
/// <summary>When set, ReadEvents serves <c>EventsSource(start, end)</c> instead of <see cref="Events"/>.</summary>
public Func<DateTime, DateTime, IEnumerable<HistorianEvent>>? EventsSource;
/// <summary>Events actually pulled by the consumer across every ReadEvents stream.</summary>
public int EventsYieldedCount;
/// <summary>ReadEvents streams the consumer enumerated to the END (never broke out of — R08:1.2).</summary>
public int EventStreamsCompleted;
/// <summary>ReadEvents streams the consumer disposed before their end (an abandoned gateway stream).</summary>
public int EventStreamsAbandoned;
/// <summary>Thrown (deferred to first enumeration) by every read method when set.</summary>
public Exception? ThrowOnRead;
@@ -204,7 +211,7 @@ public sealed class FakeHistorianGatewayClient : IHistorianGatewayClient
LastReadEventsEndUtc = endUtc;
LastReadEventsMaxEvents = maxEvents;
ReadEventsCallCount++;
return ToAsyncStream(EventsSource is null ? Events : EventsSource(startUtc, endUtc).ToList(), ThrowOnRead, ct);
return CountingEventStream(ToAsyncStream(EventsSource is null ? Events : EventsSource(startUtc, endUtc).ToList(), ThrowOnRead, ct));
}
public Task<WriteAck> WriteLiveValuesAsync(
@@ -272,6 +279,26 @@ public sealed class FakeHistorianGatewayClient : IHistorianGatewayClient
RawStreamsCompleted++;
}
private async IAsyncEnumerable<HistorianEvent> CountingEventStream(IAsyncEnumerable<HistorianEvent> inner)
{
var completed = false;
try
{
await foreach (var item in inner.ConfigureAwait(false))
{
Interlocked.Increment(ref EventsYieldedCount);
yield return item;
}
completed = true;
Interlocked.Increment(ref EventStreamsCompleted);
}
finally
{
// A consumer that breaks out (or a fault) disposes the stream early: on the real gateway that cancels the RPC.
if (!completed) Interlocked.Increment(ref EventStreamsAbandoned);
}
}
private static async IAsyncEnumerable<T> ToAsyncStream<T>(
IReadOnlyList<T> items,
Exception? error,