Skip to content

Commit

Permalink
Avoid tripping the race detector
Browse files Browse the repository at this point in the history
We need to set a flag so that the sendMsgs goroutine can return errors
if the stream is down. However, using a bool for this was tripping the
race detector. To avoid this, we can use atomic load/store instead.
  • Loading branch information
johningve authored and meling committed Jul 20, 2020
1 parent 8dcf5aa commit 3a3c36c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions cmd/protoc-gen-gorums/dev/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type orderedNodeStream struct {
gorumsClient ordering.GorumsClient
gorumsStream ordering.Gorums_NodeStreamClient
streamMut sync.RWMutex
streamBroken bool
streamBroken uint32
}

func (s *orderedNodeStream) connectOrderedStream(ctx context.Context, conn *grpc.ClientConn) error {
Expand All @@ -97,7 +97,7 @@ func (s *orderedNodeStream) sendMsgs(ctx context.Context) {
case req = <-s.sendQ:
}
// return error if stream is broken
if s.streamBroken {
if atomic.LoadUint32(&s.streamBroken) == 1 {
err := status.Errorf(codes.Unavailable, "stream is down")
s.putResult(req.metadata.MessageID, &orderingResult{nid: s.node.ID(), reply: nil, err: err})
continue
Expand All @@ -109,7 +109,7 @@ func (s *orderedNodeStream) sendMsgs(ctx context.Context) {
s.streamMut.RUnlock()
continue
}
s.streamBroken = true
atomic.StoreUint32(&s.streamBroken, 1)
s.streamMut.RUnlock()
s.node.setLastErr(err)
// return the error
Expand All @@ -123,7 +123,7 @@ func (s *orderedNodeStream) recvMsgs(ctx context.Context) {
s.streamMut.RLock()
err := s.gorumsStream.RecvMsg(resp)
if err != nil {
s.streamBroken = true
atomic.StoreUint32(&s.streamBroken, 1)
s.streamMut.RUnlock()
s.node.setLastErr(err)
// attempt to reconnect
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *orderedNodeStream) reconnectStream(ctx context.Context) {
var err error
s.gorumsStream, err = s.gorumsClient.NodeStream(ctx)
if err == nil {
s.streamBroken = false
atomic.StoreUint32(&s.streamBroken, 0)
return
}
s.node.setLastErr(err)
Expand Down
10 changes: 5 additions & 5 deletions cmd/protoc-gen-gorums/gengorums/template_static.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a3c36c

Please sign in to comment.