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
@@ -184,6 +184,26 @@ public sealed class GrpcDataHistoryClientTests
a.Headers.Count.ShouldBe(probes, "backing off: no new probe");
}
[Fact]
public async Task A_caller_cancel_during_the_call_surfaces_as_OperationCanceledException()
{
var a = new Endpoint("a") { Delay = TimeSpan.FromSeconds(30) };
await using var connection = Connect([a]);
using var cancel = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
await Should.ThrowAsync<OperationCanceledException>(() => connection.ReadHistoryAsync(Request(), cancel.Token));
}
[Fact]
public async Task A_failure_mid_assembly_releases_the_origin_chunk_tokens_it_holds()
{
var a = new Endpoint("a") { Chunks = 3, FailAfterFirst = Grpc.Core.StatusCode.Internal };
await using var connection = Connect([a]);
(await connection.ReadHistoryAsync(Request(), TestContext.Current.CancellationToken)).Single().StatusCode.ShouldBe(0x80310000u);
var deadline = DateTime.UtcNow.AddSeconds(10);
while (a.Released.Count == 0 && DateTime.UtcNow < deadline) await Task.Delay(20, TestContext.Current.CancellationToken);
a.Released.ShouldContain(t => t.SequenceEqual(new byte[] { 7, 7 }), "the chunk token the failed follow-up carried is released");
}
private static GrpcDataConnection Connect(Endpoint[] endpoints, TimeSpan? historyTimeout = null)
{
var manager = new DataConnectionManager("consumer", 1);
@@ -203,6 +223,10 @@ public sealed class GrpcDataHistoryClientTests
public bool Ready { get; set; } = true;
public bool Granted { get; set; } = true;
public bool Malformed { get; set; }
/// <summary>Delay before answering a HistoryRead (honours the request's token).</summary>
public TimeSpan? Delay { get; set; }
/// <summary>Fail every HistoryRead after the first with this status.</summary>
public Grpc.Core.StatusCode? FailAfterFirst { get; set; }
public int Chunks { get; init; } = 1;
public byte[]? PageToken { get; init; }
public int HistoryCalls;
@@ -232,13 +256,19 @@ public sealed class GrpcDataHistoryClientTests
}
else
{
Interlocked.Increment(ref HistoryCalls);
var call = Interlocked.Increment(ref HistoryCalls);
if (Fail is { } fail) return Status(fail);
if (Delay is { } delay) await Task.Delay(delay, token);
if (request.Headers.TryGetValues("grpc-timeout", out var timeouts)) LastTimeout = ParseTimeout(timeouts.Single());
var bytes = await request.Content!.ReadAsByteArrayAsync(token);
var wire = W.HistoryReadRequest.Parser.ParseFrom(bytes.AsSpan(5).ToArray());
LastBudgetTimeout = TimeSpan.FromTicks(wire.Budget.TimeoutTicks);
var response = new W.HistoryReadResponse { Serving = DataProtoMapper.EncodeServing(Serving) };
if (!wire.ReleaseContinuations && call > 1 && FailAfterFirst is { } later)
{
LastContinuations = wire.Items.Where(x => x.HasContinuation).Select(x => x.Continuation.ToByteArray()).ToList();
return Status(later);
}
if (wire.ReleaseContinuations)
{
foreach (var item in wire.Items) Released.Add(item.Continuation.ToByteArray());