Skip to content

Commit

Permalink
Fixes race condition in EventStream
Browse files Browse the repository at this point in the history
Locks the mutex for reading when getting Length(). Otherwise, a data race can occur. This can happen despite the previous atomic read because writes are non-atomic.

Signed-off-by: Emily Casey <[email protected]>
  • Loading branch information
ekcasey committed Aug 8, 2023
1 parent c163360 commit 1bae8af
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion eventstream/eventstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func (es *EventStream) Publish(evt interface{}) {

// Returns an integer that represents the current number of subscribers to the stream
func (es *EventStream) Length() int32 {
return atomic.LoadInt32(&es.counter)
es.RLock()
defer es.RUnlock()
return es.counter
}

// Subscription is returned from the Subscribe function.
Expand Down

0 comments on commit 1bae8af

Please sign in to comment.