Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hubble: Fix data races in pkg/hubble.TestRingReader_NextFollow_WithEmptyRing #17397

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions pkg/hubble/container/ring_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"sync"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/cilium/pkg/lock"
)

// RingReader is a reader for a Ring container.
type RingReader struct {
ring *Ring
idx uint64
ctx context.Context
mutex lock.Mutex // protects writes to followChan
followChan chan *v1.Event
followChanLen int
wg sync.WaitGroup
Expand Down Expand Up @@ -75,16 +77,21 @@ func (r *RingReader) NextFollow(ctx context.Context) *v1.Event {
// if the context changed between invocations, we also have to restart
// readFrom, as the old readFrom instance will be using the old context.
if r.ctx != ctx {
r.mutex.Lock()
if r.followChan == nil {
r.followChan = make(chan *v1.Event, r.followChanLen)
}
r.mutex.Unlock()

r.wg.Add(1)
go func(ctx context.Context) {
r.ring.readFrom(ctx, r.idx, r.followChan)
r.mutex.Lock()
if ctx.Err() != nil && r.followChan != nil { // context is done
close(r.followChan)
r.followChan = nil
}
r.mutex.Unlock()
r.wg.Done()
}(ctx)
r.ctx = ctx
Expand All @@ -95,8 +102,12 @@ func (r *RingReader) NextFollow(ctx context.Context) *v1.Event {
}
}()

r.mutex.Lock()
followChan := r.followChan
r.mutex.Unlock()

select {
case e, ok := <-r.followChan:
case e, ok := <-followChan:
if !ok {
// the channel is closed so the context is done
return nil
Expand All @@ -110,9 +121,10 @@ func (r *RingReader) NextFollow(ctx context.Context) *v1.Event {
}
}

// Close waits for any method to return and closes the RingReader. It is not
// Close waits for any spawned go routines to finish. It is not
// required to call Close on a RingReader but it may be useful for specific
// situations such as testing.
// situations such as testing. Must not be called concurrently with NextFollow,
// as otherwise NextFollow spawns new go routines that are not waited on.
func (r *RingReader) Close() error {
r.wg.Wait()
return nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/hubble/container/ring_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,13 @@ func TestRingReader_NextFollow_WithEmptyRing(t *testing.T) {
reader := NewRingReader(ring, ring.LastWriteParallel())
ctx, cancel := context.WithCancel(context.Background())
c := make(chan *v1.Event)
done := make(chan struct{})
go func() {
select {
case <-ctx.Done():
case c <- reader.NextFollow(ctx):
}
close(done)
}()
select {
case <-c:
Expand All @@ -302,5 +304,6 @@ func TestRingReader_NextFollow_WithEmptyRing(t *testing.T) {
// the call blocked, we're good
}
cancel()
<-done
assert.Nil(t, reader.Close())
}