diff --git a/index.js b/index.js index 77adaa64..70aa8ca9 100644 --- a/index.js +++ b/index.js @@ -315,12 +315,16 @@ class LRUCache { purgeStale () { let deleted = false + const toDelete = [] for (const i of this.rindexes({ allowStale: true })) { if (this.isStale(i)) { - this.delete(this.keyList[i]) + toDelete.push(this.keyList[i]) deleted = true } } + for (const k of toDelete) { + this.delete(k) + } return deleted } diff --git a/test/ttl.js b/test/ttl.js index e711fa21..099f5bea 100644 --- a/test/ttl.js +++ b/test/ttl.js @@ -359,6 +359,23 @@ const runTests = (LRU, t) => { t.end() }) + t.test('purgeStale() lockup', t => { + const c = new LRU({ + max: 3, + ttl: 10, + updateAgeOnGet: true, + }) + c.set(1, 1) + c.set(2, 2) + c.set(3, 3) + clock.advance(5) + c.get(2) + clock.advance(15) + c.purgeStale() + t.pass('did not get locked up') + t.end() + }) + t.end() }