Skip to content

chore: fix race in cron close behavior (TestAgent_WriteVSCodeConfigs) #11243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add comment to protect order
  • Loading branch information
Emyrk committed Dec 15, 2023
commit 687f7f63fc9631df95aeb2d1331c1ac22fdfd41f
7 changes: 4 additions & 3 deletions agent/agentscripts/agentscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func (r *Runner) StartCron() {
// So if we are closed, we just return, and skip the Run() entirely.
select {
case <-r.cronCtx.Done():
// The cronCtx is cancelled before cron.Close() happens. So if the ctx is
// cancelled, then Close() will be called, or it is about to be called.
// So do nothing!
default:
// There is still a chance the Close() happens right here before we'
// r.cron.Run(). Fixing this requires modifying the cron package
// to add a hook for when the cron "running" flag is set.
r.cron.Run()
}
})
Expand Down Expand Up @@ -326,6 +326,7 @@ func (r *Runner) Close() error {
return nil
}
close(r.closed)
// Must cancel the cron ctx BEFORE stopping the cron.
r.cronCtxCancel()
<-r.cron.Stop().Done()
r.cmdCloseWait.Wait()
Expand Down
4 changes: 2 additions & 2 deletions agent/agentscripts/agentscripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func TestTimeout(t *testing.T) {
require.ErrorIs(t, runner.Execute(context.Background(), nil), agentscripts.ErrTimeout)
}

// TestCronClose exists because StartCront() can happen after Close(),
// so the cron go routine is not exited.
// TestCronClose exists because cron.Run() can happen after cron.Close().
// If this happens, there used to be a deadlock.
func TestCronClose(t *testing.T) {
t.Parallel()
runner := agentscripts.New(agentscripts.Options{})
Expand Down