fix(grpc-data): H2 review2-7d/e - caller cancel is OperationCanceledException; abandoned assembly releases chunk tokens

A caller cancel during a history call surfaced as RpcException(Cancelled); it now throws OperationCanceledException.
When an assembly is abandoned (timeout, communication failure, unsupported, cancel) after a follow-up, the origin chunk
tokens it carried are released best effort.
This commit is contained in:
Joseph Doherty
2026-11-23 23:20:53 -05:00
parent 808e1fe985
commit 28de03783f
2 changed files with 47 additions and 2 deletions
@@ -158,8 +158,15 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
Headers(selected.Channel, RequestedFeatures(selected.Capabilities)), DateTime.UtcNow + deadline, linked.Token))
.ResponseAsync.ConfigureAwait(false);
}
catch (Exception ex) when (cancellationToken.IsCancellationRequested && ex is RpcException or OperationCanceledException)
{
// A caller cancel is never an outcome: surface it as cancellation, and let go of held chunk tokens.
if (!first) ReleaseChunkTokens(selected, current);
throw new OperationCanceledException("History read cancelled by the caller.", ex, cancellationToken);
}
catch (RpcException ex) when (ex.StatusCode is StatusCode.FailedPrecondition or StatusCode.Unimplemented)
{
if (!first) ReleaseChunkTokens(selected, current);
foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, BadHistoryOperationUnsupported, [], null);
break;
}
@@ -167,15 +174,17 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
{
// The call's own deadline, or the origin cancelling it at ITS unary timeout (the effective ceiling is
// min(HistoryTimeout, origin DataConnections:UnaryTimeout)): a timeout, never an endpoint failure.
if (!first) ReleaseChunkTokens(selected, current);
foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, BadTimeout, [], null);
break;
}
catch (RpcException ex) when (first && ex.StatusCode is StatusCode.Unavailable or StatusCode.Unauthenticated or StatusCode.PermissionDenied
&& !cancellationToken.IsCancellationRequested)
{ RecordFailure(ex); return null; }
catch (RpcException ex) when (ex.StatusCode != StatusCode.Cancelled || !cancellationToken.IsCancellationRequested)
catch (RpcException ex)
{
RecordFailure(ex);
if (!first) ReleaseChunkTokens(selected, current); // the origin still holds their remainders
foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, BadNoCommunication, [], null);
break;
}
@@ -261,6 +270,12 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
}
}
/// <summary>Best effort: a follow-up's items carry origin CHUNK tokens; release them when the assembly is abandoned.</summary>
private void ReleaseChunkTokens(Selected selected, IEnumerable<DataHistoryReadItem> items)
{
foreach (var item in items) _ = ReleaseOnEndpointAsync(selected, item.Continuation);
}
private async Task ReleaseOnEndpointAsync(Selected selected, byte[]? token)
{
if (token is null) return;