diff --git a/retrieval/targetmanager.go b/retrieval/targetmanager.go index 43763466ac..c11efe98ef 100644 --- a/retrieval/targetmanager.go +++ b/retrieval/targetmanager.go @@ -171,12 +171,15 @@ func (tm *TargetManager) Run() { tm.running = true } -// handleTargetUpdates receives target group updates and handles them in the +// handleUpdates receives target group updates and handles them in the // context of the given job config. func (tm *TargetManager) handleUpdates(ch <-chan targetGroupUpdate, done <-chan struct{}) { for { select { - case update := <-ch: + case update, ok := <-ch: + if !ok { + return + } if update.tg == nil { break } diff --git a/retrieval/targetmanager_test.go b/retrieval/targetmanager_test.go index b30bfdfef5..54f9d9fc2c 100644 --- a/retrieval/targetmanager_test.go +++ b/retrieval/targetmanager_test.go @@ -376,3 +376,10 @@ func TestTargetManagerConfigUpdate(t *testing.T) { } } } + +func TestHandleUpdatesReturnsWhenUpdateChanIsClosed(t *testing.T) { + tm := NewTargetManager(nopAppender{}) + ch := make(chan targetGroupUpdate) + close(ch) + tm.handleUpdates(ch, make(chan struct{})) +}