Skip to content

grpcerrors: preserve context wrapping decoded status errors - #7445

Open
crazy-max wants to merge 1 commit into
moby:masterfrom
crazy-max:fix-grpcerrors-preserve-wrapped-context
Open

crazy-max wants to merge 1 commit into
moby:masterfrom
crazy-max:fix-grpcerrors-preserve-wrapped-context

Conversation

@crazy-max

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

Copy link
Copy Markdown
Member

Preserve context added by callers after decoding a gRPC status when the error is serialized again. Mark the complete decoded error so ToGRPC can distinguish new caller context from text reconstructed from the status and typed details. This prevents typed error messages from growing on each round trip while preserving added context, status codes, and typed details.

Found while investigating #7444. Regression coverage checks repeated round trips for plain and typed errors, including caller wrapping. Propagating cancellation and timeout causes is handled separately in #7446.

@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:

The new check treats any difference from grpcStatusError.Error() as context added after decoding. FromGRPC also rebuilds typed wrappers from the error details, and three of them add their own text in Error():

  • UnsupportedSubrequestError (solver/errdefs/subrequest.go)
  • UnsupportedFrontendCapError (solver/errdefs/frontendcap.go)
  • UnsupportedCompatibilityFeatureError (solver/errdefs/compatibility.go)

For these errors, ToGRPC now copies the rebuilt text into Status.Message, and the next decode adds it again. The message gets longer on every hop:

err := grpcerrors.WrapCode(errdefs.NewUnsupportedFrontendCapError("foo"), codes.Unimplemented)
// repeat: enc := grpcerrors.ToGRPC(ctx, err); err = grpcerrors.FromGRPC(enc)
hop 0: unsupported frontend capability foo
hop 1: unsupported frontend capability foo: Unimplemented: unsupported frontend capability foo
hop 2: unsupported frontend capability foo: Unimplemented: unsupported frontend capability foo: Unimplemented: unsupported frontend capability foo

Before this change the message stays the same on every hop. NewUnsupportedSubrequestError and NewUnsupportedCompatibilityFeatureError behave the same way.

This happens in a normal build. The frontend returns the error (frontend/dockerui/requests.go, frontend/dockerfile/builder/caps.go) and encodes it in frontend/gateway/grpcclient/client.go. The daemon decodes it in frontend/gateway/gateway.go and encodes it again in util/grpcerrors/intercept.go. So the client already receives a doubled message after one frontend, and each nested frontend adds another copy. Existing integration tests use ErrorContains, so they don't catch it.

The check has to tell apart wrappers rebuilt by FromGRPC and context a caller adds afterwards. One way to do that:

// in FromGRPC, before stack.Enable
err = &decodedError{error: err}

type decodedError struct{ error }

func (e *decodedError) Unwrap() error { return e.error }
// in errorHasMoreContext
if grpcStatusError, ok := errors.AsType[*grpcStatusError](err); ok {
	decoded, _ := errors.AsType[*decodedError](err)
	if decoded == nil || errMessage != decoded.Error() {
		return true
	}
	return st.Code() != grpcStatusError.st.Code() || st.Message() != grpcStatusError.st.Message()
}

With this, the tests in this PR pass. The messages for all three typed errors stay the same over 4 round trips, and errors.Wrap(decodedTypedErr, "extra") keeps the added context.

Please also add a round-trip test for typed errors. It should check that the message, code and details stay the same over several ToGRPC/FromGRPC hops, and include one case that adds caller context on top of a decoded typed error. It needs an external test package, because solver/errdefs imports util/grpcerrors.

Two smaller points:

  • The PR description says "avoiding repeated status prefixes". Wrapped errors still show the code twice to the client, for example DeadlineExceeded: extra context: DeadlineExceeded: context deadline exceeded. The wrapped test expects this. Please describe it accurately in the PR description.
  • The comment above the check ("only due to prepending with the status code") no longer matches what the check does.

Mark the complete error reconstructed by FromGRPC so ToGRPC can distinguish
caller-added context from text rebuilt from status and typed details.
Preserve added context without growing typed error messages on every hop.

Cover repeated round trips for plain and typed errors, including caller
wrapping, while checking status codes and typed details remain intact.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max
crazy-max force-pushed the fix-grpcerrors-preserve-wrapped-context branch from 9f14a61 to 5d9647b Compare November 23, 2026 14:47
@crazy-max
crazy-max requested a review from tonistiigi November 23, 2026 14:48
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.

2 participants