From 502e652b7afc9be137995783245a4f9ad31a7160 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 7 Feb 2020 23:35:56 +0100 Subject: [PATCH] fix: Avoid closing target in RegisterNotificationTargets (#8966) This will prevent a double target Close() call when fetchLambdaInfo() is executed (mc admin info) This fixes a crash when mc admin info is called. --- cmd/config/notify/parse.go | 54 ++++++++++++++------------------------ 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/cmd/config/notify/parse.go b/cmd/config/notify/parse.go index 328d9f015..dbe2d96a5 100644 --- a/cmd/config/notify/parse.go +++ b/cmd/config/notify/parse.go @@ -42,7 +42,14 @@ const ( func TestNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport, targetIDs []event.TargetID) error { test := true - _, err := RegisterNotificationTargets(cfg, doneCh, transport, targetIDs, test) + targets, err := RegisterNotificationTargets(cfg, doneCh, transport, targetIDs, test) + if err == nil { + // Close all targets since we are only testing connections. + for _, t := range targets.TargetMap() { + _ = t.Close() + } + } + return err } @@ -58,8 +65,18 @@ func GetNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport // * Add a new target in pkg/event/target package. // * Add newly added target configuration to serverConfig.Notify.. // * Handle the configuration in this function to create/add into TargetList. -func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport, targetIDs []event.TargetID, test bool) (*event.TargetList, error) { - targetList := event.NewTargetList() +func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, transport *http.Transport, targetIDs []event.TargetID, test bool) (targetList *event.TargetList, registerErr error) { + targetList = event.NewTargetList() + + // Automatially close all connections when an error occur + defer func() { + if registerErr != nil { + for _, t := range targetList.TargetMap() { + _ = t.Close() + } + } + }() + if err := checkValidNotificationKeys(cfg); err != nil { return nil, err } @@ -125,9 +142,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range esTargets { @@ -142,9 +156,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range kafkaTargets { @@ -159,9 +170,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range mqttTargets { @@ -176,9 +184,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range mysqlTargets { @@ -192,9 +197,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range natsTargets { @@ -208,9 +210,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range nsqTargets { @@ -224,9 +223,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range postgresTargets { @@ -240,9 +236,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range redisTargets { @@ -256,9 +249,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err = targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - } } for id, args := range webhookTargets { @@ -272,10 +262,6 @@ func RegisterNotificationTargets(cfg config.Config, doneCh <-chan struct{}, tran if err := targetList.Add(newTarget); err != nil { return nil, err } - if test { - newTarget.Close() - continue - } } if test {