From edb14fc904bf1e9f383f4c8d9b71769e4f307cd1 Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Fri, 15 Jan 2021 13:08:24 -0800 Subject: [PATCH 1/3] vault/core_metrics.go --- vault/core_metrics.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/vault/core_metrics.go b/vault/core_metrics.go index 3d24c2edda3a..dd61f72a3bc3 100644 --- a/vault/core_metrics.go +++ b/vault/core_metrics.go @@ -256,6 +256,13 @@ type kvMount struct { func (c *Core) findKvMounts() []*kvMount { mounts := make([]*kvMount, 0) + // emitMetrics doesn't grab the statelock, so this code might run during or after the seal process. + // Therefore, we need to check if c.mounts is nil. If we do not, emitMetrics will panic if this is + // run after seal. + if c.mounts == nil { + return mounts + } + c.mountsLock.RLock() defer c.mountsLock.RUnlock() From d5403eae93d05c2c009ae83ab0d695824c2a00b2 Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Fri, 15 Jan 2021 13:10:52 -0800 Subject: [PATCH 2/3] changelog --- changelog/10708.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog/10708.txt diff --git a/changelog/10708.txt b/changelog/10708.txt new file mode 100644 index 000000000000..a33bfb078c20 --- /dev/null +++ b/changelog/10708.txt @@ -0,0 +1,3 @@ +```release-note:bug +metrics: Protect emitMetrics from panicking during post-seal +``` \ No newline at end of file From 65a9a04cf15b0c5f712d370a485b407fd3470d16 Mon Sep 17 00:00:00 2001 From: Hridoy Roy Date: Fri, 15 Jan 2021 13:16:04 -0800 Subject: [PATCH 3/3] comments --- vault/core_metrics.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vault/core_metrics.go b/vault/core_metrics.go index dd61f72a3bc3..c7d829566b81 100644 --- a/vault/core_metrics.go +++ b/vault/core_metrics.go @@ -256,6 +256,9 @@ type kvMount struct { func (c *Core) findKvMounts() []*kvMount { mounts := make([]*kvMount, 0) + c.mountsLock.RLock() + defer c.mountsLock.RUnlock() + // emitMetrics doesn't grab the statelock, so this code might run during or after the seal process. // Therefore, we need to check if c.mounts is nil. If we do not, emitMetrics will panic if this is // run after seal. @@ -263,9 +266,6 @@ func (c *Core) findKvMounts() []*kvMount { return mounts } - c.mountsLock.RLock() - defer c.mountsLock.RUnlock() - for _, entry := range c.mounts.Entries { if entry.Type == "kv" { version, ok := entry.Options["version"]