go data_error ai_generated true

proto: cannot parse invalid wire-format data

ID: go/proto-marshal-invalid-wire-type

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-05-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf v1.31+ active — — —

Root Cause

The bytes being unmarshaled are not valid protobuf wire format — typically caused by sending JSON, gzip-compressed payloads, or a message of a different schema over the wire.

generic

中文

被反序列化的字节不是有效的 protobuf wire format —— 通常是因为发送了 JSON、gzip 压缩载荷或使用了不同 schema 的消息。

Workarounds

  1. 90% success
    if err := proto.Unmarshal(data, msg); err != nil {
        log.Printf("unmarshal failed: %v, first bytes: % x", err, data[:min(32, len(data))])
        return err
    }
  2. 86% success
    // client
    conn, _ := grpc.NewClient(addr,
        grpc.WithDefaultCallOptions(grpc.ForceCodec(proto.Marshaler{})),
    )
    // server: verify incoming Content-Type header
    md, _ := metadata.FromIncomingContext(ctx)
    log.Printf("content-type: %v", md["content-type"])

Dead Ends

Common approaches that don't work:

  1. 99% fail

    Unmarshal is deterministic; invalid wire bytes always fail at the same offset.

  2. 85% fail

    Doesn't fix the underlying wire corruption; just moves the failure deeper.