Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae5dd1e

Browse files
chore: ensure we close and init
1 parent 996d440 commit ae5dd1e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

agent/agentcontainers/api.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
265265
api := &API{
266266
ctx: ctx,
267267
cancel: cancel,
268-
watcherDone: make(chan struct{}),
269-
updaterDone: make(chan struct{}),
270268
initialUpdateDone: make(chan struct{}),
271269
updateTrigger: make(chan chan error),
272270
updateInterval: defaultUpdateInterval,
@@ -326,6 +324,9 @@ func (api *API) Init(opts ...Option) {
326324
opt(api)
327325
}
328326

327+
api.watcherDone = make(chan struct{})
328+
api.updaterDone = make(chan struct{})
329+
329330
go api.watcherLoop()
330331
go api.updaterLoop()
331332
}
@@ -1640,8 +1641,12 @@ func (api *API) Close() error {
16401641
err := api.watcher.Close()
16411642

16421643
// Wait for loops to finish.
1643-
<-api.watcherDone
1644-
<-api.updaterDone
1644+
if api.watcherDone != nil {
1645+
<-api.watcherDone
1646+
}
1647+
if api.updaterDone != nil {
1648+
<-api.updaterDone
1649+
}
16451650

16461651
// Wait for all async tasks to complete.
16471652
api.asyncWg.Wait()

agent/agentcontainers/api_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ func TestAPI(t *testing.T) {
437437
agentcontainers.WithContainerCLI(mLister),
438438
agentcontainers.WithContainerLabelIncludeFilter("this.label.does.not.exist.ignore.devcontainers", "true"),
439439
)
440+
api.Init()
440441
defer api.Close()
441442
r.Mount("/", api.Routes())
442443

@@ -614,6 +615,7 @@ func TestAPI(t *testing.T) {
614615
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI),
615616
agentcontainers.WithWatcher(watcher.NewNoop()),
616617
)
618+
api.Init()
617619
defer api.Close()
618620
r.Mount("/", api.Routes())
619621

@@ -1027,6 +1029,7 @@ func TestAPI(t *testing.T) {
10271029
}
10281030

10291031
api := agentcontainers.NewAPI(logger, apiOptions...)
1032+
api.Init()
10301033
defer api.Close()
10311034

10321035
r.Mount("/", api.Routes())
@@ -1111,6 +1114,7 @@ func TestAPI(t *testing.T) {
11111114
[]codersdk.WorkspaceAgentScript{{LogSourceID: uuid.New(), ID: dc.ID}},
11121115
),
11131116
)
1117+
api.Init()
11141118
defer api.Close()
11151119

11161120
// Make sure the ticker function has been registered
@@ -1206,6 +1210,7 @@ func TestAPI(t *testing.T) {
12061210
agentcontainers.WithWatcher(fWatcher),
12071211
agentcontainers.WithClock(mClock),
12081212
)
1213+
api.Init()
12091214
defer api.Close()
12101215

12111216
r := chi.NewRouter()
@@ -1358,6 +1363,7 @@ func TestAPI(t *testing.T) {
13581363
agentcontainers.WithDevcontainerCLI(fakeDCCLI),
13591364
agentcontainers.WithManifestInfo("test-user", "test-workspace"),
13601365
)
1366+
api.Init()
13611367
apiClose := func() {
13621368
closeOnce.Do(func() {
13631369
// Close before api.Close() defer to avoid deadlock after test.
@@ -1578,6 +1584,7 @@ func TestAPI(t *testing.T) {
15781584
agentcontainers.WithSubAgentClient(fakeSAC),
15791585
agentcontainers.WithDevcontainerCLI(&fakeDevcontainerCLI{}),
15801586
)
1587+
api.Init()
15811588
defer api.Close()
15821589

15831590
tickerTrap.MustWait(ctx).MustRelease(ctx)
@@ -1899,6 +1906,7 @@ func TestAPI(t *testing.T) {
18991906
agentcontainers.WithSubAgentURL("test-subagent-url"),
19001907
agentcontainers.WithWatcher(watcher.NewNoop()),
19011908
)
1909+
api.Init()
19021910
defer api.Close()
19031911

19041912
// Close before api.Close() defer to avoid deadlock after test.
@@ -1991,6 +1999,7 @@ func TestAPI(t *testing.T) {
19911999
agentcontainers.WithSubAgentURL("test-subagent-url"),
19922000
agentcontainers.WithWatcher(watcher.NewNoop()),
19932001
)
2002+
api.Init()
19942003
defer api.Close()
19952004

19962005
// Close before api.Close() defer to avoid deadlock after test.
@@ -2045,6 +2054,7 @@ func TestAPI(t *testing.T) {
20452054
agentcontainers.WithExecer(fakeExec),
20462055
agentcontainers.WithCommandEnv(commandEnv),
20472056
)
2057+
api.Init()
20482058
defer api.Close()
20492059

20502060
// Call RefreshContainers directly to trigger CommandEnv usage.
@@ -2134,6 +2144,7 @@ func TestAPI(t *testing.T) {
21342144
agentcontainers.WithWatcher(fWatcher),
21352145
agentcontainers.WithClock(mClock),
21362146
)
2147+
api.Init()
21372148
defer func() {
21382149
close(fakeSAC.createErrC)
21392150
close(fakeSAC.deleteErrC)
@@ -2334,6 +2345,7 @@ func TestSubAgentCreationWithNameRetry(t *testing.T) {
23342345
agentcontainers.WithSubAgentClient(fSAC),
23352346
agentcontainers.WithWatcher(watcher.NewNoop()),
23362347
)
2348+
api.Init()
23372349
defer api.Close()
23382350

23392351
tickerTrap.MustWait(ctx).MustRelease(ctx)

0 commit comments

Comments
 (0)