title: Recursive data structure unmarshalling gives error "cannot parse invalid wire-format data" in Go Lang Protobuf
body: <p><strong>OS and protobuf version</strong></p>
  <p>go1.18.1 linux/amd64, github.com/golang/protobuf v1.5.2</p>
  <p><strong>Introduction</strong></p>
  <p>I am trying to use recursive proto definitions.</p>
  <p><code>.proto</code> file</p>
  <pre><code>message AsyncConsensus {
    int32 sender = 1;
    int32 receiver = 2;
    string unique_id = 3; // to specify the fall back block id to which the vote asyn is for
    int32 type = 4; // 1-propose, 2-vote, 3-timeout, 4-propose-async, 5-vote-async, 6-timeout-internal, 7-consensus-external-request, 8-consensus-external-response, 9-fallback-complete
    string note = 5;
    int32 v = 6 ; // view number
    int32 r = 7;// round number
    message Block {
      string id = 1;
      int32 v = 2 ; // view number
      int32 r = 3;// round number
      Block parent = 4;
      repeated int32 commands = 5;
      int32 level = 6; // for the fallback mode
    }
    Block blockHigh = 8;
    Block blockNew = 9;
    Block blockCommit = 10;
  }
  </code></pre>
  <p>The following is how I Marshal and Un-Marshal</p>
  <pre><code>func (t *AsyncConsensus) Marshal(wire io.Writer) error {
      data, err := proto.Marshal(t)
      if err != nil {
          return err
      }
      lengthWritten := len(data)
      var b [8]byte
      bs := b[:8]
      binary.LittleEndian.PutUint64(bs, uint64(lengthWritten))
      _, err = wire.Write(bs)
      if err != nil {
          return err
      }
      _, err = wire.Write(data)
      if err != nil {
          return err
      }
      return nil
  }
  
  func (t *AsyncConsensus) Unmarshal(wire io.Reader) error {
  
      var b [8]byte
      bs := b[:8]
      _, err := io.ReadFull(wire, bs)
      if err != nil {
          return err
      }
      numBytes := binary.LittleEndian.Uint64(bs)
      data := make([]byte, numBytes)
      length, err := io.ReadFull(wire, data)
      if err != nil {
          return err
      }
      err = proto.Unmarshal(data[:length], t)
      if err != nil {
          return err
      }
      return nil
  }
  
  func (t *AsyncConsensus) New() Serializable {
      return new(AsyncConsensus)
  }
  </code></pre>
  <p><strong>My expected outcome</strong></p>
  <p>When marshaled and sent to the same process via TCP, it should correctly unmarshal and produce correct data structures.</p>
  <p><strong>Resulting error</strong></p>
  <p>error <code>&quot;cannot parse invalid wire-format data&quot; </code></p>
  <p><strong>Additional information</strong></p>
  <p>I tried with non-recursive <code>.proto</code> definitions, and never had this issue before.</p>