Skip to content

Commit d734d8c

Browse files
Michal Tichákknopers8
authored andcommitted
[core] fixed race conditions caused by repeated use of GetParent
1 parent f7f5d95 commit d734d8c

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

core/task/manager.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,8 @@ func (m *Manager) updateTaskState(taskId string, state string) {
976976
taskPtr.state = st
977977
taskPtr.safeToStop = false
978978
taskPtr.SendEvent(&event.TaskEvent{Name: taskPtr.GetName(), TaskID: taskId, State: state, Hostname: taskPtr.hostname, ClassName: taskPtr.GetClassName()})
979-
if taskPtr.GetParent() != nil {
980-
taskPtr.GetParent().UpdateState(st)
979+
if parent := taskPtr.GetParent(); parent != nil {
980+
parent.UpdateState(st)
981981
}
982982
}
983983

@@ -1049,8 +1049,8 @@ func (m *Manager) updateTaskStatus(status *mesos.TaskStatus) {
10491049
WithField("partition", envId.String()).
10501050
Debug("task active (received TASK_RUNNING event from executor)")
10511051
taskPtr.status = ACTIVE
1052-
if taskPtr.GetParent() != nil {
1053-
taskPtr.GetParent().UpdateStatus(ACTIVE)
1052+
if parent := taskPtr.GetParent(); parent != nil {
1053+
parent.UpdateStatus(ACTIVE)
10541054
}
10551055
if status.GetAgentID() != nil {
10561056
taskPtr.agentId = status.GetAgentID().GetValue()
@@ -1062,8 +1062,8 @@ func (m *Manager) updateTaskStatus(status *mesos.TaskStatus) {
10621062
case mesos.TASK_DROPPED, mesos.TASK_LOST, mesos.TASK_KILLED, mesos.TASK_FAILED, mesos.TASK_ERROR, mesos.TASK_FINISHED:
10631063

10641064
taskPtr.status = INACTIVE
1065-
if taskPtr.GetParent() != nil {
1066-
taskPtr.GetParent().UpdateStatus(INACTIVE)
1065+
if parent := taskPtr.GetParent(); parent != nil {
1066+
parent.UpdateStatus(INACTIVE)
10671067
}
10681068
}
10691069
taskPtr.SendEvent(&event.TaskEvent{Name: taskPtr.GetName(), TaskID: taskId, Status: taskPtr.status.String(), Hostname: taskPtr.hostname, ClassName: taskPtr.GetClassName()})

core/task/task.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ func (t *Task) GetControlMode() controlmode.ControlMode {
238238
// If it's a BASIC task but its parent role uses it as a HOOK,
239239
// we modify the actual control mode of the task.
240240
// The class itself can never be HOOK, only BASIC
241-
if class.Control.Mode == controlmode.BASIC && t.GetParent() != nil {
242-
traits := t.GetParent().GetTaskTraits()
241+
parent := t.GetParent()
242+
if class.Control.Mode == controlmode.BASIC && parent != nil {
243+
traits := parent.GetTaskTraits()
243244
if len(traits.Trigger) > 0 {
244245
return controlmode.HOOK
245246
}
@@ -409,7 +410,7 @@ func (t *Task) BuildTaskCommand(role parentRole) (err error) {
409410
// If it's a HOOK, we must pass the Timeout to the TCI for
410411
// executor-side timeout enforcement
411412
if cmd.ControlMode == controlmode.HOOK || cmd.ControlMode == controlmode.BASIC {
412-
traits := t.GetParent().GetTaskTraits()
413+
traits := role.GetTaskTraits()
413414
cmd.Timeout, err = time.ParseDuration(traits.Timeout)
414415
}
415416

@@ -579,7 +580,8 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
579580
propMap = make(controlcommands.PropertyMap)
580581
if class := t.GetTaskClass(); class != nil {
581582
if class.Control.Mode != controlmode.BASIC { // if it's NOT a basic task or hook, we template the props
582-
if t.GetParent() == nil {
583+
parent := t.GetParent()
584+
if parent == nil {
583585
err = fmt.Errorf("cannot build property map for parentless task %s (id %s)", t.name, t.taskId)
584586
return
585587
}
@@ -590,7 +592,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
590592
// First we get the full varStack from the parent role, and
591593
// consolidate it.
592594
var varStack map[string]string
593-
varStack, err = t.GetParent().ConsolidatedVarStack()
595+
varStack, err = parent.ConsolidatedVarStack()
594596
if err != nil {
595597
err = fmt.Errorf("cannot fetch variables stack for property map: %w", err)
596598
return
@@ -616,7 +618,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
616618

617619
// Finally we build the task-specific special values, and write them
618620
// into the varStack (overwriting anything).
619-
specialVarStack := t.buildSpecialVarStack(t.GetParent())
621+
specialVarStack := t.buildSpecialVarStack(parent)
620622
for k, v := range specialVarStack {
621623
varStack[k] = v
622624
}
@@ -634,7 +636,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
634636
// For FAIRMQ tasks, we append FairMQ channel configuration
635637
if class.Control.Mode == controlmode.FAIRMQ ||
636638
class.Control.Mode == controlmode.DIRECT {
637-
for _, inbCh := range channel.MergeInbound(t.GetParent().CollectInboundChannels(), class.Bind) {
639+
for _, inbCh := range channel.MergeInbound(parent.CollectInboundChannels(), class.Bind) {
638640
// We get the FairMQ-formatted propertyMap from the inbound channel spec
639641
var chanProps controlcommands.PropertyMap
640642
chanProps, err = inbCh.ToFMQMap(t.localBindMap)
@@ -647,7 +649,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
647649
propMap[k] = v
648650
}
649651
}
650-
for _, outboundCh := range channel.MergeOutbound(t.GetParent().CollectOutboundChannels(), class.Connect) {
652+
for _, outboundCh := range channel.MergeOutbound(parent.CollectOutboundChannels(), class.Connect) {
651653
// We get the FairMQ-formatted propertyMap from the outbound channel spec
652654
var chanProps controlcommands.PropertyMap
653655
chanProps, err = outboundCh.ToFMQMap(bindMap)
@@ -672,7 +674,7 @@ func (t *Task) BuildPropertyMap(bindMap channel.BindMap) (propMap controlcommand
672674
err = fields.Execute(the.ConfSvc(), t.name, varStack, objStack, nil, make(map[string]texttemplate.Template), nil)
673675
if err != nil {
674676
log.WithError(err).
675-
WithField("partition", t.GetParent().GetEnvironmentId().String()).
677+
WithField("partition", parent.GetEnvironmentId().String()).
676678
WithField("detector", detector).
677679
Error("cannot resolve templates for property map")
678680
return
@@ -718,13 +720,14 @@ func (t *Task) GetMesosCommandTarget() controlcommands.MesosCommandTarget {
718720
}
719721

720722
func (t *Task) GetProperties() map[string]string {
721-
t.mu.RLock()
722-
defer t.mu.RUnlock()
723-
724723
if t == nil {
725724
log.Warn("attempted to get properties of nil task")
726725
return make(map[string]string)
727726
}
727+
728+
t.mu.RLock()
729+
defer t.mu.RUnlock()
730+
728731
propertiesMap, err := t.properties.Flattened()
729732
if err != nil {
730733
return make(map[string]string)
@@ -733,22 +736,24 @@ func (t *Task) GetProperties() map[string]string {
733736
}
734737

735738
func (t *Task) setTaskPID(pid int) {
736-
t.mu.Lock()
737-
defer t.mu.Unlock()
738-
739739
if t == nil {
740740
return
741741
}
742+
743+
t.mu.Lock()
744+
defer t.mu.Unlock()
745+
742746
t.pid = strconv.Itoa(pid)
743747
}
744748

745749
func (t *Task) GetTaskPID() string {
746-
t.mu.RLock()
747-
defer t.mu.RUnlock()
748-
749750
if t == nil {
750751
return ""
751752
}
753+
754+
t.mu.RLock()
755+
defer t.mu.RUnlock()
756+
752757
return t.pid
753758
}
754759

@@ -767,11 +772,5 @@ func (t *Task) SetParent(parent parentRole) {
767772
}
768773

769774
func (t *Task) GetTask() *Task {
770-
if t == nil {
771-
return nil
772-
}
773-
t.mu.RLock()
774-
defer t.mu.RUnlock()
775-
776775
return t
777776
}

0 commit comments

Comments
 (0)