Skip to content

Commit 09f7b8e

Browse files
authored
fix(agent/agentscripts): track cron run and wait for cron stop (#10388)
Fixes #10289
1 parent 1a2aea3 commit 09f7b8e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

agent/agentscripts/agentscripts.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,15 @@ func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error {
9797
// StartCron starts the cron scheduler.
9898
// This is done async to allow for the caller to execute scripts prior.
9999
func (r *Runner) StartCron() {
100-
r.cron.Start()
100+
// cron.Start() and cron.Stop() does not guarantee that the cron goroutine
101+
// has exited by the time the `cron.Stop()` context returns, so we need to
102+
// track it manually.
103+
err := r.trackCommandGoroutine(func() {
104+
r.cron.Run()
105+
})
106+
if err != nil {
107+
r.Logger.Warn(context.Background(), "start cron failed", slog.Error(err))
108+
}
101109
}
102110

103111
// Execute runs a set of scripts according to a filter.
@@ -254,7 +262,7 @@ func (r *Runner) Close() error {
254262
}
255263
close(r.closed)
256264
r.cronCtxCancel()
257-
r.cron.Stop()
265+
<-r.cron.Stop().Done()
258266
r.cmdCloseWait.Wait()
259267
return nil
260268
}

0 commit comments

Comments
 (0)