Skip to content

Commit 1b93ea9

Browse files
committed
Fix concurrent reads issue in MimeTypeUtils cache
As of gh-22340, `MimeTypeUtils` has a built-in LRU cache implementation for caching parsed MIME types and avoiding excessive garbage creation at runtime. This implementation, when hit with highly concurrent reads on the same media type (the cache key), can create multiple keys for the same MIME type string. This duplication leads to the cache filling up and evicting entries. When the cache fetches a duplicate key, it is then not associated with a value and the cache can return a `null` value, which is forbidden by the API contract. This commit adds another cache check within the write lock: this avoids creating duplicate entries in the cache and `null` return values. Fixes gh-23211
1 parent 5bf070a commit 1b93ea9

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

+5
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ public V get(K key) {
443443
}
444444
this.lock.writeLock().lock();
445445
try {
446+
// retrying in case of concurrent reads on the same key
447+
if (this.queue.remove(key)) {
448+
this.queue.add(key);
449+
return this.cache.get(key);
450+
}
446451
if (this.queue.size() == this.maxSize) {
447452
K leastUsed = this.queue.poll();
448453
if (leastUsed != null) {

0 commit comments

Comments
 (0)