From 09f7b8e88cf33a8a2ab9b8fa7cbff3fd87e496de Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 Oct 2023 20:08:52 +0300 Subject: [PATCH] fix(agent/agentscripts): track cron run and wait for cron stop (#10388) Fixes #10289 --- agent/agentscripts/agentscripts.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/agent/agentscripts/agentscripts.go b/agent/agentscripts/agentscripts.go index 98a6901ebb..c3bd525109 100644 --- a/agent/agentscripts/agentscripts.go +++ b/agent/agentscripts/agentscripts.go @@ -97,7 +97,15 @@ func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error { // StartCron starts the cron scheduler. // This is done async to allow for the caller to execute scripts prior. func (r *Runner) StartCron() { - r.cron.Start() + // cron.Start() and cron.Stop() does not guarantee that the cron goroutine + // has exited by the time the `cron.Stop()` context returns, so we need to + // track it manually. + err := r.trackCommandGoroutine(func() { + r.cron.Run() + }) + if err != nil { + r.Logger.Warn(context.Background(), "start cron failed", slog.Error(err)) + } } // Execute runs a set of scripts according to a filter. @@ -254,7 +262,7 @@ func (r *Runner) Close() error { } close(r.closed) r.cronCtxCancel() - r.cron.Stop() + <-r.cron.Stop().Done() r.cmdCloseWait.Wait() return nil }