fix(plugin): close timed out kubernetes exec sockets

This commit is contained in:
Dotta
2026-05-13 15:06:26 -05:00
committed by Chris Farhood
parent 1eccb71213
commit 9035b70aa9
2 changed files with 12 additions and 2 deletions
@@ -155,7 +155,15 @@ export async function execInPod(
websocketPromise
.then((webSocket) => {
ws = webSocket as WebSocketLike;
if (!settled && stdinStream && stdinPayload) {
if (settled) {
try {
ws.close();
} catch {
// Ignore best-effort close failures.
}
return;
}
if (stdinStream && stdinPayload) {
stdinStream.end(stdinPayload);
}
ws.on("close", (code: number, reason: Buffer) => {
@@ -116,6 +116,7 @@ describe("execInPod", () => {
let resolveWebsocket: ((ws: EventEmitter) => void) | undefined;
let observedStdin = "";
let observedStdinFinished = false;
const ws = Object.assign(new EventEmitter(), { close: vi.fn() });
execMock.mockImplementation((_namespace, _pod, _container, _command, _stdout, _stderr, stdin) => {
stdin?.on("data", (chunk: Buffer) => {
@@ -132,9 +133,10 @@ describe("execInPod", () => {
const result = await execInPod({} as never, "ns", "pod-1", "agent", ["base64", "-d"], "abc", 5);
expect(result.timedOut).toBe(true);
resolveWebsocket?.(new EventEmitter());
resolveWebsocket?.(ws);
await Promise.resolve();
expect(ws.close).toHaveBeenCalled();
expect(observedStdin).toBe("");
expect(observedStdinFinished).toBe(false);
});