From 3c0ac40b3bf429ee0af217d82af722a41ceb7659 Mon Sep 17 00:00:00 2001 From: "Kenneth J. Shackleton" Date: Wed, 7 Jul 2021 14:46:55 +0100 Subject: [PATCH] Close the factory under the SingleObjectPool lock to avoid racing on creation when closing the pool. Signed-off-by: Kenneth J. Shackleton --- .../com/bloomberg/selekt/pools/SingleObjectPool.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/src/main/kotlin/com/bloomberg/selekt/pools/SingleObjectPool.kt b/Lib/src/main/kotlin/com/bloomberg/selekt/pools/SingleObjectPool.kt index 70276d71f8..311217ca74 100644 --- a/Lib/src/main/kotlin/com/bloomberg/selekt/pools/SingleObjectPool.kt +++ b/Lib/src/main/kotlin/com/bloomberg/selekt/pools/SingleObjectPool.kt @@ -63,7 +63,7 @@ internal class SingleObjectPool>( override fun returnObject(obj: T) { canEvict = false if (isClosed) { - evictThenUnlock() + processCloseThenUnlock() } else { mutex.unlock() } @@ -75,12 +75,11 @@ internal class SingleObjectPool>( internal fun evict(priority: Priority? = null) = mutex.run { when { - isClosed -> { + isClosed -> withTryLock { factory.close() + evictions(null) + }.also { attemptUnparkWaiters() - withTryLock { - evictions(null) - } } priority.isHigh() -> withTryLock { evictions(priority) @@ -141,8 +140,9 @@ internal class SingleObjectPool>( } } - private fun evictThenUnlock() { + private fun processCloseThenUnlock() { try { + factory.close() evictions(null) } finally { mutex.unlock()