From 60cdb41a8bb2a7334edd5e464e4dc5f178f9c6e5 Mon Sep 17 00:00:00 2001 From: fent <933490+fent@users.noreply.github.com> Date: Thu, 12 Nov 2020 20:17:00 -0700 Subject: [PATCH] fix: fix race condition with cache causing ytdl-core to hang sometimes when clearing the ytdl-core internal cache, the app would still hang, waiting for any entries in the cache to expire. --- lib/cache.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cache.js b/lib/cache.js index 1eb0a903..e99ef2e2 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -5,6 +5,9 @@ module.exports = class Cache extends Map { this.timeout = timeout; } set(key, value) { + if (this.has(key)) { + clearTimeout(super.get(key).tid); + } super.set(key, { tid: setTimeout(this.delete.bind(this, key), this.timeout), value, @@ -17,11 +20,11 @@ module.exports = class Cache extends Map { } return null; } - async getOrSet(key, fn) { + getOrSet(key, fn) { if (this.has(key)) { return this.get(key); } else { - let value = await fn(); + let value = fn(); this.set(key, value); return value; }