From 6f2b09f67675bc1e2d487155ca39ca116ba508fb Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Thu, 1 Sep 2016 17:34:40 -0700 Subject: [PATCH] Add sanity check to SaveState Also just reuse the task states snapshot taken by `Alloc()` instead of doing a redundant copy. --- client/alloc_runner.go | 5 +---- client/util.go | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/client/alloc_runner.go b/client/alloc_runner.go index e2705a341..d79374ef1 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -180,12 +180,9 @@ func (r *AllocRunner) SaveState() error { func (r *AllocRunner) saveAllocRunnerState() error { // Create the snapshot. - r.taskStatusLock.RLock() - states := copyTaskStates(r.taskStates) - r.taskStatusLock.RUnlock() - alloc := r.Alloc() r.allocLock.Lock() + states := alloc.TaskStates allocClientStatus := r.allocClientStatus allocClientDescription := r.allocClientDescription r.allocLock.Unlock() diff --git a/client/util.go b/client/util.go index 369b5f059..ff2fa3d25 100644 --- a/client/util.go +++ b/client/util.go @@ -92,6 +92,13 @@ func persistState(path string, data interface{}) error { if err := os.Rename(tmpPath, path); err != nil { return fmt.Errorf("failed to rename tmp to path: %v", err) } + + // Sanity check since users have reported empty state files on disk + if stat, err := os.Stat(path); err != nil { + return fmt.Errorf("unable to stat state file %s: %v", path, err) + } else if stat.Size() == 0 { + return fmt.Errorf("persisted invalid state file %s; see https://github.com/hashicorp/nomad/issues/1367") + } return nil }