Release the shared lock using a non cancelled context (#918)

This commit is contained in:
Todd Knight authored and GitHub committed 2021-02-05 12:58:54 -07:00
1 parent cb3980cb8c
commit 568594d1b3
3 files changed
+46 -13

No files matched your search

+30 -10
View File
@@ -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)
+6 -2
View File
@@ -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())
+10 -1
View File
@@ -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."))