fix(grpc-data): H3 review round 3 nits b/c/d — no origin cursor stranded on Bad, scope moves or a serve-time throw

- b: ProjectedHistoryAccess releases the prior origin cursor on any Bad answer
  to a resumed node (and on a mismatched answer) — an unsent refusal left it
  open at the origin; idempotent.
- c: DataConnectionDeploymentLease, when its scope moves while a history
  answer is in flight, releases the dropped answer's new continuations (best
  effort); resumed items are BadContinuationPointInvalid, fresh ones
  BadNoCommunication.
- d: ProjectedHistoryRead: a throw while serving outcome i (FromClr on an
  unconvertible value, a store fault) releases outcome i's cursor unless the
  store already owns it, and every later outcome's.

Each red on revert (mutation).
This commit is contained in:
Joseph Doherty
2026-11-24 07:15:16 -05:00
parent 30d66fdc7c
commit f1b23abcb4
6 changed files with 157 additions and 7 deletions
@@ -198,16 +198,34 @@ public sealed partial class OtOpcUaNodeManager
return;
}
for (var i = 0; i < pending.Count; i++)
var done = 0; // outcomes [0, done) are fully handed over (a continuation saved to the store, or released)
ProjectedHistoryContinuation? handedOver = null; // outcome `done`'s continuation once the store owns it (or released it)
try
{
for (var i = 0; i < pending.Count; i++)
{
handedOver = null;
ServeProjectedOutcome(pending[i].Handle, outcomes[i]);
done = i + 1;
}
}
catch
{
// H3 review round 3 nit (d): a throw for outcome i (FromClr on an unconvertible value, a store fault) strands no
// origin cursor: outcome i's — unless the store already owns it — and every later outcome's are released.
for (var j = done; j < outcomes.Count; j++)
if (outcomes[j].Continuation is { } c && !ReferenceEquals(c, handedOver)) c.Release();
throw;
}
void ServeProjectedOutcome(NodeHandle handle, ProjectedHistoryOutcome outcome)
{
var handle = pending[i].Handle;
var outcome = outcomes[i];
var status = new StatusCode(outcome.StatusCode);
if (StatusCode.IsBad(status))
{
outcome.Continuation?.Release();
SetProjectedStatus(handle, results, errors, status);
continue;
return;
}
byte[]? cp = null;
@@ -215,6 +233,7 @@ public sealed partial class OtOpcUaNodeManager
{
var key = MapKey(handle.NodeId);
cp = SaveRemoteContinuation(session, store, key.Realm, key.Id, HistoryBindingTag(handle.NodeId), epoch, next);
handedOver = next; // saved (store-owned) or already released by SaveRemoteContinuation
}
var values = new List<DataValue>(outcome.Samples.Count);
@@ -541,13 +541,25 @@ public sealed class DataConnectionDeploymentLease : IDataConnection, IDataHistor
catch (Exception ex) when (ex is InvalidOperationException or UnauthorizedAccessException or OperationCanceledException
&& !cancellationToken.IsCancellationRequested)
{
// The scope moved while the answer was in flight: the answer is dropped, so the new origin continuations it carries are
// released (best effort) rather than stranded until the origin's idle expiry (H3 review round 3 nit c).
var orphans = value.Select(x => x.Continuation).OfType<byte[]>().ToArray();
foreach (var item in value) item.Dispose();
return Unavailable(request); // the scope moved while the answer was in flight
if (orphans.Length > 0) _ = ReleaseQuietlyAsync(history, orphans);
return Unavailable(request);
}
}
private static async Task ReleaseQuietlyAsync(IDataHistoryService history, byte[][] continuations)
{
try { await history.ReleaseAsync(continuations, CancellationToken.None).ConfigureAwait(false); }
catch (Exception ex) when (ex is not OutOfMemoryException) { } // best effort; the origin's idle expiry is the backstop
}
/// <summary>A resumed item's token cannot be presented again: <c>BadContinuationPointInvalid</c>; a fresh one <c>BadNoCommunication</c>.</summary>
private static DataHistoryReadResult[] Unavailable(DataHistoryReadRequest request) => request.Items
.Select(x => new DataHistoryReadResult(x.CorrelationId, GrpcDataConnection.BadNoCommunication, [], null)).ToArray();
.Select(x => new DataHistoryReadResult(x.CorrelationId,
x.Continuation is not null ? GrpcDataConnection.BadContinuationPointInvalid : GrpcDataConnection.BadNoCommunication, [], null)).ToArray();
Task<IReadOnlyList<DataHistoryReadResult>> IDataHistoryService.ReadAsync(DataHistoryReadRequest request, CancellationToken cancellationToken) =>
ReadHistoryAsync(request, cancellationToken);
@@ -302,6 +302,7 @@ public sealed class ProjectedHistoryAccess : IProjectedHistoryAccess, IDisposabl
var answer = answers[k];
if (answer.CorrelationId != items[k].CorrelationId)
{
(nodes[i].Resume as RemoteCursor)?.ReleaseOrigin();
outcomes[i] = Fail(BadNoCommunication);
continue; // its token (if any) is released in the finally
}
@@ -326,6 +327,9 @@ public sealed class ProjectedHistoryAccess : IProjectedHistoryAccess, IDisposabl
: null;
outcomes[i] = new ProjectedHistoryOutcome(answer.StatusCode, samples, next);
assigned[k] = true;
// Round 3 nit (b): a Bad answer to a resume may have left the prior origin cursor unconsumed (refused
// unsent, a failed call) — release it; idempotent, and a no-op at the origin when it was consumed.
if ((answer.StatusCode & 0x80000000u) != 0 && nodes[i].Resume is RemoteCursor resumedFrom) resumedFrom.ReleaseOrigin();
}
}
finally