@@ -815,8 +815,9 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
815
815
// @Router /workspaceagents/{workspaceagent}/containers/watch [get]
816
816
func (api * API )watchWorkspaceAgentContainers (rw http.ResponseWriter ,r * http.Request ) {
817
817
var (
818
- ctx = r .Context ()
818
+ ctx , cancelCtx = context . WithCancel ( r .Context () )
819
819
workspaceAgent = httpmw .WorkspaceAgentParam (r )
820
+ logger = api .Logger .Named ("agent_container_watcher" ).With (slog .F ("agent_id" ,workspaceAgent .ID ))
820
821
)
821
822
822
823
// If the agent is unreachable, the request will hang. Assume that if we
@@ -857,8 +858,7 @@ func (api *API) watchWorkspaceAgentContainers(rw http.ResponseWriter, r *http.Re
857
858
}
858
859
defer release ()
859
860
860
- watcherLogger := api .Logger .Named ("agent_container_watcher" ).With (slog .F ("agent_id" ,workspaceAgent .ID ))
861
- containersCh ,closer ,err := agentConn .WatchContainers (ctx ,watcherLogger )
861
+ containersCh ,closer ,err := agentConn .WatchContainers (ctx ,logger )
862
862
if err != nil {
863
863
httpapi .Write (ctx ,rw ,http .StatusInternalServerError , codersdk.Response {
864
864
Message :"Internal error watching agent's containers." ,
@@ -881,11 +881,11 @@ func (api *API) watchWorkspaceAgentContainers(rw http.ResponseWriter, r *http.Re
881
881
// close frames.
882
882
_ = conn .CloseRead (context .Background ())
883
883
884
+ go httpapi .HeartbeatClose (ctx ,logger ,cancelCtx ,conn )
885
+
884
886
ctx ,wsNetConn := codersdk .WebsocketNetConn (ctx ,conn ,websocket .MessageText )
885
887
defer wsNetConn .Close ()
886
888
887
- go httpapi .Heartbeat (ctx ,conn )
888
-
889
889
encoder := json .NewEncoder (wsNetConn )
890
890
891
891
for {