[lxc-devel] [lxd/master] lxd/cluster: Use goroutine to avoid cgo crash

stgraber on Github lxc-bot at linuxcontainers.org
Thu Jul 18 19:22:09 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190718/65e71fc0/attachment.bin>
-------------- next part --------------
From 9b5f28994b9529a89893f15b297b779624b74104 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 18 Jul 2019 15:21:36 -0400
Subject: [PATCH] lxd/cluster: Use goroutine to avoid cgo crash
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cluster/gateway.go | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/lxd/cluster/gateway.go b/lxd/cluster/gateway.go
index 4a760c9a98..d194c10e91 100644
--- a/lxd/cluster/gateway.go
+++ b/lxd/cluster/gateway.go
@@ -46,15 +46,29 @@ func NewGateway(db *db.Node, cert *shared.CertInfo, options ...Option) (*Gateway
 	}
 
 	gateway := &Gateway{
-		db:        db,
-		cert:      cert,
-		options:   o,
-		ctx:       ctx,
-		cancel:    cancel,
-		upgradeCh: make(chan struct{}, 16),
-		acceptCh:  make(chan net.Conn),
-		store:     &dqliteServerStore{},
-	}
+		db:          db,
+		cert:        cert,
+		options:     o,
+		ctx:         ctx,
+		cancel:      cancel,
+		upgradeCh:   make(chan struct{}, 16),
+		acceptCh:    make(chan net.Conn),
+		heartbeatCh: make(chan error, 1),
+		store:       &dqliteServerStore{},
+	}
+
+	// Handler for dqlite triggered hearbeats
+	go func() {
+		for {
+			select {
+			case <-gateway.ctx.Done():
+				return
+			case <-gateway.heartbeatCh:
+				gateway.heartbeat(gateway.ctx, true)
+				continue
+			}
+		}
+	}()
 
 	err := gateway.init()
 	if err != nil {
@@ -104,6 +118,7 @@ type Gateway struct {
 	upgradeTriggered bool
 
 	// Used for the heartbeat handler
+	heartbeatCh       chan error
 	Cluster           *db.Cluster
 	HeartbeatNodeHook func(*APIHeartbeat)
 
@@ -820,7 +835,7 @@ func (g *Gateway) watchFunc(oldState int, newState int) {
 		logger.Info("Node was elected as dqlite leader", log.Ctx{"id": g.raft.info.ID, "address": g.raft.info.Address})
 
 		// Trigger an immediate full hearbeat run
-		go g.heartbeat(g.ctx, true)
+		g.heartbeatCh <- nil
 	}
 }
 


More information about the lxc-devel mailing list