Skip to content

Commit 0bce3e1

Browse files
authored
Merge pull request #1787 from fcrisciani/goroutine_leak
Fix leak of handleTableEvents
2 parents eed0fe8 + 1045cfe commit 0bce3e1

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

agent.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,13 @@ func (n *network) cancelDriverWatches() {
722722
}
723723
}
724724

725-
func (c *controller) handleTableEvents(ch chan events.Event, fn func(events.Event)) {
725+
func (c *controller) handleTableEvents(ch *events.Channel, fn func(events.Event)) {
726726
for {
727727
select {
728-
case ev, ok := <-ch:
729-
if !ok {
730-
return
731-
}
732-
728+
case ev := <-ch.C:
733729
fn(ev)
730+
case <-ch.Done():
731+
return
734732
}
735733
}
736734
}

networkdb/networkdb_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,17 @@ func TestNetworkDBWatch(t *testing.T) {
339339
err = dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
340340
assert.NoError(t, err)
341341

342-
testWatch(t, ch, CreateEvent{}, "test_table", "network1", "test_key", "test_value")
342+
testWatch(t, ch.C, CreateEvent{}, "test_table", "network1", "test_key", "test_value")
343343

344344
err = dbs[0].UpdateEntry("test_table", "network1", "test_key", []byte("test_updated_value"))
345345
assert.NoError(t, err)
346346

347-
testWatch(t, ch, UpdateEvent{}, "test_table", "network1", "test_key", "test_updated_value")
347+
testWatch(t, ch.C, UpdateEvent{}, "test_table", "network1", "test_key", "test_updated_value")
348348

349349
err = dbs[0].DeleteEntry("test_table", "network1", "test_key")
350350
assert.NoError(t, err)
351351

352-
testWatch(t, ch, DeleteEvent{}, "test_table", "network1", "test_key", "")
352+
testWatch(t, ch.C, DeleteEvent{}, "test_table", "network1", "test_key", "")
353353

354354
cancel()
355355
closeNetworkDBInstances(dbs)

networkdb/watch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type DeleteEvent event
4343
// filter is an empty string it acts as a wildcard for that
4444
// field. Watch returns a channel of events, where the events will be
4545
// sent.
46-
func (nDB *NetworkDB) Watch(tname, nid, key string) (chan events.Event, func()) {
46+
func (nDB *NetworkDB) Watch(tname, nid, key string) (*events.Channel, func()) {
4747
var matcher events.Matcher
4848

4949
if tname != "" || nid != "" || key != "" {
@@ -82,7 +82,7 @@ func (nDB *NetworkDB) Watch(tname, nid, key string) (chan events.Event, func())
8282
}
8383

8484
nDB.broadcaster.Add(sink)
85-
return ch.C, func() {
85+
return ch, func() {
8686
nDB.broadcaster.Remove(sink)
8787
ch.Close()
8888
sink.Close()

0 commit comments

Comments
 (0)