chore: fix race in cron close behavior (TestAgent_WriteVSCodeConfigs) #11243
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #11226
Problem
The problem is
cmdCloseWait
never closes, so there is an open agent go routine that will not close. In the unit test in Question,cron.Run()
is the only routine started. So I figured it is likely the routine left open. You can exercise the test locally withgo test -run TestAgent_WriteVSCodeConfigs --timeout=8s -count=20
. Or I wrote a new test that always exercises what I think the bug is.What is happening
The cron package we are using allows
Run()
to be called afterClose()
. This essentially allows restarting the cron daemon, which is interesting.Run()
is called on a go routine. So in our unit tests, it is possible theClose()
gets called first, thenRun()
is called. There exists a time period between these 2 lines of code:coder/agent/agentscripts/agentscripts.go
Lines 330 to 331 in 9b343c4
If
Run()
is called between there, thecmdCloseWait
will never terminate.A fix
If the
cronCtx
is cancelled, we just never callcron.Run()
. The ctx is always cancelled before the stop, so this should be pretty good. Ideally we implement this inside the cron package and hook on theirrunning
flag. However this should suffice.