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
Provenance
- Commit
e673eaa- Tree
0dde55f- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
5791b66
2 files changed+38 added−9 removed
modified:source/core/Ky.ts+5 added-9 removed
60 unmodified lines61 response = modifiedResponse;62 }63 64 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 lines109 // 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 = [];113 114 if (originalRequest && !originalRequest.bodyUsed) {117- cleanupPromises.push(originalRequest.body?.cancel());115+ void originalRequest.body?.cancel();116 }117 118 if (!ky.request.bodyUsed) {121- cleanupPromises.push(ky.request.body?.cancel());119+ void ky.request.body?.cancel();120 }123-124- await Promise.all(cleanupPromises);121 }) as ResponsePromise;122 123 for (const [type, mimeType] of Object.entries(responseTypes) as ObjectEntries<typeof responseTypes>) {124 // Only expose `.bytes()` when the environment implements it.371 unmodified linesmodified:test/hooks.ts+33 added
976 unmodified lines977 978 await server.close();979 });980 981+test('afterResponse hook forced retry does not await cancellation when hook clones response', async t => {982+ t.timeout(1000);983+984+ let requestCount = 0;985+986+ const customFetch = async () => {987+ requestCount++;988+989+ if (requestCount === 1) {990+ return new Response('unauthorized', {status: 401});991+ }992+993+ return new Response('ok');994+ };995+996+ 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();1009+1010+ t.is(result, 'ok');1011+ t.is(requestCount, 2);1012+});1013+1014 test('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