Skip to content

Commit fcec95f

Browse files
committed
fix(signal): cancel status-only response bodies
1 parent e67f8ba commit fcec95f

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎extensions/signal/src/client-container.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ describe("containerCheck", () => {
130130
expectFirstFetchCall("http://localhost:8080/v1/about", "GET");
131131
});
132132

133+
it("cancels /v1/about response bodies after simple health checks", async () => {
134+
const cancel = vi.fn(async () => undefined);
135+
mockFetch.mockResolvedValue({
136+
ok: true,
137+
status: 200,
138+
body: { cancel },
139+
});
140+
141+
await expect(containerCheck("http://localhost:8080")).resolves.toEqual({
142+
ok: true,
143+
status: 200,
144+
error: null,
145+
});
146+
expect(cancel).toHaveBeenCalledTimes(1);
147+
});
148+
133149
it("returns ok:false when /v1/about returns 404", async () => {
134150
mockFetch.mockResolvedValue({
135151
ok: false,
@@ -635,16 +651,19 @@ describe("containerFetchAttachment", () => {
635651
});
636652

637653
it("returns null on non-ok response", async () => {
654+
const cancel = vi.fn(async () => undefined);
638655
mockFetch.mockResolvedValue({
639656
ok: false,
640657
status: 404,
658+
body: { cancel },
641659
});
642660

643661
const result = await containerFetchAttachment("attachment-123", {
644662
baseUrl: "http://localhost:8080",
645663
});
646664

647665
expect(result).toBeNull();
666+
expect(cancel).toHaveBeenCalledTimes(1);
648667
});
649668

650669
it("encodes attachment ID in URL", async () => {

‎extensions/signal/src/client-container.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ async function readCappedResponseBuffer(res: Response, maxResponseBytes: number)
111111
});
112112
}
113113

114+
async function releaseUnreadResponseBody(res: Response | undefined): Promise<void> {
115+
if (res?.bodyUsed !== true) {
116+
await res?.body?.cancel().catch(() => undefined);
117+
}
118+
}
119+
114120
/**
115121
* Check if bbernhard container REST API is available.
116122
*/
@@ -120,8 +126,9 @@ export async function containerCheck(
120126
account?: string,
121127
): Promise<{ ok: boolean; status?: number | null; error?: string | null }> {
122128
const normalized = normalizeBaseUrl(baseUrl);
129+
let res: Response | undefined;
123130
try {
124-
const res = await fetchWithTimeout(`${normalized}/v1/about`, { method: "GET" }, timeoutMs);
131+
res = await fetchWithTimeout(`${normalized}/v1/about`, { method: "GET" }, timeoutMs);
125132
if (!res.ok) {
126133
return { ok: false, status: res.status, error: `HTTP ${res.status}` };
127134
}
@@ -136,6 +143,8 @@ export async function containerCheck(
136143
status: null,
137144
error: err instanceof Error ? err.message : String(err),
138145
};
146+
} finally {
147+
await releaseUnreadResponseBody(res);
139148
}
140149
}
141150

@@ -253,14 +262,19 @@ export async function containerFetchAttachment(
253262
): Promise<Buffer | null> {
254263
const baseUrl = normalizeBaseUrl(opts.baseUrl);
255264
const url = `${baseUrl}/v1/attachments/${encodeURIComponent(attachmentId)}`;
265+
let res: Response | undefined;
256266

257-
const res = await fetchWithTimeout(url, { method: "GET" }, opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
267+
try {
268+
res = await fetchWithTimeout(url, { method: "GET" }, opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
258269

259-
if (!res.ok) {
260-
return null;
261-
}
270+
if (!res.ok) {
271+
return null;
272+
}
262273

263-
return readCappedResponseBuffer(res, normalizeMaxResponseBytes(opts.maxResponseBytes));
274+
return await readCappedResponseBuffer(res, normalizeMaxResponseBytes(opts.maxResponseBytes));
275+
} finally {
276+
await releaseUnreadResponseBody(res);
277+
}
264278
}
265279

266280
/**

0 commit comments

Comments
 (0)