{"record":{"id":"2cf634e4c8f5bce6","repo":"grpc/grpc-go","slug":"no-peer-found-in-context","errorCode":null,"errorMessage":"no Peer found in Context","messagePattern":"no Peer found in Context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"credentials/alts/utils.go","lineNumber":38,"sourceCode":"\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"google.golang.org/grpc/codes\"\n\t\"google.golang.org/grpc/peer\"\n\t\"google.golang.org/grpc/status\"\n)\n\n// AuthInfoFromContext extracts the alts.AuthInfo object from the given context,\n// if it exists. This API should be used by gRPC server RPC handlers to get\n// information about the communicating peer. For client-side, use grpc.Peer()\n// CallOption.\nfunc AuthInfoFromContext(ctx context.Context) (AuthInfo, error) {\n\tp, ok := peer.FromContext(ctx)\n\tif !ok {\n\t\treturn nil, errors.New(\"no Peer found in Context\")\n\t}\n\treturn AuthInfoFromPeer(p)\n}\n\n// AuthInfoFromPeer extracts the alts.AuthInfo object from the given peer, if it\n// exists. This API should be used by gRPC clients after obtaining a peer object\n// using the grpc.Peer() CallOption.\nfunc AuthInfoFromPeer(p *peer.Peer) (AuthInfo, error) {\n\taltsAuthInfo, ok := p.AuthInfo.(AuthInfo)\n\tif !ok {\n\t\treturn nil, errors.New(\"no alts.AuthInfo found in Peer\")\n\t}\n\treturn altsAuthInfo, nil\n}\n\n// ClientAuthorizationCheck checks whether the client is authorized to access\n// the requested resources based on the given expected client service accounts.\n// This API should be used by gRPC server RPC handlers. This API should not be","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/credentials/alts/utils.go#L20-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nfunc myHandler(ctx context.Context, req *pb.Req) (*pb.Resp, error) {\n    ai, err := alts.AuthInfoFromContext(context.Background()) // wrong ctx\n}\n// after\nfunc myHandler(ctx context.Context, req *pb.Req) (*pb.Resp, error) {\n    ai, err := alts.AuthInfoFromContext(ctx) // ctx from gRPC\n}","handlingStrategy":"validation","validationCode":"// Before calling AuthInfoFromContext, verify a peer exists:\np, ok := peer.FromContext(ctx)\nif !ok {\n    return status.Error(codes.Unauthenticated, \"no gRPC peer in context; ensure this runs inside an RPC handler\")\n}","typeGuard":"func hasGRPCPeer(ctx context.Context) bool {\n    _, ok := peer.FromContext(ctx)\n    return ok\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["go","grpc","alts","credentials","context"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}