grpc-ecosystem/grpc-gateway · error

failed to read code generator request: %w

Error message

failed to read code generator request: %w

What it means

ParseRequest wraps any io.ReadAll failure when reading a protoc plugin CodeGeneratorRequest from stdin (or another reader). Protoc plugins receive their request as serialized protobuf on stdin; if the stream cannot be read, the plugin cannot proceed.

Source

Thrown at internal/codegenerator/parse_req.go:15

package codegenerator

import (
	"fmt"
	"io"

	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/pluginpb"
)

// ParseRequest parses a code generator request from a proto Message.
func ParseRequest(r io.Reader) (*pluginpb.CodeGeneratorRequest, error) {
	input, err := io.ReadAll(r)
	if err != nil {
		return nil, fmt.Errorf("failed to read code generator request: %w", err)
	}
	req := new(pluginpb.CodeGeneratorRequest)
	if err := proto.Unmarshal(input, req); err != nil {
		return nil, fmt.Errorf("failed to unmarshal code generator request: %w", err)
	}
	return req, nil
}

View on GitHub (pinned to a58a4436a3)

Solutions

  1. Run the binary only via protoc (protoc --plugin=... --grpc-gateway_out=... ) so a valid serialized request is piped to stdin
  2. When testing manually, pipe a captured request file: protoc-gen-x < request.bin
  3. Check disk/pipe environment (ulimit, container stdin settings) if protoc itself reports the failure

Example fix

// before
./protoc-gen-myplugin   # no stdin -> read error
// after
protoc --plugin=protoc-gen-myplugin=./protoc-gen-myplugin --myplugin_out=. api.proto
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure stdin is provided when invoking manually:
//   stat, _ := os.Stdin.Stat()
//   if (stat.Mode() & os.ModeCharDevice) != 0 {
//       fmt.Fprintln(os.Stderr, "expect piped CodeGeneratorRequest on stdin")
//       os.Exit(1)
//   }

Try / catch

req, err := codegenerator.ParseRequest(os.Stdin)
if err != nil {
    // message begins with "failed to read code generator request:"
    grpclog.Fatalf("plugin input error: %v", err)
}

Prevention

When it happens

Trigger: Running a plugin binary (protoc-gen-*) whose main calls codegenerator.ParseRequest(os.Stdin) when stdin is a closed pipe, a broken pipe occurs, or the reader errors mid-read.

Common situations: Invoking the plugin binary directly in a terminal with no stdin redirection; protoc killed mid-invocation leaving a broken pipe; CI sandboxes that deny stdin access; shell pipelines where the producer failed.

Related errors


AI-assisted analysis of grpc-ecosystem/grpc-gateway@a58a4436a3 (2026-09-02). Data as JSON: /api/errors/0e5141adb6b66b24. Report an issue: GitHub.