[lxc-devel] [lxd/master] lxd/api: Fail requests made after shutdown has started

freeekanayaka on Github lxc-bot at linuxcontainers.org
Mon May 25 12:49:53 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 514 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200525/28bb33f1/attachment.bin>
-------------- next part --------------
From 23f555fc6a96cd364638a75af78991b4c74a5a3d Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Mon, 25 May 2020 12:37:26 +0100
Subject: [PATCH] lxd/api: Fail requests made after shutdown has started

We need to do this since now we're leaving the endpoints open for a good part of
the shutdown sequence, to allow for cluster membership adjustments.

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/api.go         | 11 +++++++++++
 lxd/daemon.go      |  2 +-
 lxd/main_daemon.go |  2 +-
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/lxd/api.go b/lxd/api.go
index 0b95fe729a..16d1aeff21 100644
--- a/lxd/api.go
+++ b/lxd/api.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"fmt"
 	"net/http"
 	"net/url"
 	"strings"
@@ -80,6 +81,16 @@ func (s *lxdHttpServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
 			resp.Render(rw)
 			return
 		}
+	} else {
+		var isClosing bool
+		s.d.clusterMembershipMutex.RLock()
+		isClosing = s.d.clusterMembershipClosing
+		s.d.clusterMembershipMutex.RUnlock()
+		if isClosing {
+			resp := response.SmartError(fmt.Errorf("Shutting down"))
+			resp.Render(rw)
+			return
+		}
 	}
 
 	// OPTIONS request don't need any further processing
diff --git a/lxd/daemon.go b/lxd/daemon.go
index 5e5fceb630..47ef372f7f 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -97,7 +97,7 @@ type Daemon struct {
 
 	// Serialize changes to cluster membership (joins, leaves, role
 	// changes).
-	clusterMembershipMutex   sync.Mutex
+	clusterMembershipMutex   sync.RWMutex
 	clusterMembershipClosing bool // Prevent further rebalances
 }
 
diff --git a/lxd/main_daemon.go b/lxd/main_daemon.go
index d7761bd03c..e6ade1aac0 100644
--- a/lxd/main_daemon.go
+++ b/lxd/main_daemon.go
@@ -85,8 +85,8 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
 			containersShutdown(s)
 			networkShutdown(s)
 		} else {
-			d.Kill()
 			logger.Infof("Received '%s signal', exiting", sig)
+			d.Kill()
 		}
 
 	case <-d.shutdownChan:


More information about the lxc-devel mailing list