Skip to content
Myr

Avoid awaiting response cancel on forced retry (#806)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent 5791b66 · Browse files · 2 files+38 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−9 removed

  • tests:+33 added−0 removed
  • other:+5 added−9 removed

2 files+38 added−9 removed

modified:source/core/Ky.ts+5 added-9 removed
60 unmodified lines
61 response = modifiedResponse;62 }6364 if (modifiedResponse instanceof RetryMarker) {65 // Cancel both the cloned response passed to the hook and the current response66 // to prevent resource leaks (especially important in Deno/Bun)67 // eslint-disable-next-line no-await-in-loop68 await Promise.all([65 // Cancel both the cloned response passed to the hook and the current response to prevent resource leaks (especially important in Deno/Bun).66 // Do not await cancellation since hooks can clone the response, leaving extra tee branches that keep cancel promises pending per the Streams spec.67 void Promise.allSettled([68 clonedResponse.body?.cancel(),69 response.body?.cancel(),70 ]);71 throw new ForceRetryError(modifiedResponse.options);
37 unmodified lines
109 // Method retriability is checked in #calculateRetryDelay for non-forced retries110 const result = ky.#retry(function_)111 .finally(async () => {112 const originalRequest = ky.#originalRequest;114 const cleanupPromises = [];113114 if (originalRequest && !originalRequest.bodyUsed) {117 cleanupPromises.push(originalRequest.body?.cancel());115 void originalRequest.body?.cancel();116 }117118 if (!ky.request.bodyUsed) {121 cleanupPromises.push(ky.request.body?.cancel());119 void ky.request.body?.cancel();120 }123124 await Promise.all(cleanupPromises);121 }) as ResponsePromise;122123 for (const [type, mimeType] of Object.entries(responseTypes) as ObjectEntries<typeof responseTypes>) {124 // Only expose `.bytes()` when the environment implements it.
371 unmodified lines
modified:test/hooks.ts+33 added
976 unmodified lines
977978 await server.close();979});980981test('afterResponse hook forced retry does not await cancellation when hook clones response', async t => {982 t.timeout(1000);983984 let requestCount = 0;985986 const customFetch = async () => {987 requestCount++;988989 if (requestCount === 1) {990 return new Response('unauthorized', {status: 401});991 }992993 return new Response('ok');994 };995996 const result = await ky('https://example.test', {997 fetch: customFetch,998 hooks: {999 afterResponse: [1000 (_request, _options, response) => {1001 response.clone();1002 if (response.status === 401) {1003 return ky.retry();1004 }1005 },1006 ],1007 },1008 }).text();10091010 t.is(result, 'ok');1011 t.is(requestCount, 2);1012});10131014test('afterResponse hook can force retry with custom delay', async t => {1015 let requestCount = 0;1016 const customDelay = 100;1017 const startTime = Date.now();
1101 unmodified lines