From 568594d1b34135f0ad84c9e7017b6cf2e27c99be Mon Sep 17 00:00:00 2001 From: Todd Knight Date: Fri, 5 Feb 2021 12:58:54 -0700 Subject: [PATCH] Release the shared lock using a non cancelled context (#918) --- internal/cmd/base/initial_resources.go | 40 +++++++++++++++++++------- internal/cmd/base/servers.go | 8 ++++-- internal/cmd/commands/server/server.go | 11 ++++++- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/internal/cmd/base/initial_resources.go b/internal/cmd/base/initial_resources.go index a7203bfb8..06ba317bd 100644 --- a/internal/cmd/base/initial_resources.go +++ b/internal/cmd/base/initial_resources.go @@ -32,9 +32,13 @@ func (b *Server) CreateInitialLoginRole(ctx context.Context) (*iam.Role, error) return nil, fmt.Errorf("error adding config keys to kms: %w", err) } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() iamRepo, err := iam.NewRepository(rw, rw, kmsCache, iam.WithRandomReader(b.SecureRandomReader)) @@ -104,9 +108,13 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) (*password.AuthMet } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() am, err := pwRepo.CreateAuthMethod(cancelCtx, authMethod, @@ -212,9 +220,13 @@ func (b *Server) CreateInitialScopes(ctx context.Context) (*iam.Scope, *iam.Scop } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() iamRepo, err := iam.NewRepository(rw, rw, kmsCache) @@ -290,9 +302,13 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) (*static.HostCa } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() staticRepo, err := static.NewRepository(rw, rw, kmsCache) @@ -396,9 +412,13 @@ func (b *Server) CreateInitialTarget(ctx context.Context) (target.Target, error) } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() targetRepo, err := target.NewRepository(rw, rw, kmsCache) diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index d072dc1d9..f18a8e788 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -593,9 +593,13 @@ func (b *Server) CreateGlobalKmsKeys(ctx context.Context) error { } cancelCtx, cancel := context.WithCancel(ctx) + defer cancel() go func() { - <-b.ShutdownCh - cancel() + select { + case <-b.ShutdownCh: + cancel() + case <-cancelCtx.Done(): + } }() _, err = kms.CreateKeysTx(cancelCtx, rw, rw, b.RootKms, b.SecureRandomReader, scope.Global.String()) diff --git a/internal/cmd/commands/server/server.go b/internal/cmd/commands/server/server.go index 0784d1ba1..d746a09c9 100644 --- a/internal/cmd/commands/server/server.go +++ b/internal/cmd/commands/server/server.go @@ -1,10 +1,12 @@ package server import ( + "context" "fmt" "net" "runtime" "strings" + "time" "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/cmd/base" @@ -365,7 +367,13 @@ func (c *Command) Run(args []string) int { return 1 } defer func() { - if err := sMan.SharedUnlock(c.Context); err != nil { + // The base context has already been cancelled so we shouldn't use it here. + // 1 second is chosen so the shutdown is still responsive and this is a mostly + // non critical step since the lock should be released when the session with the + // database is closed. + ctx, cancel := context.WithTimeout(context.Background(), 1 * time.Second) + defer cancel() + if err := sMan.SharedUnlock(ctx); err != nil { c.UI.Error(fmt.Errorf("Unable to release shared lock to the database: %w", err).Error()) } }() @@ -376,6 +384,7 @@ func (c *Command) Run(args []string) int { } if !ckState.InitializationStarted { c.UI.Error(base.WrapAtLength("The database has not been initialized. Please run 'boundary database init'.")) + return 1 } if ckState.Dirty { c.UI.Error(base.WrapAtLength("Database is in a bad state. Please revert the database into the last known good state."))