Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix Integer Overflow in Calculation of Virtual Thread Queue Size #80

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -83,7 +83,12 @@ public VirtualThreadsManagedExecutorService(String name,
if (maxParallelTasks > 0) {
parallelTasksSemaphore = new Semaphore(maxParallelTasks, true);
if (queueCapacity > 0) {
queuedTasksSemaphore = new Semaphore(queueCapacity + maxParallelTasks, true);
int virtualCapacity = queueCapacity + maxParallelTasks;
if (virtualCapacity <= 0) {
// int overflow; queue capacity is often MAX_VALUE
virtualCapacity = Integer.MAX_VALUE;
}
queuedTasksSemaphore = new Semaphore(virtualCapacity, true);
}
}
executor = Executors.newThreadPerTaskExecutor(getManagedThreadFactory());
Expand Down Expand Up @@ -111,7 +116,7 @@ protected void executeManagedFutureTask(ManagedFutureTask<?> task) {
getThreadPoolExecutor().execute(task);
runningFutures.add(task);
} else {
throw new RejectedExecutionException("Too many tasks submitted (maxParallelTasks=" + maxParallelTasks + ", queueCapacity=" + queueCapacity);
throw new RejectedExecutionException("Too many tasks submitted (available = " + (queuedTasksSemaphore == null ? "UNUSED" : queuedTasksSemaphore.availablePermits()) + ", maxParallelTasks = " + maxParallelTasks + ", queueCapacity = " + queueCapacity);
}
}

Expand Down