[lxc-devel] [lxd/master] lxd/cluster/heartbeat: Fix race in HeartbeatNode

tomponline on Github lxc-bot at linuxcontainers.org
Tue Jun 16 14:55:26 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 361 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200616/c01b2d29/attachment.bin>
-------------- next part --------------
From 7eccacb73fa8a0b6ac1d3237d61abec8b8e4eb3d Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 16 Jun 2020 15:53:14 +0100
Subject: [PATCH] lxd/cluster/heartbeat: Fix race in HeartbeatNode

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/cluster/heartbeat.go | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lxd/cluster/heartbeat.go b/lxd/cluster/heartbeat.go
index b43be4d9b1..5c7474ce32 100644
--- a/lxd/cluster/heartbeat.go
+++ b/lxd/cluster/heartbeat.go
@@ -125,7 +125,7 @@ func (hbState *APIHeartbeat) Send(ctx context.Context, cert *shared.CertInfo, lo
 
 		err := HeartbeatNode(ctx, address, cert, heartbeatData)
 		if err == nil {
-			hbState.Lock()
+			heartbeatData.Lock()
 			// Ensure only update nodes that exist in Members already.
 			hbNode, existing := hbState.Members[nodeID]
 			if !existing {
@@ -135,8 +135,8 @@ func (hbState *APIHeartbeat) Send(ctx context.Context, cert *shared.CertInfo, lo
 			hbNode.LastHeartbeat = time.Now()
 			hbNode.Online = true
 			hbNode.updated = true
-			hbState.Members[nodeID] = hbNode
-			hbState.Unlock()
+			heartbeatData.Members[nodeID] = hbNode
+			heartbeatData.Unlock()
 			logger.Debugf("Successful heartbeat for %s", address)
 		} else {
 			logger.Debugf("Failed heartbeat for %s: %v", address, err)
@@ -365,7 +365,9 @@ func HeartbeatNode(taskCtx context.Context, address string, cert *shared.CertInf
 	}
 
 	buffer := bytes.Buffer{}
+	heartbeatData.Lock()
 	err = json.NewEncoder(&buffer).Encode(heartbeatData)
+	heartbeatData.Unlock()
 	if err != nil {
 		return err
 	}


More information about the lxc-devel mailing list