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

fix: Do not wrap io.EOF intercepted by stream Sends #37647

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions api/utils/grpc/interceptors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ type grpcClientStreamWrapper struct {

// SendMsg wraps around ClientStream.SendMsg
func (s *grpcClientStreamWrapper) SendMsg(m interface{}) error {
if err := s.ClientStream.SendMsg(m); err != nil {
return &RemoteError{Err: trace.Unwrap(trail.FromGRPC(s.ClientStream.SendMsg(m)))}
}
return nil
return wrapStreamErr(s.ClientStream.SendMsg(m))
}

// RecvMsg wraps around ClientStream.RecvMsg
func (s *grpcClientStreamWrapper) RecvMsg(m interface{}) error {
switch err := s.ClientStream.RecvMsg(m); {
return wrapStreamErr(s.ClientStream.RecvMsg(m))
}

func wrapStreamErr(err error) error {
switch {
case err == nil:
return nil
case errors.Is(err, io.EOF):
codingllama marked this conversation as resolved.
Show resolved Hide resolved
// Do not wrap io.EOF errors, they are often used as stop guards for streams.
return err
case err != nil:
return &RemoteError{Err: trace.Unwrap(trail.FromGRPC(s.ClientStream.RecvMsg(m)))}
default:
return &RemoteError{Err: trace.Unwrap(trail.FromGRPC(err))}
}
return nil
}

// GRPCServerUnaryErrorInterceptor is a gRPC unary server interceptor that
Expand Down
16 changes: 10 additions & 6 deletions api/utils/grpc/interceptors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package interceptors_test

import (
"context"
"errors"
"io"
"net"
"testing"
"time"

"github.com/gravitational/trace"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -88,15 +88,19 @@ func TestGRPCErrorWrapping(t *testing.T) {
stream, err := client.AddMFADevice(context.Background())
require.NoError(t, err)

// Give the server time to close the stream. This allows us to more
// consistently hit the io.EOF error.
time.Sleep(100 * time.Millisecond)

//nolint:staticcheck // SA1019. The specific stream used here doesn't matter.
sendErr := stream.Send(&proto.AddMFADeviceRequest{})

// io.EOF means the server closed the stream, which can
// happen depending in timing. In either case, it is
// still safe to recv from the stream and check for
// Expect either a success (unlikely because of the Sleep) or an unwrapped
// io.EOF error (meaning the server errored and closed the stream).
// In either case, it is still safe to recv from the stream and check for
// the already exists error.
if sendErr != nil && !errors.Is(sendErr, io.EOF) {
t.Fatalf("Unexpected error: %v", sendErr)
if sendErr != nil && sendErr != io.EOF /* == error comparison on purpose! */ {
t.Fatalf("Unexpected error: %q (%T)", sendErr, sendErr)
}

_, err = stream.Recv()
Expand Down