Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2500,10 +2500,17 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {

const ac = new AbortController()
const { signal } = options
// when/if our AC signals, then stop listening to theirs.
signal?.addEventListener('abort', () => ac.abort(signal.reason), {
signal: ac.signal,
})
// pass along an abort from their signal. This has to be removed when
// the fetch settles, or a long-lived signal would keep every handler
// (and everything in this scope) around until it aborts.
const onSignalAbort = () => ac.abort(signal?.reason)
const stopListening = () =>
signal?.removeEventListener('abort', onSignalAbort)
if (signal?.aborted) {
ac.abort(signal.reason)
} else {
signal?.addEventListener('abort', onSignalAbort)
}

const fetchOpts = {
signal: ac.signal,
Expand All @@ -2512,6 +2519,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
}

const cb = (v: V | undefined, updateCache = false): V | undefined => {
stopListening()
const { aborted } = ac.signal
const ignoreAbort = options.ignoreFetchAbort && v !== undefined
const proceed =
Expand Down Expand Up @@ -2551,6 +2559,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
}

const eb = (er: unknown) => {
stopListening()
if (options.status) {
options.status.fetchRejected = true
options.status.fetchError = er as Error
Expand Down Expand Up @@ -2599,15 +2608,18 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
// ignored, we go until we finish, regardless.
// defer check until we are actually aborting,
// so fetchMethod can override.
ac.signal.addEventListener('abort', () => {
const onAbort = () => {
if (!options.ignoreFetchAbort || options.allowStaleOnFetchAbort) {
res(undefined)
// when it eventually resolves, update the cache.
if (options.allowStaleOnFetchAbort) {
res = v => cb(v, true)
}
}
})
}
ac.signal.addEventListener('abort', onAbort)
// the event does not fire if their signal was already aborted.
if (ac.signal.aborted) onAbort()
if (fmp && fmp instanceof Promise) {
fmp.then(v => res(v === undefined ? undefined : v), rej)
} else if (fmp !== undefined) {
Expand Down
168 changes: 168 additions & 0 deletions test/fetch-signal-listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { getEventListeners } from 'node:events'
import t from 'tap'
import { LRUCache } from '../dist/esm/node/index.js'

const listeners = (signal: AbortSignal) =>
getEventListeners(signal, 'abort').length

t.test('no abort listeners left on their signal', async t => {
const ac = new AbortController()
const { signal } = ac
const c = new LRUCache<number, string>({
max: 3,
fetchMethod: async n => {
if (n % 3 === 2) throw new Error('fetch failed')
return String(n)
},
})
t.equal(listeners(signal), 0)

const pending = [c.fetch(0, { signal }), c.fetch(1, { signal })]
t.equal(listeners(signal), 2, 'listening while fetches are pending')
t.same(await Promise.all(pending), ['0', '1'])
t.equal(listeners(signal), 0, 'removed when the fetches resolve')

await t.rejects(c.fetch(2, { signal }), { message: 'fetch failed' })
t.equal(listeners(signal), 0, 'removed when a fetch rejects')

for (let i = 3; i < 500; i++) {
await c.fetch(i, { signal }).catch(() => {})
}
t.equal(listeners(signal), 0, 'no accumulation over many fetches')
t.equal(signal.aborted, false)
})

t.test('sync fetchMethod, and fetchMethod that throws', async t => {
const { signal } = new AbortController()
const sync = new LRUCache<number, string>({
max: 3,
fetchMethod: n => String(n),
})
t.equal(await sync.fetch(1, { signal }), '1')
t.equal(listeners(signal), 0)

const throws = new LRUCache<number, string>({
max: 3,
fetchMethod: () => {
throw new Error('sync throw')
},
})
await t.rejects(throws.fetch(1, { signal }), { message: 'sync throw' })
t.equal(listeners(signal), 0)
})

t.test(
'aborting their signal mid-fetch still aborts the fetch',
async t => {
const reason = new Error('stop it')
let seen: AbortSignal | undefined
const c = new LRUCache<number, string>({
max: 3,
fetchMethod: (_k, _v, { signal }) => {
seen = signal
return new Promise((_res, rej) =>
signal.addEventListener('abort', () => rej(signal.reason)),
)
},
})
const ac = new AbortController()
const p = c.fetch(1, { signal: ac.signal })
t.equal(listeners(ac.signal), 1)
t.equal(seen?.aborted, false)
ac.abort(reason)
t.equal(seen?.aborted, true, 'abort passed to the fetchMethod')
t.equal(seen?.reason, reason)
await t.rejects(p, reason)
t.equal(listeners(ac.signal), 0)
t.equal(c.has(1), false)
},
)

t.test('abort mid-fetch with allowStaleOnFetchAbort', async t => {
let n = 0
const c = new LRUCache<number, string>({
max: 3,
ttl: 10,
allowStale: true,
allowStaleOnFetchAbort: true,
fetchMethod: (_k, _v, { signal }) =>
new Promise((res, rej) => {
const i = ++n
const timer = setTimeout(() => res('fetched ' + i), 20)
signal.addEventListener('abort', () => {
clearTimeout(timer)
rej(signal.reason)
})
}),
})
c.set(1, 'stale')
await new Promise(r => setTimeout(r, 15))
const ac = new AbortController()
const p = c.fetch(1, { signal: ac.signal })
t.equal(listeners(ac.signal), 1)
ac.abort(new Error('stop it'))
t.equal(await p, 'stale')
t.equal(listeners(ac.signal), 0)
})

t.test('their signal is already aborted', async t => {
const reason = new Error('already aborted')
const ac = new AbortController()
ac.abort(reason)
const seen: boolean[] = []
const status: LRUCache.Status<number, string> = {}
const c = new LRUCache<number, string>({
max: 3,
fetchMethod: async (_k, _v, { signal }) => {
seen.push(signal.aborted)
return 'value'
},
})
await t.rejects(c.fetch(1, { signal: ac.signal, status }), reason)
t.same(seen, [true], 'fetchMethod is called with an aborted signal')
t.equal(status.fetchAborted, true)
t.equal(status.fetchError, reason)
t.equal(c.has(1), false, 'nothing cached')
t.equal(listeners(ac.signal), 0)

// same as it would be if they aborted right after calling fetch()
const ac2 = new AbortController()
const p = c.fetch(2, { signal: ac2.signal })
ac2.abort(reason)
await t.rejects(p, reason)
t.equal(c.has(2), false)

// abort is ignored when asked to, like any other abort
const ignore = new LRUCache<number, string>({
max: 3,
ignoreFetchAbort: true,
fetchMethod: async () => 'value',
})
t.equal(await ignore.fetch(1, { signal: ac.signal }), 'value')
t.equal(ignore.get(1), 'value')
})

t.test('fetched values are not kept alive by their signal', async t => {
if (typeof gc !== 'function') {
t.comment('run with --expose-gc')
return
}
const { signal } = new AbortController()
const c = new LRUCache<number, object>({
max: 1,
fetchMethod: async () => ({}),
})
const refs: WeakRef<object>[] = []
for (let i = 0; i < 50; i++) {
refs.push(new WeakRef(await c.fetch(i, { signal })))
}
// WeakRef targets are kept until the end of the job that made them
await new Promise(r => setTimeout(r, 10))
gc()
await new Promise(r => setTimeout(r, 10))
gc()
// the cache holds the most recent one
const alive = refs.filter(r => r.deref() !== undefined).length
t.equal(alive, 1, 'only the value in the cache is still alive')
t.equal(c.size, 1)
})