fix(grpc-data): H3 review round 3 S5 — a cancel by redeploy/shutdown is not a timeout

GrpcDataConnection.ServeAsync mapped RpcException(Cancelled) without a caller
cancel to BadTimeout even when the linked channel-retired / scope / shutdown
token fired. That case is now BadNoCommunication (resumed:
BadContinuationPointInvalid), releases held chunk tokens and stops a split
request sending further sub-batches. ProjectedHistoryAccess's outer catch no
longer reads any escaped OperationCanceledException as a timeout: only the
caller's cancel or the deadline is BadTimeout, otherwise classified as
FailCancelled does. Red on revert (client test: scope moved mid-call); the
outer-catch change has no dedicated test (nothing reachable throws past
ReadGroupAsync's own catch).
This commit is contained in:
Joseph Doherty
2026-11-24 07:11:06 -05:00
parent 9c2d6273aa
commit 30d66fdc7c
3 changed files with 39 additions and 5 deletions
@@ -181,7 +181,7 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
var call = await ServeAsync(selected, endpoint, request with { Budget = request.Budget with { MaxBytes = left } }, items, chunk, start,
cancellationToken).ConfigureAwait(false);
served = call.Results;
failed = call.EndpointFailed; // answered, but the endpoint failed mid-way: send it nothing more (nit a)
failed = call.EndpointFailed; // answered, but the endpoint failed or retired mid-way: send it nothing more (nit a)
}
catch (Exception ex) when (offset > 0 && !cancellationToken.IsCancellationRequested
&& ex is InvalidOperationException or ArgumentException or RpcException or HttpRequestException)
@@ -230,7 +230,7 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
/// <summary>
/// Reads on one selected endpoint and assembles its chunks. Results null = the endpoint failed before answering (a fresh
/// read may move); <c>EndpointFailed</c> = it answered (per node) but failed on the way, so a caller splitting a
/// read may move); <c>EndpointFailed</c> = it answered (per node) but failed or retired on the way, so a caller splitting a
/// request sends it nothing more. Anything that escapes first releases every origin token this assembly holds — finished
/// nodes' continuations, chunk tokens, and the tokens of a response it rejected (H3 review round 3 S6).
/// </summary>
@@ -282,6 +282,16 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, BadHistoryOperationUnsupported, [], null);
break;
}
catch (Exception ex) when (!cancellationToken.IsCancellationRequested && Retiring(selected)
&& ex is OperationCanceledException or RpcException { StatusCode: StatusCode.Cancelled })
{
// Not the caller and not a deadline: the channel retired, the scope moved or the connection is shutting down
// (a redeploy). Not a timeout and not an endpoint fault (round 3 S5): the read cannot continue here.
if (!first) ReleaseChunkTokens(selected, current);
foreach (var k in map) final[k] ??= new(batch[k].CorrelationId, batch[k].Continuation is not null ? BadContinuationPointInvalid : BadNoCommunication, [], null);
endpointFailed = true;
break;
}
catch (RpcException ex) when (ex.StatusCode is StatusCode.DeadlineExceeded or StatusCode.Cancelled && !cancellationToken.IsCancellationRequested)
{
// The call's own deadline, or the origin cancelling it at ITS unary timeout (the effective ceiling is
@@ -367,6 +377,9 @@ public sealed partial class GrpcDataConnection : IDataHistoryService
}
}
/// <summary>The call was cancelled by the connection itself: its channel retired, its scope moved, or it is shutting down.</summary>
private bool Retiring(Selected selected) =>
selected.Channel.Retired.IsCancellationRequested || _scope.CancellationToken.IsCancellationRequested || _shutdown.IsCancellationRequested;
/// <summary>Releases continuations on their issuing endpoints (<see cref="IDataHistoryService.ReleaseAsync"/>); best effort.</summary>
public async Task ReleaseHistoryAsync(IReadOnlyList<byte[]> continuations, CancellationToken cancellationToken)
@@ -199,14 +199,16 @@ public sealed class ProjectedHistoryAccess : IProjectedHistoryAccess, IDisposabl
}
catch (Exception ex) when (ex is not OutOfMemoryException)
{
// Last resort (H3 review D1): whatever escaped, fail per node — a timeout when the deadline fired — and release every
// Last resort (H3 review D1): whatever escaped, fail per node — a timeout only when the caller or the deadline
// cancelled; any other cancel (a retired lease) is classified as FailCancelled does (round 3 S5) — and release every
// origin cursor this request still owns. Nothing escapes to the SDK override.
var timedOut = cancellationToken.IsCancellationRequested || Remaining() <= TimeSpan.Zero || ex is OperationCanceledException;
var timedOut = cancellationToken.IsCancellationRequested || Remaining() <= TimeSpan.Zero;
for (var i = 0; i < nodes.Count; i++)
{
if (outcomes[i] is not null) continue;
var resumed = nodes[i].Resume is RemoteCursor;
nodes[i].Resume?.ReleaseEvenIfClaimed();
outcomes[i] = Fail(timedOut ? BadTimeout : BadNoCommunication);
outcomes[i] = Fail(timedOut ? BadTimeout : ex is OperationCanceledException && resumed ? BadContinuationPointInvalid : BadNoCommunication);
}
return outcomes.Select(o => o!).ToArray();
@@ -351,6 +351,25 @@ public sealed class GrpcDataHistoryClientTests
ReleasedCount(a, [7, 7]).ShouldBeGreaterThan(0, "n1's chunk tokens are released");
}
[Fact]
[Trait("Phase", "H3")]
public async Task A_call_cancelled_by_a_scope_move_is_not_a_timeout()
{
// S5: neither the caller nor the deadline cancelled the call — the connection's scope moved (a redeploy).
DataConnectionManager? manager = null;
var a = new Endpoint("a") { HangOnDataCall = 1 };
await using var connection = Connect([a], onManager: m => manager = m);
var request = new DataHistoryReadRequest(Request().Details,
[new("fresh", View), new("resumed", View, null, GrpcDataConnection.Wrap(0, "boot-a", [1, 2]))], Budget);
var read = connection.ReadHistoryAsync(request, TestContext.Current.CancellationToken);
var deadline = DateTime.UtcNow.AddSeconds(10);
while (Volatile.Read(ref a.DataCalls) < 1 && DateTime.UtcNow < deadline) await Task.Delay(10, TestContext.Current.CancellationToken);
manager!.ApplyScope(new Dictionary<string, IReadOnlySet<string>>());
var results = await read;
results[0].StatusCode.ShouldBe(0x80310000u, "fresh: BadNoCommunication, not BadTimeout");
results[1].StatusCode.ShouldBe(0x804A0000u, "resumed: BadContinuationPointInvalid");
}
[Fact]
public async Task A_failure_mid_assembly_releases_the_origin_chunk_tokens_it_holds()
{