From 0780adfa7f4daa6c36975a9b9fc035a49507f0e4 Mon Sep 17 00:00:00 2001 From: Jasmine Dahilig Date: Tue, 24 Sep 2019 16:28:21 -0700 Subject: [PATCH] timeout after 5 seconds when client opens a data directory (#6348) --- client/state/state_database.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/client/state/state_database.go b/client/state/state_database.go index bb42a59ae..d6784e98e 100644 --- a/client/state/state_database.go +++ b/client/state/state_database.go @@ -6,6 +6,8 @@ import ( "path/filepath" "time" + "github.com/boltdb/bolt" + hclog "github.com/hashicorp/go-hclog" trstate "github.com/hashicorp/nomad/client/allocrunner/taskrunner/state" dmstate "github.com/hashicorp/nomad/client/devicemanager/state" @@ -120,9 +122,14 @@ func NewBoltStateDB(logger hclog.Logger, stateDir string) (StateDB, error) { } firstRun := fi == nil + // Timeout to force failure when accessing a data dir that is already in use + timeout := &bolt.Options{Timeout: 5 * time.Second} + // Create or open the boltdb state database - db, err := boltdd.Open(fn, 0600, nil) - if err != nil { + db, err := boltdd.Open(fn, 0600, timeout) + if err == bolt.ErrTimeout { + return nil, fmt.Errorf("timed out while opening database, is another Nomad process accessing data_dir %s?", stateDir) + } else if err != nil { return nil, fmt.Errorf("failed to create state database: %v", err) }