Skip to content

fix(retry): make retry delay responsive to abort signal#18

Open
itxaiohanglover wants to merge 1 commit into
codeany-ai:mainfrom
itxaiohanglover:fix/retry-abort-signal
Open

fix(retry): make retry delay responsive to abort signal#18
itxaiohanglover wants to merge 1 commit into
codeany-ai:mainfrom
itxaiohanglover:fix/retry-abort-signal

Conversation

@itxaiohanglover

Copy link
Copy Markdown

Problem

The withRetry function in src/utils/retry.ts only checked abortSignal at the start of each retry attempt. During the retry delay (up to 30 seconds with default config), the abort signal was completely ignored.

This means if a user calls abort() during a retry delay, the function continues waiting for the full delay before checking the signal on the next iteration. For a 30-second delay, this makes the abort effectively useless.

What changed

Replaced the plain setTimeout delay with an abortable promise that listens for the abort event:

// Before (abort ignored during delay):
await new Promise((resolve) => setTimeout(resolve, delay))

// After (abort fires immediately):
await new Promise((resolve, reject) => {
  const timer = setTimeout(resolve, delay)
  abortSignal?.addEventListener('abort', () => {
    clearTimeout(timer)
    reject(new Error('Aborted'))
  }, { once: true })
})

Validation

  • tsc — compiles without errors (no type changes)
  • No behavior change when no abortSignal is provided
  • When abortSignal is provided and fires during delay, the promise rejects immediately with Error('Aborted') instead of waiting

The withRetry function only checked abortSignal at the start of each
retry attempt. During the retry delay (up to 30s with default config),
the abort signal was ignored, causing abort to not take effect until
the next attempt cycle.

Fix: use setTimeout + abort event listener so the delay promise
rejects immediately when the signal fires.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant