Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/filters/proxies/grpcproxy/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ func (sp *ServerPool) biTransport(ctx *serverPoolContext, proxyAsClientStream gr
ctx.resp.SetTrailer(grpcprot.NewTrailer(proxyAsClientStream.Trailer()))
// c2sErr will contain RPC error from client code. If not io.EOF return the RPC error as server stream error.
if s2cErr != io.EOF {
// A well-formed gRPC status error means the backend communicated an
// application-level result (e.g. NOT_FOUND, INVALID_ARGUMENT) over a
// healthy connection. Marking the pool entry as resultServerError in
// that case degrades a perfectly usable backend connection and causes
// subsequent calls to stall waiting on pool.borrow(). Only return
// resultServerError for transport-level failures (non-gRPC errors)
// where the connection itself is broken.
if st, isGRPCStatus := status.FromError(s2cErr); isGRPCStatus {
// Preserve the backend's gRPC status so the caller receives the
// correct code (e.g. NOT_FOUND, INVALID_ARGUMENT). The connection
// itself is healthy so we still return nil to avoid pool degradation.
ctx.resp.SetStatus(st)
return nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This keeps the pool healthy, but it also loses the status from the backend. sp.handle treats a nil error as success, and the gRPC server later returns resp.Err(), so a backend NOT_FOUND or INVALID_ARGUMENT can become an OK response. Please set ctx.resp from s2cErr before taking this path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — fixed in 0e69ba3.

Capturing the *status.Status return value from status.FromError and calling ctx.resp.SetStatus(st) before returning nil:

if st, isGRPCStatus := status.FromError(s2cErr); isGRPCStatus {
    ctx.resp.SetStatus(st)
    return nil
}

This preserves the backend’s NOT_FOUND / INVALID_ARGUMENT / etc. on the response so handle() propagates it correctly to the caller.

}
return serverPoolError{status.Convert(s2cErr), resultServerError}
}
return nil
Expand Down
117 changes: 117 additions & 0 deletions pkg/filters/proxies/grpcproxy/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package grpcproxy

import (
"context"
"io"
"math/rand"
"sync"
"testing"
Expand All @@ -28,7 +29,9 @@ import (
"github.com/megaease/easegress/v2/pkg/filters/proxies"
"github.com/megaease/easegress/v2/pkg/util/objectpool"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

Expand Down Expand Up @@ -241,3 +244,117 @@ name: grpcforwardproxy
request.Header().Set("targetAddress", "192.168.1.1")
at.Equal("", proxy.mainPool.getTarget(proxy.mainPool.LoadBalancer().ChooseServer(request).URL))
}

// biTransportClientStream is a minimal grpc.ClientStream that returns a
// predetermined error from RecvMsg and blocks the caller until the provided
// done channel is closed.
type biTransportClientStream struct {
grpc.ClientStream
recvErr error
}

func (m *biTransportClientStream) Header() (metadata.MD, error) { return metadata.New(nil), nil }
func (m *biTransportClientStream) Trailer() metadata.MD { return metadata.New(nil) }
func (m *biTransportClientStream) CloseSend() error { return nil }
func (m *biTransportClientStream) Context() context.Context { return context.Background() }
func (m *biTransportClientStream) SendMsg(msg interface{}) error { return nil }
func (m *biTransportClientStream) RecvMsg(msg interface{}) error { return m.recvErr }

// biTransportServerStream is a minimal grpc.ServerStream whose RecvMsg blocks
// until the done channel is closed, simulating a client that has not finished
// sending. This prevents the c2sErrChan from racing with s2cErrChan in tests.
type biTransportServerStream struct {
grpc.ServerStream
done <-chan struct{}
}

func (m *biTransportServerStream) Context() context.Context { return context.Background() }
func (m *biTransportServerStream) SetHeader(md metadata.MD) error { return nil }
func (m *biTransportServerStream) SendHeader(md metadata.MD) error { return nil }
func (m *biTransportServerStream) SetTrailer(md metadata.MD) {}
func (m *biTransportServerStream) SendMsg(msg interface{}) error { return nil }
func (m *biTransportServerStream) RecvMsg(msg interface{}) error {
<-m.done
return io.EOF
}

// TestBiTransportDoesNotDegradePoolOnGRPCAppError verifies that biTransport
// returns nil (no pool degradation) when the backend responds with a valid
// gRPC application-level status (e.g. NOT_FOUND, codes.Code 5). Previously,
// any non-EOF error from the backend would call svr.close(resultServerError),
// degrading the pool and causing subsequent calls to stall on pool.borrow().
func TestBiTransportDoesNotDegradePoolOnGRPCAppError(t *testing.T) {
sp := &ServerPool{}

cases := []struct {
name string
err error
wantNil bool
wantCode codes.Code // non-OK: assert spCtx.resp carries this status; OK means skip (e.g. io.EOF)
}{
{
name: "NOT_FOUND is an application error — pool must not degrade",
err: status.Error(codes.NotFound, "resource not found"),
wantNil: true,
wantCode: codes.NotFound,
},
{
name: "INVALID_ARGUMENT is an application error — pool must not degrade",
err: status.Error(codes.InvalidArgument, "bad request"),
wantNil: true,
wantCode: codes.InvalidArgument,
},
{
name: "INTERNAL is a server-side gRPC error — pool must not degrade",
err: status.Error(codes.Internal, "internal error"),
wantNil: true,
wantCode: codes.Internal,
},
{
name: "io.EOF is the happy-path completion — pool must not degrade",
err: io.EOF,
wantNil: true,
// wantCode left as codes.OK — no status assertion for the normal success path
},
{
name: "plain transport error — pool should degrade",
err: io.ErrUnexpectedEOF,
wantNil: false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
done := make(chan struct{})
defer close(done)

clientStream := &biTransportClientStream{recvErr: tc.err}
serverStream := &biTransportServerStream{done: done}

spCtx := &serverPoolContext{
stdr: serverStream,
stdw: serverStream,
resp: grpcprot.NewResponse(),
}

result := sp.biTransport(spCtx, clientStream)

if tc.wantNil {
assert.Nil(t, result,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This also needs to check the response status. As written, the test passes even if NOT_FOUND is turned into an OK response; asserting spCtx.resp.GetStatus().Code() for the gRPC status cases would catch that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 0e69ba3. Added a wantCode codes.Code field to each test case and assert spCtx.resp.GetStatus().Code() for every gRPC status variant:

if tc.wantCode != codes.OK {
    assert.Equal(t, tc.wantCode, spCtx.resp.GetStatus().Code(),
        "backend gRPC status must be propagated to the response for %q", tc.name)
}

All three gRPC status cases (NOT_FOUND, INVALID_ARGUMENT, INTERNAL) now carry a wantCode assertion. The io.EOF happy-path leaves wantCode as codes.OK and skips the check. The test would have caught the silent-OK regression before this fix.

"biTransport should return nil for %q so the backend pool is not degraded", tc.name)
// For gRPC status errors the backend response code must be preserved
// so the caller gets NOT_FOUND/INVALID_ARGUMENT/etc., not a silent OK.
if tc.wantCode != codes.OK {
assert.Equal(t, tc.wantCode, spCtx.resp.GetStatus().Code(),
"backend gRPC status must be propagated to the response for %q", tc.name)
}
} else {
assert.NotNil(t, result,
"biTransport should return an error for transport failures like %q", tc.name)
spe, ok := result.(serverPoolError)
assert.True(t, ok)
assert.Equal(t, resultServerError, spe.result)
}
})
}
}
Loading