diff --git a/envelope.go b/envelope.go index 098dd3a6..936495d1 100644 --- a/envelope.go +++ b/envelope.go @@ -203,15 +203,12 @@ func (r *envelopeReader) Unmarshal(message any) *Error { // stream. Save the message for protocol-specific code to process and // return a sentinel error. Since we've deferred functions to return env's // underlying buffer to a pool, we need to keep a copy. + copiedData := make([]byte, data.Len()) + copy(copiedData, data.Bytes()) r.last = envelope{ - Data: r.bufferPool.Get(), + Data: bytes.NewBuffer(copiedData), Flags: env.Flags, } - // Don't return last to the pool! We're going to reference the data - // elsewhere. - if _, err := r.last.Data.ReadFrom(data); err != nil { - return errorf(CodeUnknown, "copy final envelope: %w", err) - } return errSpecialEnvelope }