Skip to content

Commit 9ecb6b2

Browse files
committed
switch to lockCh
1 parent 3597994 commit 9ecb6b2

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

agent/agentcontainers/api.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"path"
99
"slices"
1010
"strings"
11-
"sync"
1211
"time"
1312

1413
"github.com/go-chi/chi/v5"
@@ -38,11 +37,9 @@ type API struct {
3837

3938
// lockCh protects the below fields. We use a channel instead of a
4039
// mutex so we can handle cancellation properly.
41-
lockCh chan struct{}
42-
containers codersdk.WorkspaceAgentListContainersResponse
43-
mtime time.Time
44-
45-
devcontainersMu sync.Mutex // Protects following.
40+
lockCh chan struct{}
41+
containers codersdk.WorkspaceAgentListContainersResponse
42+
mtime time.Time
4643
devcontainerNames map[string]struct{} // Track devcontainer names to avoid duplicates.
4744
knownDevcontainers []codersdk.WorkspaceAgentDevcontainer // Track predefined and runtime-detected devcontainers.
4845
}
@@ -168,9 +165,6 @@ func (api *API) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListC
168165
api.containers = updated
169166
api.mtime = now
170167

171-
api.devcontainersMu.Lock()
172-
defer api.devcontainersMu.Unlock()
173-
174168
// Reset all known devcontainers to not running.
175169
for i := range api.knownDevcontainers {
176170
api.knownDevcontainers[i].Running = false
@@ -294,9 +288,13 @@ func (api *API) handleListDevcontainers(w http.ResponseWriter, r *http.Request)
294288
return
295289
}
296290

297-
api.devcontainersMu.Lock()
291+
select {
292+
case <-ctx.Done():
293+
return
294+
case api.lockCh <- struct{}{}:
295+
}
298296
devcontainers := slices.Clone(api.knownDevcontainers)
299-
api.devcontainersMu.Unlock()
297+
<-api.lockCh
300298

301299
slices.SortFunc(devcontainers, func(a, b codersdk.WorkspaceAgentDevcontainer) int {
302300
if cmp := strings.Compare(a.WorkspaceFolder, b.WorkspaceFolder); cmp != 0 {

0 commit comments

Comments
 (0)