Skip to content

Commit

Permalink
OCTRL-920 [core] invalidating auto-stop lets the goroutine exit
Browse files Browse the repository at this point in the history
The goroutine for performing would never exit in case that the auto-stop would be invalidated. Consequently, it would be stuck indefinitely, surpassing the environment's lifetime.
I don't think this was causing any trouble except of just leaking goroutines and obfuscating the state of the application.
We also invalidate the auto-stop in case of going to ERROR, so we do not trigger a spurious transition attempt when it's pointless to do so.
  • Loading branch information
knopers8 committed Sep 13, 2024
1 parent 4fdeae0 commit bda8717
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions core/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ type Environment struct {
callsPendingAwait map[string] /*await expression, trigger only*/ callable.CallsMap
currentTransition string

autoStopTimer *time.Timer
autoStopTimer *time.Timer
autoStopCancelFcn context.CancelFunc
}

func (env *Environment) NotifyEvent(e event.DeviceEvent) {
Expand Down Expand Up @@ -593,6 +594,7 @@ func newEnvironment(userVars map[string]string, newId uid.ID) (env *Environment,
log.WithField("partition", envId.String()).
Debug("O2 End Completion time already set before after_GO_ERROR")
}
env.invalidateAutoStopTransition()
}

errHooks = errors.Join(errHooks, env.handleHooksWithPositiveWeights(env.Workflow(), trigger))
Expand Down Expand Up @@ -1403,6 +1405,8 @@ func (env *Environment) scheduleAutoStopTransition() (scheduled bool, expected t
}

env.autoStopTimer = time.NewTimer(autoStopDuration)
ctx, cancel := context.WithCancel(context.Background())
env.autoStopCancelFcn = cancel
go func() {
select {
case <-env.autoStopTimer.C:
Expand All @@ -1423,6 +1427,10 @@ func (env *Environment) scheduleAutoStopTransition() (scheduled bool, expected t
}
return
}
case <-ctx.Done():
log.WithField("partition", env.id).
WithField("run", env.currentRunNumber).
Debugf("Scheduled auto stop transition was cancelled")
}
}()

Expand All @@ -1437,7 +1445,10 @@ func (env *Environment) scheduleAutoStopTransition() (scheduled bool, expected t

func (env *Environment) invalidateAutoStopTransition() {
// Only try to stop an initialized timer
if env.autoStopTimer != nil {
env.autoStopTimer.Stop()
if env.autoStopTimer == nil {
return
}
if env.autoStopTimer.Stop() && env.autoStopCancelFcn != nil {
env.autoStopCancelFcn()
}
}

0 comments on commit bda8717

Please sign in to comment.