Skip to content

Commit

Permalink
Merge pull request #325 from xjem/fix/race-cond-full-codec
Browse files Browse the repository at this point in the history
fix(codec): full codec fix race condition
  • Loading branch information
ernado committed May 7, 2021
2 parents 49234a0 + 3bf28ef commit 9888c08
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions internal/proto/codec/full.go
Expand Up @@ -36,23 +36,19 @@ func (i *Full) Write(w io.Writer, b *bin.Buffer) error {
return err
}

if err := writeFull(w, int(atomic.LoadInt64(&i.wSeqNo)), b); err != nil {
if err := writeFull(w, int(atomic.AddInt64(&i.wSeqNo, 1)-1), b); err != nil {
return xerrors.Errorf("write full: %w", err)
}

atomic.AddInt64(&i.wSeqNo, 1)

return nil
}

// Read fills buffer with received message.
func (i *Full) Read(r io.Reader, b *bin.Buffer) error {
if err := readFull(r, int(atomic.LoadInt64(&i.rSeqNo)), b); err != nil {
if err := readFull(r, int(atomic.AddInt64(&i.rSeqNo, 1)-1), b); err != nil {
return xerrors.Errorf("read full: %w", err)
}

atomic.AddInt64(&i.rSeqNo, 1)

if err := checkProtocolError(b); err != nil {
return err
}
Expand Down

0 comments on commit 9888c08

Please sign in to comment.