Skip to content

Commit

Permalink
Fix nil job on allocation
Browse files Browse the repository at this point in the history
The way the copying was happening on the alloc_runner was by temporarily
setting the alloc.Job to nil, copying and then restoring it. This
created an issue in which when the alloc was shared (which it is in
server/client mode and between alloc_runner/task_runner) there were race
conditions that could create a panic.

Fixes #2605
  • Loading branch information
dadgar committed May 17, 2017
1 parent 7ac5a3e commit aa15ad5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
16 changes: 3 additions & 13 deletions client/alloc_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,8 @@ func copyTaskStates(states map[string]*structs.TaskState) map[string]*structs.Ta
func (r *AllocRunner) Alloc() *structs.Allocation {
r.allocLock.Lock()

// Clear the job before copying
job := r.alloc.Job

// Since we are clearing the job, anything that access the alloc.Job field
// must acquire the lock or access it via this method.
r.alloc.Job = nil

alloc := r.alloc.Copy()

// Restore
r.alloc.Job = job
alloc.Job = job
// Don't do a deep copy of the job
alloc := r.alloc.CopySkipJob()

// The status has explicitly been set.
if r.allocClientStatus != "" || r.allocClientDescription != "" {
Expand Down Expand Up @@ -774,7 +764,7 @@ func (r *AllocRunner) handleDestroy() {

return
case <-r.updateCh:
r.logger.Printf("[ERR] client: dropping update to terminal alloc '%s'", r.alloc.ID)
r.logger.Printf("[DEBUG] client: dropping update to terminal alloc '%s'", r.alloc.ID)
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3680,13 +3680,25 @@ type Allocation struct {
}

func (a *Allocation) Copy() *Allocation {
return a.copyImpl(true)
}

// Copy provides a copy of the allocation but doesn't deep copy the job
func (a *Allocation) CopySkipJob() *Allocation {
return a.copyImpl(false)
}

func (a *Allocation) copyImpl(job bool) *Allocation {
if a == nil {
return nil
}
na := new(Allocation)
*na = *a

na.Job = na.Job.Copy()
if job {
na.Job = na.Job.Copy()
}

na.Resources = na.Resources.Copy()
na.SharedResources = na.SharedResources.Copy()

Expand Down

0 comments on commit aa15ad5

Please sign in to comment.