Skip to content

Commit 0a8da3b

Browse files
Monkey-wuskysteipete
authored andcommitted
fix(docs): cancel non-OK search response body before throwing
When the Cloudflare docs search API returns a non-OK status, fetchDocsSearch threw without cancelling the response body. This could leave the underlying connection open until GC. Cancel the body before throwing, matching the pattern established in reef guard adapters (#109196) and fetchOpenRouterModels.
1 parent f977649 commit 0a8da3b

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎src/commands/docs.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,25 @@ describe("docsSearchCommand", () => {
6565
expect(init).toMatchObject({ headers: { Accept: "application/json" } });
6666
});
6767

68-
it("fails loudly when the Cloudflare docs search API fails", async () => {
69-
fetchMock.mockResolvedValueOnce(new Response("nope", { status: 503 }));
68+
it("cancels non-OK docs search response bodies and fails loudly", async () => {
69+
let cancelled = false;
70+
const response = new Response(
71+
new ReadableStream({
72+
start(controller) {
73+
controller.enqueue(new TextEncoder().encode("unavailable"));
74+
},
75+
cancel() {
76+
cancelled = true;
77+
},
78+
}),
79+
{ status: 503 },
80+
);
81+
fetchMock.mockResolvedValueOnce(response);
7082
const runtime = makeRuntime();
7183

7284
await docsSearchCommand(["browser", "existing-session"], runtime);
7385

86+
expect(cancelled).toBe(true);
7487
expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("HTTP 503"));
7588
expect(runtime.exit).toHaveBeenCalledWith(1);
7689
});

‎src/commands/docs.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async function fetchDocsSearch(query: string): Promise<DocResult[]> {
7575
signal: controller.signal,
7676
});
7777
if (!response.ok) {
78+
await response.body?.cancel().catch(() => undefined);
7879
throw new Error(`HTTP ${response.status}`);
7980
}
8081
const bytes = await readResponseWithLimit(response, DOCS_SEARCH_RESPONSE_MAX_BYTES, {

0 commit comments

Comments
 (0)