Merge pull request #12198 from jetty/jetty-12.0.x-VirtualThreadPoolSemaphore

Fix potential NPE from VirtualThreadPool
This commit is contained in:
Joakim Erdfelt 2024-08-26 10:49:37 -05:00 committed by GitHub
commit 394bc136f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 4 deletions

View File

@ -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 class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool,
public void execute(Runnable task)
{
Runnable job = task;
if (_semaphore != null)
Semaphore semaphore = _semaphore;
if (semaphore != null)
{
job = () ->
{
@ -265,7 +266,7 @@ public class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool,
// 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 class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool,
}
finally
{
_semaphore.release();
semaphore.release();
}
};
}