Skip to content

Commit 205eb29

Browse files
fix: stop reading closed channel for /watch devcontainers endpoint (#19373)
Fixes #19372 We increase the read limit to 4MiB (we use this limit elsewhere). We also make sure to stop sending messages when `containersCh` becomes closed.
1 parent a9f607a commit 205eb29

File tree

5 files changed

+251
-151
lines changed

5 files changed

+251
-151
lines changed

agent/agentcontainers/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,11 @@ func (api *API) broadcastUpdatesLocked() {
763763
func (api *API) watchContainers(rw http.ResponseWriter, r *http.Request) {
764764
ctx := r.Context()
765765

766-
conn, err := websocket.Accept(rw, r, nil)
766+
conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{
767+
// We want `NoContextTakeover` compression to balance improving
768+
// bandwidth cost/latency with minimal memory usage overhead.
769+
CompressionMode: websocket.CompressionNoContextTakeover,
770+
})
767771
if err != nil {
768772
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
769773
Message: "Failed to upgrade connection to websocket.",

coderd/workspaceagents.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,11 @@ func (api *API) watchWorkspaceAgentContainers(rw http.ResponseWriter, r *http.Re
896896
case <-ctx.Done():
897897
return
898898

899-
case containers := <-containersCh:
899+
case containers, ok := <-containersCh:
900+
if !ok {
901+
return
902+
}
903+
900904
if err := encoder.Encode(containers); err != nil {
901905
api.Logger.Error(ctx, "encode containers", slog.Error(err))
902906
return

0 commit comments

Comments
 (0)