Skip to content

fix(grpcproxy): do not degrade pool on gRPC application-level errors (fixes #1517) - #1518

Merged
xxx7xxxx merged 3 commits into
easegress-io:mainfrom
akpradheeph:fix/grpc-pool-biTransport-degrade-on-app-error
May 29, 2026
Merged

xxx7xxxx merged 3 commits into
easegress-io:mainfrom
akpradheeph:fix/grpc-pool-biTransport-degrade-on-app-error

Conversation

@akpradheeph

Copy link
Copy Markdown
Contributor

Problem

Fixes #1517.

biTransportProxy() in pkg/filters/proxies/grpcproxy/pool.go treats every non-io.EOF error from the backend stream as a transport failure and calls svr.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:

  1. Call A → backend returns NOT_FOUND → pool entry marked resultServerError → pool exhausted
  2. Call B → pool.borrow() with no BorrowTimeout → blocks indefinitely
  3. Outer 30-second deadline fires → context.Canceled → caller gets a generic service error

Root Cause

// Before (buggy)
if s2cErr != io.EOF {
    return serverPoolError{status.Convert(s2cErr), resultServerError}  // fires for NOT_FOUND too
}

grpc.ClientStream.RecvMsg() returns a *status.Status-wrapped error for application-level responses. status.FromError() returns ok=true for these — they indicate the backend sent a valid gRPC response, not that the connection is broken.

Fix

// After
if s2cErr != io.EOF {
    // A valid gRPC status error means the backend communicated an
    // application-level result over a healthy connection.
    // Only degrade the pool for genuine transport failures.
    if _, isGRPCStatus := status.FromError(s2cErr); isGRPCStatus {
        return nil
    }
    return serverPoolError{status.Convert(s2cErr), resultServerError}
}

Changes

  • pkg/filters/proxies/grpcproxy/pool.go — one status.FromError() guard in biTransport()
  • pkg/filters/proxies/grpcproxy/pool_test.go — TestBiTransportDoesNotDegradePoolOnGRPCAppError covering:
    • NOT_FOUND → returns nil (no pool degradation)
    • INVALID_ARGUMENT → returns nil
    • INTERNAL → returns nil
    • io.EOF → returns nil (existing happy path)
    • plain transport error (io.ErrUnexpectedEOF) → returns serverPoolError{resultServerError} (unchanged)

Testing

=== RUN   TestBiTransportDoesNotDegradePoolOnGRPCAppError
    --- PASS: .../NOT_FOUND_is_an_application_error (0.00s)
    --- PASS: .../INVALID_ARGUMENT_is_an_application_error (0.00s)
    --- PASS: .../INTERNAL_is_a_server-side_gRPC_error (0.00s)
    --- PASS: .../io.EOF_is_the_happy-path_completion (0.00s)
    --- PASS: .../plain_transport_error (0.00s)
PASS — full grpcproxy suite: 0 regressions

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

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.

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.

…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.
Comment on lines +256 to +261
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 }

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.

gofmt still catches this block. This should be the only change here.

Suggested change
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 }

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 869121b — ran gofmt -w on the file, which aligns both blocks as suggested.

Comment on lines +271 to +275
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 }

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.

Same gofmt alignment here.

Suggested change
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 }

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 869121b — same gofmt -w pass covers this block too.

@xxx7xxxx
xxx7xxxx enabled auto-merge May 29, 2026 14:42
@xxx7xxxx
xxx7xxxx added this pull request to the merge queue May 29, 2026
Merged via the queue into easegress-io:main with commit b6be769 May 29, 2026
8 checks passed
@akpradheeph
akpradheeph deleted the fix/grpc-pool-biTransport-degrade-on-app-error branch May 30, 2026 07:27
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.

GRPCProxy: biTransportProxy degrades backend pool on valid gRPC application errors (e.g. NOT_FOUND), causing subsequent calls to stall

2 participants