Throw abort error if signal is pre-aborted

Respect pre-aborted AbortSignals immediately instead of attempting
the operation first. This ensures callers that abort before invoking
the retry function receive the expected rejection without unnecessary
execution.
This commit is contained in:
Jinjing
2026-09-02 21:18:08 -07:00
parent 8c1b69cf8f
commit 4bd0ee6ee2
2 changed files with 13 additions and 0 deletions
+10
View File
@@ -45,6 +45,16 @@ describe('git index lock retry', () => {
expect(run).toHaveBeenCalledTimes(2)
})
it('rejects without running when the signal is already aborted', async () => {
vi.useFakeTimers()
const run = vi.fn<() => Promise<string>>().mockResolvedValue('ok')
await expect(runWithGitIndexLockRetry(run, AbortSignal.abort())).rejects.toMatchObject({
name: 'AbortError'
})
expect(run).not.toHaveBeenCalled()
})
it('cancels a pending retry when the caller aborts', async () => {
vi.useFakeTimers()
const controller = new AbortController()
+3
View File
@@ -51,6 +51,9 @@ export async function runWithGitIndexLockRetry<T>(
signal?: AbortSignal
): Promise<T> {
for (let attempt = 0; ; attempt += 1) {
if (signal?.aborted) {
throw createAbortError()
}
try {
return await run()
} catch (error) {