From 1bae8af6cd4572302f9347b11433a2732609cf90 Mon Sep 17 00:00:00 2001 From: Emily Casey Date: Tue, 8 Aug 2023 15:57:19 -0400 Subject: [PATCH] Fixes race condition in EventStream 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 --- eventstream/eventstream.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eventstream/eventstream.go b/eventstream/eventstream.go index 3487b05f..290337cf 100644 --- a/eventstream/eventstream.go +++ b/eventstream/eventstream.go @@ -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.