Skip to content

Commit

Permalink
fix: fix race condition with cache causing ytdl-core to hang
Browse files Browse the repository at this point in the history
sometimes when clearing the ytdl-core internal cache, the app would
still hang, waiting for any entries in the cache to expire.
  • Loading branch information
fent committed Nov 14, 2020
1 parent 64d643c commit 60cdb41
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/cache.js
Expand Up @@ -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,
Expand All @@ -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 comment has been minimized.

Copy link
@benkaiser

benkaiser Dec 8, 2020

@fent I'm seeing some instances where we are now caching a promise in the identity token cache rather than the value it will resolve to. Is this change here the root cause? Perhaps fixing this issue regressed the promises passed to getOrSet

This comment has been minimized.

Copy link
@benkaiser

benkaiser Dec 8, 2020

Created issue: #815

this.set(key, value);
return value;
}
Expand Down

0 comments on commit 60cdb41

Please sign in to comment.