Skip to content

Commit 8ade1b6

Browse files
committed
Only retry for the specific thing raising EBUSY
Fix: #187
1 parent 5d5e87c commit 8ade1b6

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/fs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import fs from 'fs'
44

5-
export type FsError = Error & { code?: string }
5+
export type FsError = Error & {
6+
code?: string
7+
path?: string
8+
}
69

710
// sync ones just take the sync version from node
811
export {

src/retry-busy.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const retryBusy = (fn: (path: string) => Promise<any>) => {
2424
return await fn(path)
2525
} catch (er) {
2626
const fer = er as FsError
27-
if (fer?.code && codes.has(fer.code)) {
27+
if (fer?.path === path && fer?.code && codes.has(fer.code)) {
2828
backoff = Math.ceil(backoff * rate)
2929
total = backoff + total
3030
if (total < mbo) {
@@ -57,7 +57,12 @@ export const retryBusySync = (fn: (path: string) => any) => {
5757
return fn(path)
5858
} catch (er) {
5959
const fer = er as FsError
60-
if (fer?.code && codes.has(fer.code) && retries < max) {
60+
if (
61+
fer?.path === path &&
62+
fer?.code &&
63+
codes.has(fer.code) &&
64+
retries < max
65+
) {
6166
retries++
6267
continue
6368
}

test/retry-busy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ t.test('retry when known error code thrown', t => {
5151
thrown = true
5252
t.equal(calls, 0, 'first call')
5353
calls++
54-
throw Object.assign(new Error(code), { code })
54+
throw Object.assign(new Error(code), { path: a, code })
5555
} else {
5656
t.equal(calls, 1, 'second call')
5757
calls++
@@ -83,10 +83,10 @@ t.test('retry and eventually give up', t => {
8383
t.equal(a, arg, 'got first argument')
8484
t.equal(b, undefined, 'did not get another argument')
8585
calls++
86-
throw Object.assign(new Error(code), { code })
86+
throw Object.assign(new Error(code), { path: a, code })
8787
}
8888
const rBS = retryBusySync(method)
89-
t.throws(() => rBS(arg, opt), { code })
89+
t.throws(() => rBS(arg, opt), { path: arg, code })
9090
t.equal(calls, 3)
9191
calls = 0
9292
const rB = retryBusy(method)
@@ -101,7 +101,7 @@ t.test('throw unknown error gives up right away', async t => {
101101
const method = (a, b) => {
102102
t.equal(a, arg, 'got first argument')
103103
t.equal(b, undefined, 'did not get another argument')
104-
throw Object.assign(new Error('nope'), { code: 'nope' })
104+
throw Object.assign(new Error('nope'), { path: a, code: 'nope' })
105105
}
106106
const rBS = retryBusySync(method)
107107
t.throws(() => rBS(arg, opt), { code: 'nope' })

0 commit comments

Comments
 (0)