{"record":{"id":"16d52958afc35463","repo":"grpc/grpc-go","slug":"external-processor-sent-response-trailers-before-r","errorCode":null,"errorMessage":"external processor sent response trailers before response trailers were sent to it","messagePattern":"external processor sent response trailers before response trailers were sent to it","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/xds/httpfilter/extproc/ext_proc.go","lineNumber":1503,"sourceCode":"\t\t\t\tcs.failProcStream(fmt.Errorf(\"external processor returned unexpected status %v for response headers, expected %v\", status, v3procservicepb.CommonResponse_CONTINUE))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif err = cs.applyMutations(header.GetResponse().GetHeaderMutation(), cs.responseHeader); err != nil {\n\t\t\t\tcs.failProcStream(err)\n\t\t\t\treturn\n\t\t\t}\n\t\t\t// Signal that the response header is modified and ready to be sent to the\n\t\t\t// client, so that if there is any buffered response body, it can be sent\n\t\t\t// after the header.\n\t\t\tcs.fireResponseHeadersReady()\n\n\t\tcase resp.GetResponseTrailers() != nil:\n\t\t\tif cs.config.processingModes.responseTrailerMode == modeSkip {\n\t\t\t\tcs.failProcStream(fmt.Errorf(\"external processor unexpectedly sent response trailers when response trailer processing is disabled\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif !cs.trailerSent.Load() {\n\t\t\t\tcs.failProcStream(fmt.Errorf(\"external processor sent response trailers before response trailers were sent to it\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif cs.responseTrailerReady.HasFired() {\n\t\t\t\tcs.failProcStream(fmt.Errorf(\"external processor unexpectedly sent duplicate response trailers after response trailers were already processed\"))\n\t\t\t\treturn\n\t\t\t}\n\t\t\ttrailer := resp.GetResponseTrailers()\n\t\t\tif err = cs.applyMutations(trailer.GetHeaderMutation(), cs.responseTrailers); err != nil {\n\t\t\t\tcs.failProcStream(err)\n\t\t\t\treturn\n\t\t\t}\n\t\t\t// Signal that the response trailer is modified and ready to be sent to\n\t\t\t// the client.\n\t\t\tcs.fireResponseTrailerReady()\n\t\t}\n\t}\n}\n","sourceCodeStart":1485,"sourceCodeEnd":1521,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/internal/xds/httpfilter/extproc/ext_proc.go#L1485-L1521","documentation":"Raised by recvFromProcServerLoop (ext_proc.go:1503) when the ext_proc server sends a response_trailers response before the client has forwarded the response trailers (trailerSent is false). The server cannot mutate trailers it has not received; failProcStream fails the RPC unless failure_mode_allow bypasses it.","triggerScenarios":"Triggered when the server emits a response_trailers mutation (ext_proc.go:1497) on a stream where the client has not yet sent the response_trailers event — e.g. the server proactively pushes trailer mutations at stream open.","commonSituations":"Server that pushes a fixed set of trailers immediately instead of responding to the response_trailers request, or a handler that conflates headers and trailers phases.","solutions":["On the server, only send response_trailers mutations in reply to a response_trailers ProcessingRequest.","Set failure_mode_allow so the premature mutation does not fail the RPC.","Add server-side request-type logging to confirm response_trailers is only emitted after one is received.","If injecting static trailers, do it inside the response_trailers handler, not eagerly."],"exampleFix":"// before: server pushes trailer mutation immediately\nfunc handle(stream) {\n  stream.Send(&procpb.ProcessingResponse{Response: &procpb.ProcessingResponse_ResponseTrailers{...}})\n}\n\n// after: respond only to the matching request type\nfor {\n  req, _ := stream.Recv()\n  if _, ok := req.Request.(*procpb.ProcessingRequest_ResponseTrailers); ok {\n    stream.Send(&procpb.ProcessingResponse{Response: &procpb.ProcessingResponse_ResponseTrailers{...}})\n  }\n}","handlingStrategy":"fallback","validationCode":"// On the ext_proc SERVER: only emit response_trailers in reply to a\n// response_trailers request.\nfunc shouldAnswerResponseTrailers(req *procpb.ProcessingRequest) bool {\n    _, ok := req.GetRequest().(*procpb.ProcessingRequest_ResponseTrailers)\n    return ok\n}","typeGuard":null,"tryCatchPattern":"filter.failure_mode_allow = true\nif st, ok := status.FromError(err); ok && st.Code() == codes.Internal &&\n    strings.Contains(st.Message(), \"response trailers before response trailers were sent to it\") {\n    // server pushed response_trailers before the client forwarded them\n}","preventionTips":["Server: send response_trailers only in reply to a response_trailers ProcessingRequest.","Add static trailer mutations inside the response_trailers handler, not eagerly.","Enable failure_mode_allow so a premature mutation does not fail the RPC.","Log received ProcessingRequest types server-side to confirm ordering."],"tags":["grpc","xds","extproc","envoy","protocol-violation","ordering","response-trailers"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}