Skip to content

Commit 089f960

Browse files
fix(site): only attempt to watch containers when agent connected (#18873)
This PR ensures we do not attempt to call `containers/watch` on the agent _before_ it is connected.
1 parent 4354633 commit 089f960

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

site/src/modules/resources/useAgentContainers.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,22 @@ describe("useAgentContainers", () => {
193193
displayErrorSpy.mockRestore();
194194
watchAgentContainersSpy.mockRestore();
195195
});
196+
197+
it("does not establish WebSocket connection when agent is not connected", () => {
198+
const watchAgentContainersSpy = jest.spyOn(API, "watchAgentContainers");
199+
200+
const disconnectedAgent = {
201+
...MockWorkspaceAgent,
202+
status: "disconnected" as const,
203+
};
204+
205+
const { result } = renderHook(() => useAgentContainers(disconnectedAgent), {
206+
wrapper: createWrapper(),
207+
});
208+
209+
expect(watchAgentContainersSpy).not.toHaveBeenCalled();
210+
expect(result.current).toBeUndefined();
211+
212+
watchAgentContainersSpy.mockRestore();
213+
});
196214
});

site/src/modules/resources/useAgentContainers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function useAgentContainers(
3131
);
3232

3333
useEffect(() => {
34+
if (agent.status !== "connected") {
35+
return;
36+
}
37+
3438
const socket = watchAgentContainers(agent.id);
3539

3640
socket.addEventListener("message", (event) => {
@@ -53,7 +57,7 @@ export function useAgentContainers(
5357
});
5458

5559
return () => socket.close();
56-
}, [agent.id, updateDevcontainersCache]);
60+
}, [agent.id, agent.status, updateDevcontainersCache]);
5761

5862
return devcontainers;
5963
}

0 commit comments

Comments
 (0)