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

fix: data race / nil channel read in pattern aggregation push #15410

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pkg/pattern/aggregation/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type Push struct {
contentType string
logger log.Logger

// shutdown channels
quit chan struct{}
running sync.WaitGroup
quit chan struct{}
quitOnce sync.Once

// auth
username, password string
Expand Down Expand Up @@ -149,6 +150,7 @@ func NewPush(
metrics: metrics,
}

p.running.Add(1)
go p.run(pushPeriod)
return p, nil
}
Expand All @@ -160,10 +162,10 @@ func (p *Push) WriteEntry(ts time.Time, e string, lbls labels.Labels) {

// Stop will cancel any ongoing requests and stop the goroutine listening for requests
func (p *Push) Stop() {
if p.quit != nil {
p.quitOnce.Do(func() {
close(p.quit)
p.quit = nil
}
})
p.running.Wait()
}

// buildPayload creates the snappy compressed protobuf to send to Loki
Expand Down Expand Up @@ -244,6 +246,8 @@ func (p *Push) buildPayload(ctx context.Context) ([]byte, error) {

// run pulls lines out of the channel and sends them to Loki
func (p *Push) run(pushPeriod time.Duration) {
p.running.Done()
na-- marked this conversation as resolved.
Show resolved Hide resolved

ctx, cancel := context.WithCancel(context.Background())
pushTicker := time.NewTimer(pushPeriod)
defer pushTicker.Stop()
Expand Down
1 change: 1 addition & 0 deletions pkg/pattern/aggregation/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func Test_Push(t *testing.T) {
lbls2,
)

p.running.Add(1)
go p.run(time.Nanosecond)

select {
Expand Down
Loading