Skip to content

Commit

Permalink
Lock around saving state
Browse files Browse the repository at this point in the history
Prevent interleaving state syncs as it could conceivably lead to
empty state files as per #1367
  • Loading branch information
schmichael committed Sep 2, 2016
1 parent 6f2b09f commit 8d9468d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions client/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type AllocRunner struct {
destroyCh chan struct{}
destroyLock sync.Mutex
waitCh chan struct{}

// serialize saveAllocRunnerState calls
persistLock sync.Mutex
}

// allocRunnerState is used to snapshot the state of the alloc runner
Expand Down Expand Up @@ -179,8 +182,12 @@ func (r *AllocRunner) SaveState() error {
}

func (r *AllocRunner) saveAllocRunnerState() error {
r.persistLock.Lock()
defer r.persistLock.Unlock()

// Create the snapshot.
alloc := r.Alloc()

r.allocLock.Lock()
states := alloc.TaskStates
allocClientStatus := r.allocClientStatus
Expand Down
6 changes: 6 additions & 0 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type TaskRunner struct {
destroyLock sync.Mutex
destroyEvent *structs.TaskEvent
waitCh chan struct{}

// serialize SaveState calls
persistLock sync.Mutex
}

// taskRunnerState is used to snapshot the state of the task runner
Expand Down Expand Up @@ -186,6 +189,9 @@ func (r *TaskRunner) RestoreState() error {

// SaveState is used to snapshot our state
func (r *TaskRunner) SaveState() error {
r.persistLock.Lock()
defer r.persistLock.Unlock()

snap := taskRunnerState{
Task: r.task,
Version: r.config.Version,
Expand Down

0 comments on commit 8d9468d

Please sign in to comment.