From 65c697d3ac47ab97570f043229158948b15ee05c Mon Sep 17 00:00:00 2001 From: phinner <62483793+phinner@users.noreply.github.com> Date: Wed, 17 Apr 2024 23:47:00 +0200 Subject: [PATCH] fix: Fix argon and balloon blocking application shutdown --- src/main/java/com/password4j/Argon2Function.java | 2 +- .../com/password4j/BalloonHashingFunction.java | 2 +- src/main/java/com/password4j/Utils.java | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/password4j/Argon2Function.java b/src/main/java/com/password4j/Argon2Function.java index 49632b8..32f7658 100755 --- a/src/main/java/com/password4j/Argon2Function.java +++ b/src/main/java/com/password4j/Argon2Function.java @@ -101,7 +101,7 @@ public class Argon2Function extends AbstractHashingFunction if (parallelism >= 1) { - service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS); + service = Utils.createExecutorService(); } } diff --git a/src/main/java/com/password4j/BalloonHashingFunction.java b/src/main/java/com/password4j/BalloonHashingFunction.java index 08c416e..09538b1 100644 --- a/src/main/java/com/password4j/BalloonHashingFunction.java +++ b/src/main/java/com/password4j/BalloonHashingFunction.java @@ -59,7 +59,7 @@ public class BalloonHashingFunction extends AbstractHashingFunction this.delta = delta; if (parallelism > 1) { - this.service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS); + this.service = Utils.createExecutorService(); } } diff --git a/src/main/java/com/password4j/Utils.java b/src/main/java/com/password4j/Utils.java index c573de0..d3fe2f3 100755 --- a/src/main/java/com/password4j/Utils.java +++ b/src/main/java/com/password4j/Utils.java @@ -32,6 +32,9 @@ import java.util.LinkedList; import java.util.List; import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,6 +53,10 @@ class Utils private static final int[] FROM_BASE64 = new int[256]; + private static final AtomicInteger THREAD_COUNTER = new AtomicInteger(1); + + private static final ThreadGroup THREAD_GROUP = new ThreadGroup("Password4j Workers"); + static { Arrays.fill(FROM_BASE64, -1); @@ -655,6 +662,11 @@ static List split(byte[] array, byte delimiter) { return byteArrays; } - - + static ExecutorService createExecutorService() { + return Executors.newFixedThreadPool(AVAILABLE_PROCESSORS, runnable -> { + Thread thread = new Thread(THREAD_GROUP, runnable, "password4j-worker-" + THREAD_COUNTER.getAndIncrement()); + thread.setDaemon(true); + return thread; + }); + } }