Skip to content

Commit 9b343c4

Browse files
committed
Implement a *fix for cron stop() before run()
This fix still has a race condition. I do not see a clean solution without modifying the cron libary. The cron library uses a boolean to indicate running, and that boolean needs to be set to "true" before we call "Close()". Or "Close()" should prevent "Run()" from doing anything. In either case, this solves the issue for a niche unit test bug in which the test finishes, calling Close(), before there was an oppertunity to start the go routine. It probably isn't worth a lot of time investment, and this fix will suffice
1 parent 2782521 commit 9b343c4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

agent/agentscripts/agentscripts.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,18 @@ func (r *Runner) StartCron() {
129129
// has exited by the time the `cron.Stop()` context returns, so we need to
130130
// track it manually.
131131
err := r.trackCommandGoroutine(func() {
132-
r.cron.Run()
132+
// Since this is run async, in quick unit tests, it is possible the
133+
// Close() function gets called before we even start the cron.
134+
// In these cases, the Run() will never end.
135+
// So if we are closed, we just return, and skip the Run() entirely.
136+
select {
137+
case <-r.cronCtx.Done():
138+
default:
139+
// There is still a chance the Close() happens right here before we'
140+
// r.cron.Run(). Fixing this requires modifying the cron package
141+
// to add a hook for when the cron "running" flag is set.
142+
r.cron.Run()
143+
}
133144
})
134145
if err != nil {
135146
r.Logger.Warn(context.Background(), "start cron failed", slog.Error(err))

0 commit comments

Comments
 (0)