From c0916edc44e15f7bea4a6773b711bc5cd0886ed1 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Thu, 9 Mar 2023 13:54:18 +0100 Subject: [PATCH] events: prevent defer in loop Signed-off-by: Hidde Beydals --- cmd/flux/events.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/flux/events.go b/cmd/flux/events.go index 3521dc37..1176bc0c 100644 --- a/cmd/flux/events.go +++ b/cmd/flux/events.go @@ -199,7 +199,6 @@ func eventsCmdWatchRun(ctx context.Context, kubeclient client.WithWatch, listOpt if err != nil { return err } - defer eventWatch.Stop() firstIteration := true @@ -234,15 +233,19 @@ func eventsCmdWatchRun(ctx context.Context, kubeclient client.WithWatch, listOpt if err != nil { return err } - defer refEventWatch.Stop() - go receiveEventChan(ctx, refEventWatch, handleEvent) + go func() { + err := receiveEventChan(ctx, refEventWatch, handleEvent) + if err != nil { + logger.Failuref("error watching events: %s", err.Error()) + } + }() } return receiveEventChan(ctx, eventWatch, handleEvent) - } func receiveEventChan(ctx context.Context, eventWatch watch.Interface, f func(e watch.Event) error) error { + defer eventWatch.Stop() for { select { case e, ok := <-eventWatch.ResultChan():