Skip to content

grpcerrors: preserve unary RPC cancellation causes - #7446

Open
crazy-max wants to merge 1 commit into
moby:masterfrom
crazy-max:fix-session-auth-timeout-cause
Open

crazy-max wants to merge 1 commit into
moby:masterfrom
crazy-max:fix-session-auth-timeout-cause

Conversation

@crazy-max

@crazy-max crazy-max commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

fixes #7444

Preserve the local context cause when a unary RPC returns a matching bare cancellation or deadline error. This makes session-auth timeouts identify the operation that timed out and also preserves causes attached to session-derived contexts. When a deadline response arrives after the deadline but before the local context timer fires, wait for cancellation before reading the cause.

Keep the RPC status code consistent across grpcerrors.Code, status.Code, and serialization, even when the cause carries a different status or is classified as an internal error. Distinct remote errors and configured timeouts remain unchanged.

@crazy-max
crazy-max force-pushed the fix-session-auth-timeout-cause branch 2 times, most recently from dd2dffb to 2e725db Compare November 19, 2026 10:08
@crazy-max crazy-max changed the title session: preserve auth timeout causes grpcerrors: preserve unary RPC cancellation causes Nov 19, 2026
@crazy-max
crazy-max marked this pull request as ready for review November 19, 2026 10:32
@crazy-max crazy-max added this to the v0.36.0 milestone Nov 20, 2026

@tonistiigi tonistiigi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agentic:


I found four issues while testing this change. Each one is reproduced below.

1. Deadline causes are still lost intermittently (util/grpcerrors/intercept.go#L44)

go test -count=30 ./session/auth fails 7 of 30 runs on this branch. Every failure is the bare DeadlineExceeded: context deadline exceeded.

I added logging inside the interceptor. Each failing call received a bare DeadlineExceeded status with no details, 0.3–0.6 ms after the context deadline had passed. At that point ctx.Err() and context.Cause(ctx) were both still nil, so the restore branch was skipped. The server gets the same deadline through grpc-timeout, so its bare status can arrive before the client context's timer has run.

This is the code path from #7444, so the fix does not hold reliably. Cancellation is not affected, because cancel() sets Err() immediately.

2. The RPC code is not kept for some causes (util/grpcerrors/intercept.go#L48)

Code() checks errdefs.IsInternal before it looks at the Code() method of withCodeError (util/grpcerrors/grpcerrors.go#L118). As a result, WrapCode cannot override that classification. With the invoker returning a bare codes.Canceled:

cancel cause grpcerrors.Code and ToGRPC code
errors.New("custom") Canceled
errdefs.Internal(...) Internal
errors.WithStack(syscall.ENOSPC) ResourceExhausted

This contradicts "Preserve the RPC status code" in the commit message. For example, if a sibling in an errgroup.WithContext group fails with ENOSPC, an RPC that was only canceled is reported as ResourceExhausted.

3. The restored error no longer carries a gRPC status (util/grpcerrors/intercept.go#L48)

Before this change, the interceptor returned *grpcStatusError, which implements GRPCStatus(). The new error does not, so status.Code(err) and status.FromError(err) now return Unknown for these errors. This includes contexts with no custom cause (plain context.WithCancel).

If the cause is itself a status error, the two code helpers disagree. With a cause of status.Error(codes.Unavailable, ...), status.Code returns Unavailable, while grpcerrors.Code and ToGRPC return Canceled.

The client package is public API, so callers that check Solve errors with status.Code will see this change.

4. The parent-cancel half of TestSessionAuthTimeout never reaches the RPC (session/auth/auth_test.go#L79-L84)

The test cancels the parent context before making the call. Manager.Get checks ctx.Done() first (session/manager.go#L171-L174). So the call returns no active session for <id>: build canceled: context canceled without ever making an RPC. These assertions pass with or without the interceptor change.


Fixes, for reference:

  • 1: when the status is DeadlineExceeded and ctx.Deadline() has already passed, wait on ctx.Done() before reading the cause. A context whose Deadline() is in the past and whose Done() closes later tests this deterministically.
  • 2 and 3: one dedicated error type fixes both. It returns the cause from Error()/Unwrap() and the original status from GRPCStatus()/Code(), and Code() checks it before IsInternal.
  • 4: cancel while the handler is blocked, as TestUnaryClientCancellationCause does with started.

Restore the local context cause when a unary RPC returns a matching bare
cancellation or deadline status. Handle this in the shared client
interceptor so session-derived causes and auth timeout messages survive.

Wait for local cancellation when a bare deadline response arrives after
the deadline but before the client context timer fires. Keep the RPC code
and GRPCStatus independent of the cause's own error classification, while
leaving distinct remote errors intact.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max
crazy-max force-pushed the fix-session-auth-timeout-cause branch from 03aa327 to e898eb9 Compare November 23, 2026 14:54
@crazy-max
crazy-max requested a review from tonistiigi November 23, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A credential helper slower than 60 s fails the build at resolve image config with a bare DeadlineExceeded: context deadline exceeded

2 participants