From 415196520ecf798121c6e3e85cc7f64e7d74918c Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Wed, 9 Dec 2020 10:38:32 +0100 Subject: [PATCH] fix race condition that may make the bucket cleaning pick the wrong one in case the timestamp is read while being modified --- .../src/main/java/org/eclipse/jetty/io/ByteBufferPool.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java index a78644ae9723..5d78be85fb9e 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferPool.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; import org.eclipse.jetty.util.BufferUtil; @@ -158,7 +159,7 @@ class Bucket private final int _capacity; private final int _maxSize; private final AtomicInteger _size; - private long _lastUpdate = System.nanoTime(); + private final AtomicLong _lastUpdate = new AtomicLong(System.nanoTime()); public Bucket(ByteBufferPool pool, int capacity, int maxSize) { @@ -196,7 +197,7 @@ public ByteBuffer acquire(boolean direct) public void release(ByteBuffer buffer) { - _lastUpdate = System.nanoTime(); + _lastUpdate.lazySet(System.nanoTime()); BufferUtil.clear(buffer); if (_size == null) queueOffer(buffer); @@ -251,7 +252,7 @@ int size() long getLastUpdate() { - return _lastUpdate; + return _lastUpdate.get(); } @Override