From 3bf2f858f72188ee30cec893375963d62d9b39a5 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 18 Aug 2025 17:24:34 -0400 Subject: [PATCH] Fixes the unable to recreate user after the GC deletes it. --- .../vitorpamplona/quartz/utils/LargeSoftCache.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/utils/LargeSoftCache.kt b/quartz/src/main/java/com/vitorpamplona/quartz/utils/LargeSoftCache.kt index 947633789..99127c13c 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/utils/LargeSoftCache.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/utils/LargeSoftCache.kt @@ -85,13 +85,21 @@ class LargeSoftCache : CacheOperations { builder: (key: K) -> V, ): V { val softRef = cache[key] - val value = softRef?.get() - return if (value != null) { - value - } else { + return if (softRef == null) { val newObject = builder(key) cache.putIfAbsent(key, WeakReference(newObject))?.get() ?: newObject + } else { + val value = softRef.get() + if (value != null) { + value + } else { + // removes first to make sure the putIfAbsent works. + // another thread may put in between + cache.remove(key, softRef) + val newObject = builder(key) + return cache.putIfAbsent(key, WeakReference(newObject))?.get() ?: newObject + } } }