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

Make buffered channel to avoid goroutines leak in groupbytraceprocessor #1505

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
12 changes: 11 additions & 1 deletion processor/groupbytraceprocessor/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,25 @@ func (em *eventMachine) shutdown() {
em.closed = true
em.shutdownLock.Unlock()

done := make(chan struct{})

// we never return an error here
ok, _ := doWithTimeout(em.shutdownTimeout, func() error {
for {
if len(em.events) == 0 {
return nil
}
time.Sleep(100 * time.Millisecond)

// Do not leak goroutine
select {
case <-done:
return nil
default:
}
}
})
close(done)

if !ok {
em.logger.Info("forcing the shutdown of the event manager", zap.Int("pending-events", len(em.events)))
Expand Down Expand Up @@ -254,7 +264,7 @@ func (em *eventMachine) handleEventWithObservability(event string, do func() err
// doWithTimeout wraps a function in a timeout, returning whether it succeeded before timing out.
// If the function returns an error within the timeout, it's considered as succeeded and the error will be returned back to the caller.
func doWithTimeout(timeout time.Duration, do func() error) (bool, error) {
done := make(chan error)
done := make(chan error, 1)
jpkrohling marked this conversation as resolved.
Show resolved Hide resolved
go func() {
done <- do()
}()
Expand Down
10 changes: 8 additions & 2 deletions processor/groupbytraceprocessor/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,23 @@ func TestForceShutdown(t *testing.T) {

// verify
assert.True(t, duration > 20*time.Millisecond)

// wait for shutdown goroutine to end
time.Sleep(100 * time.Millisecond)
}

func TestDoWithTimeout(t *testing.T) {
// prepare
start := time.Now()

done := make(chan struct{})

// test
doWithTimeout(10*time.Millisecond, func() error {
time.Sleep(20 * time.Second)
doWithTimeout(5*time.Millisecond, func() error {
<-done
return nil
})
close(done)

// verify
assert.WithinDuration(t, start, time.Now(), 20*time.Millisecond)
Expand Down
Loading