mirror of https://github.com/hashicorp/consul
Fixes memory leak when blocking on /event/list (#4482)
parent
ec755b4ba7
commit
71dd3b408a
|
@ -125,6 +125,7 @@ SETUP_NOTIFY:
|
||||||
if b.MinQueryIndex > 0 {
|
if b.MinQueryIndex > 0 {
|
||||||
notifyCh = make(chan struct{}, 1)
|
notifyCh = make(chan struct{}, 1)
|
||||||
s.agent.eventNotify.Wait(notifyCh)
|
s.agent.eventNotify.Wait(notifyCh)
|
||||||
|
defer s.agent.eventNotify.Clear(notifyCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
RUN_QUERY:
|
RUN_QUERY:
|
||||||
|
|
|
@ -46,10 +46,3 @@ func (n *NotifyGroup) Clear(ch chan struct{}) {
|
||||||
}
|
}
|
||||||
delete(n.notify, ch)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,11 +4,20 @@ import (
|
||||||
"testing"
|
"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) {
|
func TestNotifyGroup(t *testing.T) {
|
||||||
grp := &NotifyGroup{}
|
grp := &NotifyGroup{}
|
||||||
|
|
||||||
ch1 := grp.WaitCh()
|
ch1 := testWaitCh(t, grp)
|
||||||
ch2 := grp.WaitCh()
|
ch2 := testWaitCh(t, grp)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ch1:
|
case <-ch1:
|
||||||
|
@ -35,7 +44,7 @@ func TestNotifyGroup(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should be unregistered
|
// Should be unregistered
|
||||||
ch3 := grp.WaitCh()
|
ch3 := testWaitCh(t, grp)
|
||||||
grp.Notify()
|
grp.Notify()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
@ -58,7 +67,7 @@ func TestNotifyGroup(t *testing.T) {
|
||||||
func TestNotifyGroup_Clear(t *testing.T) {
|
func TestNotifyGroup_Clear(t *testing.T) {
|
||||||
grp := &NotifyGroup{}
|
grp := &NotifyGroup{}
|
||||||
|
|
||||||
ch1 := grp.WaitCh()
|
ch1 := testWaitCh(t, grp)
|
||||||
grp.Clear(ch1)
|
grp.Clear(ch1)
|
||||||
|
|
||||||
grp.Notify()
|
grp.Notify()
|
||||||
|
|
Loading…
Reference in New Issue