diff --git a/pkg/rpc/context.go b/pkg/rpc/context.go index 80cf8875e751..e8b82db3bea5 100644 --- a/pkg/rpc/context.go +++ b/pkg/rpc/context.go @@ -904,6 +904,8 @@ func (d delayingConn) Write(b []byte) (n int, err error) { return n, err } +var errMagicNotFound = errors.New("didn't get expected magic bytes header") + func (d *delayingConn) Read(b []byte) (n int, err error) { if d.readBuf.Len() == 0 { var hdr delayingHeader @@ -912,17 +914,17 @@ func (d *delayingConn) Read(b []byte) (n int, err error) { } // If we somehow don't get our expected magic, throw an error. if hdr.Magic != magic { - panic(errors.New("didn't get expected magic bytes header")) - } else { - // Once we receive our first packet, we set our delay to the expected - // delay that was sent on the write side. - d.latency = time.Duration(hdr.DelayMS) * time.Millisecond - defer func() { - time.Sleep(timeutil.Until(timeutil.Unix(0, hdr.ReadTime))) - }() - if _, err := io.CopyN(d.readBuf, d.Conn, int64(hdr.Sz)); err != nil { - return 0, err - } + return 0, errors.WithStack(errMagicNotFound) + } + + // Once we receive our first packet, we set our delay to the expected + // delay that was sent on the write side. + d.latency = time.Duration(hdr.DelayMS) * time.Millisecond + defer func() { + time.Sleep(timeutil.Until(timeutil.Unix(0, hdr.ReadTime))) + }() + if _, err := io.CopyN(d.readBuf, d.Conn, int64(hdr.Sz)); err != nil { + return 0, err } } return d.readBuf.Read(b)