From f480466d08831bba554bdeef6f25815d0fb541cb Mon Sep 17 00:00:00 2001 From: ersonp Date: Tue, 28 Sep 2021 15:39:41 +0530 Subject: [PATCH 1/4] Add mutex lock Add mutex lock to DeleteEntry in servicedisc to fix the datarace --- pkg/servicedisc/client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/servicedisc/client.go b/pkg/servicedisc/client.go index 21b88ed52f..472a5f7e9e 100644 --- a/pkg/servicedisc/client.go +++ b/pkg/servicedisc/client.go @@ -226,6 +226,9 @@ func (c *HTTPClient) postEntry(ctx context.Context) (Service, error) { // DeleteEntry calls 'DELETE /api/services/{entry_addr}'. func (c *HTTPClient) DeleteEntry(ctx context.Context) (err error) { + c.entryMx.Lock() + defer c.entryMx.Unlock() + auth, err := c.Auth(ctx) if err != nil { return err From b13bb466eac041c1a3e61bc10b7ff2e48cff7f00 Mon Sep 17 00:00:00 2001 From: ersonp Date: Tue, 28 Sep 2021 15:41:57 +0530 Subject: [PATCH 2/4] Update mutex comment The comment was updated to reflect the changes. --- pkg/servicedisc/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/servicedisc/client.go b/pkg/servicedisc/client.go index 472a5f7e9e..f8c5e271e1 100644 --- a/pkg/servicedisc/client.go +++ b/pkg/servicedisc/client.go @@ -45,7 +45,7 @@ type HTTPClient struct { log logrus.FieldLogger conf Config entry Service - entryMx sync.Mutex // only used if UpdateLoop && UpdateStats functions are used. + entryMx sync.Mutex // only used if RegisterEntry && DeleteEntry functions are used. client http.Client } From d44cde612e13e2160dd0446c7eefb81c89563235 Mon Sep 17 00:00:00 2001 From: ersonp Date: Tue, 28 Sep 2021 15:58:14 +0530 Subject: [PATCH 3/4] Add stopOnce to Stop() Stop() of service updater is called from two places so stopOnce sync.Once was added as we only need to stop it once. --- pkg/app/appdisc/discovery_manager.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/app/appdisc/discovery_manager.go b/pkg/app/appdisc/discovery_manager.go index 0a0d807817..2259f95f7c 100644 --- a/pkg/app/appdisc/discovery_manager.go +++ b/pkg/app/appdisc/discovery_manager.go @@ -2,6 +2,7 @@ package appdisc import ( "context" + "sync" "github.com/skycoin/skywire/pkg/servicedisc" ) @@ -24,7 +25,8 @@ func (emptyUpdater) Stop() {} // serviceUpdater updates service-discovery entry of locally running App. type serviceUpdater struct { - client *servicedisc.HTTPClient + client *servicedisc.HTTPClient + stopOnce sync.Once } func (u *serviceUpdater) Start() { @@ -35,8 +37,10 @@ func (u *serviceUpdater) Start() { } func (u *serviceUpdater) Stop() { - ctx := context.Background() - if err := u.client.DeleteEntry(ctx); err != nil { - return - } + u.stopOnce.Do(func() { + ctx := context.Background() + if err := u.client.DeleteEntry(ctx); err != nil { + return + } + }) } From 66257eeb22e847ed5f6aa9e2cb147dc7552292f2 Mon Sep 17 00:00:00 2001 From: ersonp Date: Tue, 28 Sep 2021 16:14:19 +0530 Subject: [PATCH 4/4] Add debug logs Debug logs added to DeleteEntry() and RegisterEntry() in servicedisc. --- pkg/servicedisc/client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/servicedisc/client.go b/pkg/servicedisc/client.go index f8c5e271e1..c602ade2bc 100644 --- a/pkg/servicedisc/client.go +++ b/pkg/servicedisc/client.go @@ -166,6 +166,7 @@ func (c *HTTPClient) RegisterEntry(ctx context.Context) error { return err } c.entry = entry + c.log.WithField("entry", c.entry).Debug("Entry registered successfully") return nil } @@ -263,6 +264,7 @@ func (c *HTTPClient) DeleteEntry(ctx context.Context) (err error) { } return &hErr } + c.log.WithField("entry", c.entry).Debug("Entry deleted successfully") return nil }