-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
InMemoryKache::getOrPut fails in high concurrency context #239
Comments
We are seeing similar NPEs in this code.
SHA256KeyHasher has the following code: override suspend fun transform(oldKey: String): String = hashedCache.getOrPut(oldKey) {
oldKey.encodeUtf8().sha256().hex().uppercase()
}!! // Since our creation function never returns null, we can use not-null assertion Notice the comment at the end. Now look at the following: override suspend fun getOrPut(key: K, creationFunction: suspend (key: K) -> V?): V? {
get(key)?.let { return it }
creationMutex.withLock {
if (creationMap[key] == null && map[key] == null) {
@Suppress("DeferredResultUnused")
internalPutAsync(key, creationFunction) // <!--- ASYNC
}
}
return get(key) // <!-- DID ASYNC ABOVE FINISH?
} I know nothing about coroutines in Kotlin, but I see an async call that I'm guessing is not guaranteed to finish before the return get call at the end of the method, resulting in a nasty race condition where sometimes you'll get a value and other times you will not. This seems extremely bad. |
I was able to replicate the crash using the unit test provided above:
I found that by replacing the second call to private suspend fun unsafeGet(key: K): V = (getFromCreation(key) ?: getIfAvailable(key))!! Since As an aside, by calling |
Thank you for reporting the issue and investigating it, and sorry for being late. I would like to further investigate and try to fix the issue in the next few days. |
Hi 😸
I noticed I was getting
NullPointerException
s when usingSHA256KeyHasher
.I could narrow it down to
InMemoryKache
- the following test fails:Stack trace:
The text was updated successfully, but these errors were encountered: