Repository navigation
fix(grpcproxy): do not degrade pool on gRPC application-level errors (fixes #1517) - #1518
Conversation
When a backend service returns a well-formed gRPC status error (e.g.
NOT_FOUND / codes.Code 5, INVALID_ARGUMENT, etc.) biTransportProxy()
was calling svr.close(resultServerError), which marked the connection
as broken and removed it from the pool. The next call then blocked
indefinitely on pool.borrow() until the outer context deadline fired.
Root cause:
biTransport() compared s2cErr != io.EOF and returned resultServerError
for every non-EOF, including valid gRPC status errors. A gRPC status
error indicates the backend communicated an application result over a
healthy connection; it is not a transport failure.
Fix:
Use status.FromError() to distinguish the two cases:
- ok == true → valid gRPC status, connection healthy → return nil
- ok == false → transport failure (broken pipe, TLS reset, etc.)
→ return resultServerError as before
Example failure sequence this fixes:
1. Call A → backend returns NOT_FOUND → biTransport marks pool entry
resultServerError → pool.borrow() has nothing to lend
2. Call B → pool.borrow() with no BorrowTimeout → blocks indefinitely
3. 30-second outer deadline fires → context.Canceled → caller gets
error code 06 / ISO-8583 unable-to-process
Tests:
TestBiTransportDoesNotDegradePoolOnGRPCAppError covers NOT_FOUND,
INVALID_ARGUMENT, INTERNAL (all must return nil), io.EOF (happy path),
and a plain transport error (must still return resultServerError).
| // resultServerError for transport-level failures (non-gRPC errors) | ||
| // where the connection itself is broken. | ||
| if _, isGRPCStatus := status.FromError(s2cErr); isGRPCStatus { | ||
| return nil |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| result := sp.biTransport(spCtx, clientStream) | ||
|
|
||
| if tc.wantNil { | ||
| assert.Nil(t, result, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…dation When the backend returns a well-formed gRPC status (NOT_FOUND, INVALID_ARGUMENT, etc.) biTransport now calls ctx.resp.SetStatus(st) before returning nil. Previously the status was silently discarded, causing handle() to treat any gRPC application error as success and the caller to receive an empty OK response instead of the actual error. The test cases are extended with wantCode assertions on spCtx.resp.GetStatus().Code() for each gRPC status variant, so a regression (status silently dropped) would now be caught. Addresses reviewer feedback on PR easegress-io#1518.
| 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 } |
There was a problem hiding this comment.
gofmt still catches this block. This should be the only change here.
| 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 } | |
| 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 } |
There was a problem hiding this comment.
Fixed in 869121b — ran gofmt -w on the file, which aligns both blocks as suggested.
| 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 } |
There was a problem hiding this comment.
Same gofmt alignment here.
| 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) 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 } |
There was a problem hiding this comment.
Fixed in 869121b — same gofmt -w pass covers this block too.
Addresses reviewer feedback on PR easegress-io#1518.
Problem
Fixes #1517.
biTransportProxy()inpkg/filters/proxies/grpcproxy/pool.gotreats every non-io.EOFerror from the backend stream as a transport failure and callssvr.close(resultServerError), degrading the backend connection pool.This fires incorrectly for valid gRPC status errors (e.g.
NOT_FOUND,INVALID_ARGUMENT,INTERNAL) that the backend returned over a perfectly healthy connection. The result:NOT_FOUND→ pool entry markedresultServerError→ pool exhaustedpool.borrow()with noBorrowTimeout→ blocks indefinitelycontext.Canceled→ caller gets a generic service errorRoot Cause
grpc.ClientStream.RecvMsg()returns a*status.Status-wrapped error for application-level responses.status.FromError()returnsok=truefor these — they indicate the backend sent a valid gRPC response, not that the connection is broken.Fix
Changes
pkg/filters/proxies/grpcproxy/pool.go— onestatus.FromError()guard inbiTransport()pkg/filters/proxies/grpcproxy/pool_test.go—TestBiTransportDoesNotDegradePoolOnGRPCAppErrorcovering:NOT_FOUND→ returnsnil(no pool degradation)INVALID_ARGUMENT→ returnsnilINTERNAL→ returnsnilio.EOF→ returnsnil(existing happy path)io.ErrUnexpectedEOF) → returnsserverPoolError{resultServerError}(unchanged)Testing