From afd506a595be4bd26a9a4ed88f07154ce77852c3 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Fri, 4 Jun 2021 15:05:47 -0700 Subject: [PATCH] fix possible race where bootstrap data might not save Signed-off-by: Brian Downs --- pkg/cluster/cluster.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index cc71281937..fe0a988dc9 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -3,6 +3,7 @@ package cluster import ( "context" "net/url" + "runtime" "strings" "github.com/k3s-io/kine/pkg/endpoint" @@ -102,15 +103,22 @@ func (c *Cluster) Start(ctx context.Context) (<-chan struct{}, error) { // snapshots will be empty. if c.managedDB != nil { go func() { - for range ready { - if err := c.save(ctx); err != nil { - panic(err) + for { + select { + case <-ready: + if err := c.save(ctx); err != nil { + panic(err) + } + + if err := c.managedDB.StoreSnapshotData(ctx); err != nil { + logrus.Errorf("Failed to record snapshots for cluster: %v", err) + } + + return + default: + runtime.Gosched() } } - - if err := c.managedDB.StoreSnapshotData(ctx); err != nil { - logrus.Errorf("Failed to record snapshots for cluster: %v", err) - } }() }