From a483239d98c01c88e2ac419c3660abdf7422bbe2 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Thu, 28 Mar 2024 23:14:46 +0000 Subject: [PATCH 1/2] shutdown method blocks until task executor shutdown completes Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com> --- src/main/java/dev/openfeature/sdk/EventSupport.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/openfeature/sdk/EventSupport.java b/src/main/java/dev/openfeature/sdk/EventSupport.java index f9bb67dd9..b7f2a3d04 100644 --- a/src/main/java/dev/openfeature/sdk/EventSupport.java +++ b/src/main/java/dev/openfeature/sdk/EventSupport.java @@ -12,6 +12,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; /** @@ -23,6 +24,7 @@ class EventSupport { // we use a v4 uuid as a "placeholder" for anonymous clients, since // ConcurrentHashMap doesn't support nulls private static final String defaultClientUuid = UUID.randomUUID().toString(); + private static final int SHUTDOWN_TIMEOUT_SECONDS = 3; private final Map handlerStores = new ConcurrentHashMap<>(); private final HandlerStore globalHandlerStore = new HandlerStore(); private final ExecutorService taskExecutor = Executors.newCachedThreadPool(runnable -> { @@ -146,11 +148,15 @@ public void runHandler(Consumer handler, EventDetails eventDetails } /** - * Stop the event handler task executor. + * Stop the event handler task executor and block until either termination has completed + * or timeout period has elapsed. */ public void shutdown() { try { taskExecutor.shutdown(); + if (!taskExecutor.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { + log.warn("Task executor did not terminate before the timeout period had elapsed"); + } } catch (Exception e) { log.warn("Exception while attempting to shutdown task executor", e); } From b9e1ca6bbb0e1605fc1f70b308bc44a9668ccb38 Mon Sep 17 00:00:00 2001 From: jarebudev <23311805+jarebudev@users.noreply.github.com> Date: Thu, 28 Mar 2024 23:47:42 +0000 Subject: [PATCH 2/2] addressed sonar issue with not handling interrupt during waiting Signed-off-by: jarebudev <23311805+jarebudev@users.noreply.github.com> --- src/main/java/dev/openfeature/sdk/EventSupport.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/openfeature/sdk/EventSupport.java b/src/main/java/dev/openfeature/sdk/EventSupport.java index b7f2a3d04..49156b1a5 100644 --- a/src/main/java/dev/openfeature/sdk/EventSupport.java +++ b/src/main/java/dev/openfeature/sdk/EventSupport.java @@ -152,13 +152,15 @@ public void runHandler(Consumer handler, EventDetails eventDetails * or timeout period has elapsed. */ public void shutdown() { + taskExecutor.shutdown(); try { - taskExecutor.shutdown(); if (!taskExecutor.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { log.warn("Task executor did not terminate before the timeout period had elapsed"); + taskExecutor.shutdownNow(); } - } catch (Exception e) { - log.warn("Exception while attempting to shutdown task executor", e); + } catch (InterruptedException e) { + taskExecutor.shutdownNow(); + Thread.currentThread().interrupt(); } }