grpc/grpc-go · error
no Peer found in Context
Error message
no Peer found in Context
What it means
Returned by AuthInfoFromContext (credentials/alts/utils.go:35-41) when peer.FromContext(ctx) returns ok==false — the context has no Peer value attached. gRPC normally attaches the peer to a server handler's context after the handshake; calling AuthInfoFromContext on a context that never carried a peer (e.g. a plain context.Background, or a handler invoked before transport setup) triggers it.
Source
Thrown at credentials/alts/utils.go:38
import (
"context"
"errors"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)
// AuthInfoFromContext extracts the alts.AuthInfo object from the given context,
// if it exists. This API should be used by gRPC server RPC handlers to get
// information about the communicating peer. For client-side, use grpc.Peer()
// CallOption.
func AuthInfoFromContext(ctx context.Context) (AuthInfo, error) {
p, ok := peer.FromContext(ctx)
if !ok {
return nil, errors.New("no Peer found in Context")
}
return AuthInfoFromPeer(p)
}
// AuthInfoFromPeer extracts the alts.AuthInfo object from the given peer, if it
// exists. This API should be used by gRPC clients after obtaining a peer object
// using the grpc.Peer() CallOption.
func AuthInfoFromPeer(p *peer.Peer) (AuthInfo, error) {
altsAuthInfo, ok := p.AuthInfo.(AuthInfo)
if !ok {
return nil, errors.New("no alts.AuthInfo found in Peer")
}
return altsAuthInfo, nil
}
// ClientAuthorizationCheck checks whether the client is authorized to access
// the requested resources based on the given expected client service accounts.
// This API should be used by gRPC server RPC handlers. This API should not beView on GitHub (pinned to 03255a9237)
Solutions
- Call AuthInfoFromContext only inside a gRPC server method/interceptor where gRPC has attached the Peer.
- Guard the call: check p, ok := peer.FromContext(ctx) first, and return PermissionDenied/Unauthenticated when !ok.
- In tests, inject a peer via peer.NewContext(ctx, &peer.Peer{AuthInfo: fakeALTS}) instead of using a bare context.
- Use ClientAuthorizationCheck which already wraps this into a clean codes.PermissionDenied status.
Example fix
// before — helper called on a non-RPC context
ai, err := alts.AuthInfoFromContext(context.Background())
// err: no Peer found in Context
// after — guard the peer, or inject one in tests
func authorize(ctx context.Context) error {
p, ok := peer.FromContext(ctx)
if !ok {
return status.Error(codes.PermissionDenied, "no peer")
}
return alts.ClientAuthorizationCheck(ctx, allowed)
} Defensive patterns
Strategy: validation
Validate before calling
// Guard Peer before extracting ALTS info
func authorize(ctx context.Context, allowed []string) error {
p, ok := peer.FromContext(ctx)
if !ok {
return status.Error(codes.PermissionDenied, "no peer in context")
}
return alts.ClientAuthorizationCheck(ctx, allowed)
} Type guard
func hasPeer(ctx context.Context) bool {
_, ok := peer.FromContext(ctx)
return ok
} Try / catch
if _, err := alts.AuthInfoFromContext(ctx); err != nil {
if strings.Contains(err.Error(), "no Peer found") {
return status.Error(codes.PermissionDenied, "non-RPC context")
}
} Prevention
- Call AuthInfoFromContext only inside real gRPC handlers/interceptors.
- Inject a peer via peer.NewContext in tests.
- Prefer alts.ClientAuthorizationCheck for clean status mapping.
When it happens
Trigger: AuthInfoFromContext is called on a context that is not (or not yet) a gRPC server handler context — peer.FromContext returns false at utils.go:36-38. Also if the peer was never set because the call path bypassed gRPC transport (e.g. in a unit test, a non-RPC goroutine, or an interceptor that received a fresh context).
Common situations: Unit-testing a server handler with context.Background(); calling AuthInfoFromContext inside a streaming handler before the RPC peer is established; sharing auth-check helper code between gRPC and non-gRPC paths; a context overwritten via context.WithValue losing the peer.
Related errors
- no alts.AuthInfo found in Peer
- ALTS: untrusted platform. ALTS is only supported on GCP
- client-side auth info is not of type alts.AuthInfo
- server-side auth info is not of type alts.AuthInfo
- no SubConn is available
AI-assisted analysis of grpc/grpc-go@03255a9237 (2026-08-07).
Data as JSON: /api/errors/2cf634e4c8f5bce6.
Report an issue: GitHub.