Skip to content

Commit

Permalink
Merge pull request #12198 from jetty/jetty-12.0.x-VirtualThreadPoolSe…
Browse files Browse the repository at this point in the history
…maphore

Fix potential NPE from VirtualThreadPool
  • Loading branch information
joakime authored Aug 26, 2024
2 parents 0f28d47 + 4fd306f commit 394bc13
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 = () ->
{
Expand All @@ -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)
Expand All @@ -276,7 +277,7 @@ public void execute(Runnable task)
}
finally
{
_semaphore.release();
semaphore.release();
}
};
}
Expand Down

0 comments on commit 394bc13

Please sign in to comment.