Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce issue with client error handling + fix #799

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jamesrom
Copy link

@jamesrom jamesrom commented Nov 28, 2024

One line fix is pretty self-explanatory. Update: it's still one line, but not-so self-explanatory 😅

If a client interceptor hides an error, this type assertion will fail. This is because type information is lost when unaryFunc returns nil.

I have provided a test case that reproduces the issue.

@jamesrom jamesrom force-pushed the fix/client-error-handling branch from 7bfd35e to facbe19 Compare November 28, 2024 02:23
client.go Outdated Show resolved Hide resolved
@jamesrom jamesrom force-pushed the fix/client-error-handling branch from 6729bd4 to 9bfd649 Compare November 28, 2024 04:07
@@ -110,7 +110,7 @@ func NewClient[Req, Res any](httpClient HTTPClient, url string, options ...Clien
request.peer = client.protocolClient.Peer()
protocolClient.WriteRequestHeader(StreamTypeUnary, request.Header())
response, err := unaryFunc(ctx, request)
if err != nil {
if err != nil || response == nil {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right. A nil, nil response isn't really valid. What is the framework expected to do with this when returning a value to the caller?

Copy link
Author

Choose a reason for hiding this comment

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

return func(ctx context.Context, req AnyRequest) (_ AnyResponse, retErr error) {
res, err := next(ctx, req)
if CodeOf(err) == CodeCanceled { // some criteria for ignored errors
return res, nil
Copy link
Member

Choose a reason for hiding this comment

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

This causes the interceptor to return nil, nil. I don't think that should be legal. If an interceptor is "hiding" an error, it must synthesize an actual, non-nil response value.

This could be done dynamically by inspecting req.Spec.Schema(). If that is unset then a dynamic response is not possible.

We could also potentially have connect-go's internal implementation of AnyRequest implement an optional method that could help. For example, maybe something like so:

if respTyped, ok := req.(interface {
    // Could instantiate the correct response type and then run
    // any configured initializer.
    NewResponse() any
}); ok {
    return respTyped.NewResponse(), nil
}

But, now that I've suggested it, I don't love that. It is much less intuitive of course. But it also just feels like a terrible pattern to support -- always returning a zero value response seems wrong and is almost certain to violate any expected RPC contract about what the method is supposed to return. This just doesn't seem like an appropriate function for an interceptor.

It would be much better to inject this error handling much deeper -- like into the actual response handlers, so they can return a valid response value if the application wants to ignore a particular error. Another possibility would be to use a switch on the schema type or the actual procedure name so that it always returns a valid response (which may not necessarily be empty).

Copy link
Author

Choose a reason for hiding this comment

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

The client interceptor returning an actual, non-nil response value is reasonable. However, it also doesn't seem right that interceptors lose all information about the expected response in an error condition.

I don't think smuggling the type via AnyRequest would work either -- there's no reason (other than convention, I suppose) that the same request type isn't used by multiple service methods returning different response types.

Short of smuggling the type information through the Error struct, another option is that resp.Any() could do a nil check:

// Any returns the concrete response message as an empty interface, so that
// *Response implements the [AnyResponse] interface. If the response message is
// nil, Any returns a new zero value of the message type.
func (r *Response[T]) Any() any {
	if r.Msg == nil {
		return new(T)
	}
	return r.Msg
}

But this means the framework would need to ensure that resp is never nil before being passed to the next interceptor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants