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

Fixes memory leak when blocking on /event/list #4482

Merged
merged 1 commit into from
Aug 2, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/event_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ SETUP_NOTIFY:
if b.MinQueryIndex > 0 {
notifyCh = make(chan struct{}, 1)
s.agent.eventNotify.Wait(notifyCh)
defer s.agent.eventNotify.Clear(notifyCh)
}

RUN_QUERY:
Expand Down
7 changes: 0 additions & 7 deletions agent/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,3 @@ func (n *NotifyGroup) Clear(ch chan struct{}) {
}
delete(n.notify, ch)
}

// WaitCh allocates a channel that is subscribed to notifications
func (n *NotifyGroup) WaitCh() chan struct{} {
ch := make(chan struct{}, 1)
n.Wait(ch)
return ch
}
17 changes: 13 additions & 4 deletions agent/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import (
"testing"
)

// Used to be defined in NotifyGroup.WaitCh but was only used in tests and prone
// to leaking memory if anything real did use it because there is no way to
// clear the chan later.
func testWaitCh(t *testing.T, grp *NotifyGroup) chan struct{} {
ch := make(chan struct{}, 1)
grp.Wait(ch)
return ch
}

func TestNotifyGroup(t *testing.T) {
grp := &NotifyGroup{}

ch1 := grp.WaitCh()
ch2 := grp.WaitCh()
ch1 := testWaitCh(t, grp)
ch2 := testWaitCh(t, grp)

select {
case <-ch1:
Expand All @@ -35,7 +44,7 @@ func TestNotifyGroup(t *testing.T) {
}

// Should be unregistered
ch3 := grp.WaitCh()
ch3 := testWaitCh(t, grp)
grp.Notify()

select {
Expand All @@ -58,7 +67,7 @@ func TestNotifyGroup(t *testing.T) {
func TestNotifyGroup_Clear(t *testing.T) {
grp := &NotifyGroup{}

ch1 := grp.WaitCh()
ch1 := testWaitCh(t, grp)
grp.Clear(ch1)

grp.Notify()
Expand Down