Skip to content
Myr

Fix hooks receiving an aborted `signal` after retrying a timeout

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

The abort controller was only replaced right before the next fetch, so `beforeRetry` ran with the signal that the timeout had already aborted. The replacement also bypassed the request assignment helper, leaving the cached normalized options pointing at the old signal for `afterResponse` and `beforeError`. The reset now happens before the `beforeRetry` hooks and goes through the assignment helper, so every hook sees the signal that the retry actually uses.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 015ef95 · Browse files · 2 files+90 added−8 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+90 added−8 removed

  • tests:+83 added−0 removed
  • other:+7 added−8 removed

2 files+90 added−8 removed

modified:source/core/Ky.ts+7 added-8 removed
1043 unmodified lines
1044 await delay(retryDelay, delayOptions);10451046 this.#throwIfTotalTimeoutExhausted();10471048 // Reset abortController if it was aborted (happens on timeout retry) so hooks and the retried request get a fresh signal1049 if (this.#abortController?.signal.aborted) {1050 this.#abortController = new globalThis.AbortController();1051 this.#options.signal = this.#createManagedSignal();1052 this.#assignRequest(new globalThis.Request(this.request, {signal: this.#options.signal}));1053 }10541055 // Apply custom request from forced retry before beforeRetry hooks1056 // Ensure the custom request has the correct managed signal for timeouts and user aborts1057 if (error instanceof ForceRetryError && error.customRequest) {1058 const customRequest = new globalThis.Request(error.customRequest, this.#options.signal ? {signal: this.#options.signal} : undefined);
57 unmodified lines
1116 return value;1117 }11181119 async #fetch(): Promise<Response> {1113 // Reset abortController if it was aborted (happens on timeout retry)1114 if (this.#abortController?.signal.aborted) {1115 this.#abortController = new globalThis.AbortController();1116 this.#options.signal = this.#createManagedSignal();1117 // Recreate request with new signal1118 this.request = new globalThis.Request(this.request, {signal: this.#options.signal});1119 }11201120 const nonRequestOptions = findUnknownOptions(this.#options);1121 this.#retryLimit = normalizeRetryOptions(this.#options.retry).limit;1122 const retryRequest = this.#retryLimit > 0 ? this.request.clone() : undefined;1123 const request = this.#wrapRequestWithUploadProgress(this.request, this.#options.body ?? undefined);
95 unmodified lines
modified:test/hooks.ts+83 added
1405 unmodified lines
1406 t.is(thrownError, 'oops');1407 t.false(beforeErrorHookCalled);1408});14091410test('hooks receive a fresh signal when retrying after a timeout', async t => {1411 let requestCount = 0;1412 const observedAbortStates: boolean[] = [];14131414 const response = await ky('https://example.com', {1415 timeout: 50,1416 retry: {1417 limit: 1,1418 retryOnTimeout: true,1419 delay: () => 0,1420 },1421 async fetch(request) {1422 requestCount++;1423 if (requestCount === 1) {1424 await new Promise<void>(resolve => {1425 (request as Request).signal.addEventListener('abort', () => {1426 resolve();1427 }, {once: true});1428 });1429 throw new DOMException('aborted', 'AbortError');1430 }14311432 return new Response('ok');1433 },1434 hooks: {1435 beforeRetry: [1436 ({request, options}) => {1437 observedAbortStates.push(request.signal.aborted, options.signal!.aborted);1438 },1439 ],1440 afterResponse: [1441 ({request, options}) => {1442 observedAbortStates.push(request.signal.aborted, options.signal!.aborted);1443 },1444 ],1445 },1446 });14471448 t.is(await response.text(), 'ok');1449 t.deepEqual(observedAbortStates, [false, false, false, false]);1450});14511452test('beforeError receives a fresh signal when a retry after a timeout fails', async t => {1453 let requestCount = 0;1454 let observedAbortState: boolean | undefined;14551456 await t.throwsAsync(1457 ky('https://example.com', {1458 timeout: 50,1459 retry: {1460 limit: 1,1461 retryOnTimeout: true,1462 delay: () => 0,1463 },1464 async fetch(request) {1465 requestCount++;1466 if (requestCount === 1) {1467 await new Promise<void>(resolve => {1468 (request as Request).signal.addEventListener('abort', () => {1469 resolve();1470 }, {once: true});1471 });1472 throw new DOMException('aborted', 'AbortError');1473 }14741475 return new Response('error', {status: 500});1476 },1477 hooks: {1478 beforeError: [1479 ({error, options}) => {1480 observedAbortState = options.signal!.aborted;1481 return error;1482 },1483 ],1484 },1485 }),1486 {name: 'HTTPError'},1487 );14881489 t.is(requestCount, 2);1490 t.false(observedAbortState);1491});14921493test('beforeError runs when beforeRetry rethrows TimeoutError', async t => {1494 let beforeErrorHookCalled = false;1495 let beforeRetryHookCalled = false;1496
4067 unmodified lines