Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

UNDERTOW-2309 Prevent memory leak in DefaultByteBufferPool #1518

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion core/src/main/java/io/undertow/server/DefaultByteBufferPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

Expand All @@ -39,7 +41,7 @@
*/
public class DefaultByteBufferPool implements ByteBufferPool {

private final ThreadLocal<ThreadLocalData> threadLocalCache = new ThreadLocal<>();
private final ThreadLocalCache threadLocalCache = new ThreadLocalCache();
// Access requires synchronization on the threadLocalDataList instance
private final List<WeakReference<ThreadLocalData>> threadLocalDataList = new ArrayList<>();
private final ConcurrentLinkedQueue<ByteBuffer> queue = new ConcurrentLinkedQueue<>();
Expand Down Expand Up @@ -228,6 +230,7 @@ public void close() {
local.buffers.clear();
}
ref.clear();
threadLocalCache.remove(local);
}
threadLocalDataList.clear();
}
Expand Down Expand Up @@ -332,4 +335,29 @@ protected void finalize() throws Throwable {
}
}

// This is used instead of Java ThreadLocal class. Unlike in the ThreadLocal class, the remove() method in this
// class can be called by a different thread than the one that initialized the data.
private static class ThreadLocalCache {

Map<Thread, ThreadLocalData> localsByThread = new HashMap<>();

ThreadLocalData get() {
return localsByThread.get(Thread.currentThread());
}

void set(ThreadLocalData threadLocalData) {
localsByThread.put(Thread.currentThread(), threadLocalData);
}

void remove(ThreadLocalData threadLocalData) {
// Find the entry containing given data instance and remove it from the map.
for (Map.Entry<Thread, ThreadLocalData> entry: localsByThread.entrySet()) {
if (threadLocalData.equals(entry.getValue())) {
localsByThread.remove(entry.getKey(), entry.getValue());
break;
}
}
}
}

}
Loading