Skip to content

Commit 3a3c36c

Browse files
johningvemeling
authored andcommitted
Avoid tripping the race detector
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.
1 parent 8dcf5aa commit 3a3c36c

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

cmd/protoc-gen-gorums/dev/ordering.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type orderedNodeStream struct {
7373
gorumsClient ordering.GorumsClient
7474
gorumsStream ordering.Gorums_NodeStreamClient
7575
streamMut sync.RWMutex
76-
streamBroken bool
76+
streamBroken uint32
7777
}
7878

7979
func (s *orderedNodeStream) connectOrderedStream(ctx context.Context, conn *grpc.ClientConn) error {
@@ -97,7 +97,7 @@ func (s *orderedNodeStream) sendMsgs(ctx context.Context) {
9797
case req = <-s.sendQ:
9898
}
9999
// return error if stream is broken
100-
if s.streamBroken {
100+
if atomic.LoadUint32(&s.streamBroken) == 1 {
101101
err := status.Errorf(codes.Unavailable, "stream is down")
102102
s.putResult(req.metadata.MessageID, &orderingResult{nid: s.node.ID(), reply: nil, err: err})
103103
continue
@@ -109,7 +109,7 @@ func (s *orderedNodeStream) sendMsgs(ctx context.Context) {
109109
s.streamMut.RUnlock()
110110
continue
111111
}
112-
s.streamBroken = true
112+
atomic.StoreUint32(&s.streamBroken, 1)
113113
s.streamMut.RUnlock()
114114
s.node.setLastErr(err)
115115
// return the error
@@ -123,7 +123,7 @@ func (s *orderedNodeStream) recvMsgs(ctx context.Context) {
123123
s.streamMut.RLock()
124124
err := s.gorumsStream.RecvMsg(resp)
125125
if err != nil {
126-
s.streamBroken = true
126+
atomic.StoreUint32(&s.streamBroken, 1)
127127
s.streamMut.RUnlock()
128128
s.node.setLastErr(err)
129129
// attempt to reconnect
@@ -151,7 +151,7 @@ func (s *orderedNodeStream) reconnectStream(ctx context.Context) {
151151
var err error
152152
s.gorumsStream, err = s.gorumsClient.NodeStream(ctx)
153153
if err == nil {
154-
s.streamBroken = false
154+
atomic.StoreUint32(&s.streamBroken, 0)
155155
return
156156
}
157157
s.node.setLastErr(err)

cmd/protoc-gen-gorums/gengorums/template_static.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)