From 2108bb8e0097424f6a5879b0cc8c0802d8a27389 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Thu, 11 Feb 2021 11:50:41 -0500 Subject: [PATCH] Use an atomic to avoid a race in runEventDemuxer. (#10901) --- changelog/10901.txt | 3 +++ .../consul/consul_service_registration.go | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 changelog/10901.txt diff --git a/changelog/10901.txt b/changelog/10901.txt new file mode 100644 index 000000000..456ccb207 --- /dev/null +++ b/changelog/10901.txt @@ -0,0 +1,3 @@ +```release-note:bug +serviceregistration: Fix race during shutdown of Consul service registration. +``` diff --git a/serviceregistration/consul/consul_service_registration.go b/serviceregistration/consul/consul_service_registration.go index 5622ce9cd..65f1a970a 100644 --- a/serviceregistration/consul/consul_service_registration.go +++ b/serviceregistration/consul/consul_service_registration.go @@ -327,12 +327,12 @@ func (c *serviceRegistration) runEventDemuxer(waitGroup *sync.WaitGroup, shutdow // demuxer share a lock to synchronize information at the beginning // and end of a handler's life (or after a handler wakes up from // sleeping during a back-off/retry). - var shutdown bool + var shutdown atomicB.Bool var registeredServiceID string checkLock := new(int32) serviceRegLock := new(int32) - for !shutdown { + for !shutdown.Load() { select { case <-c.notifyActiveCh: // Run reconcile immediately upon active state change notification @@ -356,7 +356,7 @@ func (c *serviceRegistration) runEventDemuxer(waitGroup *sync.WaitGroup, shutdow // Enter handler with serviceRegLock held go func() { defer atomic.CompareAndSwapInt32(serviceRegLock, 1, 0) - for !shutdown { + for !shutdown.Load() { serviceID, err := c.reconcileConsul(registeredServiceID) if err != nil { if c.logger.IsWarn() { @@ -382,7 +382,7 @@ func (c *serviceRegistration) runEventDemuxer(waitGroup *sync.WaitGroup, shutdow // Enter handler with checkLock held go func() { defer atomic.CompareAndSwapInt32(checkLock, 1, 0) - for !shutdown { + for !shutdown.Load() { if err := c.runCheck(c.isSealed.Load()); err != nil { if c.logger.IsWarn() { c.logger.Warn("check unable to talk with Consul backend", "error", err) @@ -396,7 +396,7 @@ func (c *serviceRegistration) runEventDemuxer(waitGroup *sync.WaitGroup, shutdow } case <-shutdownCh: c.logger.Info("shutting down consul backend") - shutdown = true + shutdown.Store(true) } }