grpc/grpc-go · error
no Peer found in Context
Error message
no Peer found in Context
What it means
Returned by alts.AuthInfoFromContext when the gRPC peer.Peer value is not present in the context. The function calls peer.FromContext(ctx) and expects the gRPC framework to have injected a peer during an active RPC; if the context was not prepared by gRPC (or is a plain background context), the lookup fails. This almost always indicates the helper was called outside of a real server-side RPC handler or on a client context before the stream was established.
Solutions
- Ensure AuthInfoFromContext is only called inside a gRPC server RPC handler using the ctx argument received from gRPC.
- If you need the peer on the client side, use the grpc.Peer() CallOption and call alts.AuthInfoFromPeer(p) on the returned peer instead.
- In unit tests, inject a peer into the context using peer.NewContext or test through a real bufconn gRPC server.
Example fix
// before
func myHandler(ctx context.Context, req *pb.Req) (*pb.Resp, error) {
ai, err := alts.AuthInfoFromContext(context.Background()) // wrong ctx
}
// after
func myHandler(ctx context.Context, req *pb.Req) (*pb.Resp, error) {
ai, err := alts.AuthInfoFromContext(ctx) // ctx from gRPC
} Defensive patterns
Strategy: validation
Validate before calling
// Before calling AuthInfoFromContext, verify a peer exists:
p, ok := peer.FromContext(ctx)
if !ok {
return status.Error(codes.Unauthenticated, "no gRPC peer in context; ensure this runs inside an RPC handler")
} Type guard
func hasGRPCPeer(ctx context.Context) bool {
_, ok := peer.FromContext(ctx)
return ok
} Prevention
- Only call AuthInfoFromContext inside server RPC handlers using the ctx passed by gRPC.
- Use peer.NewContext in unit tests to inject a peer.
- For client-side peer access, use grpc.Peer() CallOption + AuthInfoFromPeer.
When it happens
Trigger: Calling alts.AuthInfoFromContext(ctx) with a context.Context that was never passed through gRPC's transport layer (e.g., context.Background(), a manually-created context, or inside a non-RPC code path). Also triggered in unit tests that invoke handler logic without using a real gRPC server or without stubbing peer.FromContext.
Common situations: Developers extract RPC handler logic into a standalone function and unit-test it with context.Background(). Also occurs when server interceptorors call AuthInfoFromContext on a context that was not derived from the gRPC stream context, or when the helper is mistakenly called on the client side (where peer is obtained via grpc.Peer() CallOption instead).
Related errors
- no alts.AuthInfo found in Peer
- request info not found from context
- AuthInfo is nil
- ClientHandshake() is not supported for server credentials
- credentials: rawConn is dispatched out of gRPC
AI-assisted analysis of grpc/grpc-go@0c51461d27 (2026-08-11).
Data as JSON: /api/errors/2cf634e4c8f5bce6.
Report an issue: GitHub.
Appendix: 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 0c51461d27)