From 8464340f079591bccca5f773d17c4f8383f45ed6 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 23 Aug 2024 14:15:19 +1000 Subject: [PATCH] Fix potential NPE from VirtualThreadPool Signed-off-by: Lachlan Roberts --- .../org/eclipse/jetty/util/thread/VirtualThreadPool.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/VirtualThreadPool.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/VirtualThreadPool.java index 09e4d2577c6c..d743dd6fdd2a 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/VirtualThreadPool.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/thread/VirtualThreadPool.java @@ -46,7 +46,7 @@ public class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool, private Thread _keepAlive; private Executor _virtualExecutor; private boolean _externalExecutor; - private Semaphore _semaphore; + private volatile Semaphore _semaphore; public VirtualThreadPool() { @@ -255,7 +255,8 @@ public boolean tryExecute(Runnable task) public void execute(Runnable task) { Runnable job = task; - if (_semaphore != null) + Semaphore semaphore = _semaphore; + if (semaphore != null) { job = () -> { @@ -265,7 +266,7 @@ public void execute(Runnable task) // as it is unknown whether it is a virtual thread. // But this is a virtual thread, so acquiring a permit here // blocks the virtual thread, but does not pin the carrier. - _semaphore.acquire(); + semaphore.acquire(); task.run(); } catch (InterruptedException x) @@ -276,7 +277,7 @@ public void execute(Runnable task) } finally { - _semaphore.release(); + semaphore.release(); } }; }