@@ -815,8 +815,9 @@ func (api *API) workspaceAgentListeningPorts(rw http.ResponseWriter, r *http.Req
815815// @Router /workspaceagents/{workspaceagent}/containers/watch [get]
816816func (api * API )watchWorkspaceAgentContainers (rw http.ResponseWriter ,r * http.Request ) {
817817var (
818- ctx = r .Context ()
818+ ctx , cancelCtx = context . WithCancel ( r .Context () )
819819workspaceAgent = httpmw .WorkspaceAgentParam (r )
820+ logger = api .Logger .Named ("agent_container_watcher" ).With (slog .F ("agent_id" ,workspaceAgent .ID ))
820821)
821822
822823// 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
857858}
858859defer release ()
859860
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 )
862862if err != nil {
863863httpapi .Write (ctx ,rw ,http .StatusInternalServerError , codersdk.Response {
864864Message :"Internal error watching agent's containers." ,
@@ -881,11 +881,11 @@ func (api *API) watchWorkspaceAgentContainers(rw http.ResponseWriter, r *http.Re
881881// close frames.
882882_ = conn .CloseRead (context .Background ())
883883
884+ go httpapi .HeartbeatClose (ctx ,logger ,cancelCtx ,conn )
885+
884886ctx ,wsNetConn := codersdk .WebsocketNetConn (ctx ,conn ,websocket .MessageText )
885887defer wsNetConn .Close ()
886888
887- go httpapi .Heartbeat (ctx ,conn )
888-
889889encoder := json .NewEncoder (wsNetConn )
890890
891891for {