Skip to content

Commit e20e0f6

Browse files
committed
OCTRL-920 [core] invalidating auto-stop lets the goroutine exit
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.
1 parent 0a11513 commit e20e0f6

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

core/environment/environment.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ type Environment struct {
8888
callsPendingAwait map[string] /*await expression, trigger only*/ callable.CallsMap
8989
currentTransition string
9090

91-
autoStopTimer *time.Timer
91+
autoStopTimer *time.Timer
92+
autoStopCancelFcn context.CancelFunc
9293
}
9394

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

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

14051407
env.autoStopTimer = time.NewTimer(autoStopDuration)
1408+
ctx, cancel := context.WithCancel(context.Background())
1409+
env.autoStopCancelFcn = cancel
14061410
go func() {
14071411
select {
14081412
case <-env.autoStopTimer.C:
@@ -1423,6 +1427,10 @@ func (env *Environment) scheduleAutoStopTransition() (scheduled bool, expected t
14231427
}
14241428
return
14251429
}
1430+
case <-ctx.Done():
1431+
log.WithField("partition", env.id).
1432+
WithField("run", env.currentRunNumber).
1433+
Debugf("Scheduled auto stop transition was cancelled")
14261434
}
14271435
}()
14281436

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

14381446
func (env *Environment) invalidateAutoStopTransition() {
14391447
// Only try to stop an initialized timer
1440-
if env.autoStopTimer != nil {
1441-
env.autoStopTimer.Stop()
1448+
if env.autoStopTimer == nil {
1449+
return
1450+
}
1451+
if env.autoStopTimer.Stop() && env.autoStopCancelFcn != nil {
1452+
env.autoStopCancelFcn()
14421453
}
14431454
}

0 commit comments

Comments
 (0)