From 21ae1dbac000fc5936b33e6367e8454ab035e0b1 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 21 Jun 2020 22:07:53 +0100 Subject: [PATCH] Fix potential race condition in MRUCache --- .../lucko/luckperms/common/cache/MRUCache.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/cache/MRUCache.java b/common/src/main/java/me/lucko/luckperms/common/cache/MRUCache.java index 0e4e60c96..e9cb4ce4d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/cache/MRUCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/cache/MRUCache.java @@ -46,13 +46,21 @@ public abstract class MRUCache { } protected void offerRecent(int validAt, T offer) { - if (validAt == this.modCount.get()) { - this.recent = offer; + synchronized (this) { + if (validAt == this.modCount.get()) { + this.recent = offer; + } } } + /** + * Calling clearRecent effectively resets the instance. any other threads going to call + * offerRecent in the future with the previous mod count will be ignored. + */ protected void clearRecent() { - this.recent = null; - this.modCount.incrementAndGet(); + synchronized (this) { + this.recent = null; + this.modCount.incrementAndGet(); + } } }