Skip to content

Commit 155aa37

Browse files
authored
fix(fetch-http-handler): clear the requestTimeout timer once the request settles (#2353)
requestTimeout() started a setTimeout that was never cleared. Until it fired, the pending timer kept the request race, and the response it settled with, reachable, so every completed response (including a fully read body) was retained for the full requestTimeout. requestTimeout() now returns a cancel function, which handle() calls when the race settles. Fixes #2352
1 parent 6666716 commit 155aa37

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/fetch-http-handler": patch
3+
---
4+
5+
Clear the `requestTimeout` timer once a request settles. The pending timer kept the request race, and the response it settled with, reachable until it fired, so every completed response (including a fully read body) was retained for the full `requestTimeout`.

‎packages/fetch-http-handler/src/fetch-http-handler.spec.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,39 @@ const globalFetch = global.fetch;
356356
});
357357
});
358358

359+
describe("request timeout cleanup", () => {
360+
afterEach(() => {
361+
vi.useRealTimers();
362+
});
363+
364+
it("clears the request timeout timer once the response arrives", async () => {
365+
vi.useFakeTimers();
366+
const mockResponse = {
367+
headers: {
368+
entries: () => [],
369+
},
370+
blob: async () => new Blob(),
371+
};
372+
(global as any).fetch = vi.fn().mockResolvedValue(mockResponse);
373+
const fetchHttpHandler = new FetchHttpHandler({ requestTimeout: 60_000 });
374+
375+
await fetchHttpHandler.handle({} as any, {});
376+
377+
// A pending timer keeps the settled race, and with it the response, reachable until it fires.
378+
expect(vi.getTimerCount()).toBe(0);
379+
});
380+
381+
it("clears the request timeout timer when the request fails", async () => {
382+
vi.useFakeTimers();
383+
(global as any).fetch = vi.fn().mockRejectedValue(new Error("network error"));
384+
const fetchHttpHandler = new FetchHttpHandler({ requestTimeout: 60_000 });
385+
386+
await expect(fetchHttpHandler.handle({} as any, {})).rejects.toThrow("network error");
387+
388+
expect(vi.getTimerCount()).toBe(0);
389+
});
390+
});
391+
359392
it("will throw timeout error it timeout finishes before request", async () => {
360393
const mockFetch = vi.fn(() => {
361394
return new Promise(() => {});

‎packages/fetch-http-handler/src/fetch-http-handler.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerOptions> {
142142
}
143143

144144
let removeSignalEventListener = () => {};
145+
const timeout = requestTimeoutFn(requestTimeoutInMs);
145146

146147
const fetchRequest = createRequest(url, requestOptions);
147148
const raceOfPromises = [
@@ -177,7 +178,7 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerOptions> {
177178
}),
178179
};
179180
}),
180-
requestTimeoutFn(requestTimeoutInMs),
181+
timeout.promise,
181182
];
182183
if (abortSignal) {
183184
raceOfPromises.push(
@@ -198,7 +199,10 @@ export class FetchHttpHandler implements HttpHandler<FetchHttpHandlerOptions> {
198199
})
199200
);
200201
}
201-
return Promise.race(raceOfPromises).finally(removeSignalEventListener);
202+
return Promise.race(raceOfPromises).finally(() => {
203+
timeout.cancel();
204+
removeSignalEventListener();
205+
});
202206
}
203207

204208
updateHttpClientConfig(key: keyof FetchHttpHandlerOptions, value: FetchHttpHandlerOptions[typeof key]): void {
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
export function requestTimeout(timeoutInMs = 0): Promise<never> {
2-
return new Promise((resolve, reject) => {
1+
/**
2+
* Rejects with a TimeoutError after `timeoutInMs` (never, for 0).
3+
*
4+
* Call `cancel` once the request settles: a pending timer keeps the request
5+
* race, and the response it settled with, reachable until the timer fires.
6+
*
7+
* @internal
8+
*/
9+
export function requestTimeout(timeoutInMs = 0): { promise: Promise<never>; cancel: () => void } {
10+
let timeoutId: ReturnType<typeof setTimeout> | undefined;
11+
const promise = new Promise<never>((resolve, reject) => {
312
if (timeoutInMs) {
4-
setTimeout(() => {
13+
timeoutId = setTimeout(() => {
514
const timeoutError = new Error(`Request did not complete within ${timeoutInMs} ms`);
615
timeoutError.name = "TimeoutError";
716
reject(timeoutError);
817
}, timeoutInMs);
918
}
1019
});
20+
return { promise, cancel: () => clearTimeout(timeoutId) };
1121
}

0 commit comments

Comments
 (0)