From 4caaa2a8e84c55d9c4c0adab516e374a510816ac Mon Sep 17 00:00:00 2001 From: anthony bushong Date: Thu, 25 Sep 2025 10:33:00 -0700 Subject: [PATCH] fix(core): ensure retry sets defaults for nullish values passed into options (#9540) Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com> --- packages/core/src/utils/retry.test.ts | 17 +++++++++++++++++ packages/core/src/utils/retry.ts | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/retry.test.ts b/packages/core/src/utils/retry.test.ts index 78c4100edb..d310327827 100644 --- a/packages/core/src/utils/retry.test.ts +++ b/packages/core/src/utils/retry.test.ts @@ -116,6 +116,23 @@ describe('retryWithBackoff', () => { expect(mockFn).toHaveBeenCalledTimes(5); }); + it('should default to 5 maxAttempts if options.maxAttempts is undefined', async () => { + // This function will fail more than 5 times to ensure all retries are used. + const mockFn = createFailingFunction(10); + + const promise = retryWithBackoff(mockFn, { maxAttempts: undefined }); + + // Expect it to fail with the error from the 5th attempt. + // eslint-disable-next-line vitest/valid-expect + const assertionPromise = expect(promise).rejects.toThrow( + 'Simulated error attempt 5', + ); + await vi.runAllTimersAsync(); + await assertionPromise; + + expect(mockFn).toHaveBeenCalledTimes(5); + }); + it('should not retry if shouldRetry returns false', async () => { const mockFn = vi.fn(async () => { throw new NonRetryableError('Non-retryable error'); diff --git a/packages/core/src/utils/retry.ts b/packages/core/src/utils/retry.ts index 50f113967a..0e88fd74e3 100644 --- a/packages/core/src/utils/retry.ts +++ b/packages/core/src/utils/retry.ts @@ -78,6 +78,10 @@ export async function retryWithBackoff( throw new Error('maxAttempts must be a positive number.'); } + const cleanOptions = options + ? Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null)) + : {}; + const { maxAttempts, initialDelayMs, @@ -87,7 +91,7 @@ export async function retryWithBackoff( shouldRetry, } = { ...DEFAULT_RETRY_OPTIONS, - ...options, + ...cleanOptions, }; let attempt = 0;