Skip to content

Commit 8dec14f

Browse files
jarebudevbeeme1mrtoddbaert
authored
fix: shutdown method blocks until task executor shutdown completes (#873)
* shutdown method blocks until task executor shutdown completes Signed-off-by: jarebudev <[email protected]> * addressed sonar issue with not handling interrupt during waiting Signed-off-by: jarebudev <[email protected]> --------- Signed-off-by: jarebudev <[email protected]> Co-authored-by: Michael Beemer <[email protected]> Co-authored-by: Todd Baert <[email protected]>
1 parent dd671ad commit 8dec14f

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Diff for: src/main/java/dev/openfeature/sdk/EventSupport.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.ConcurrentHashMap;
1313
import java.util.concurrent.ExecutorService;
1414
import java.util.concurrent.Executors;
15+
import java.util.concurrent.TimeUnit;
1516
import java.util.function.Consumer;
1617

1718
/**
@@ -23,6 +24,7 @@ class EventSupport {
2324
// we use a v4 uuid as a "placeholder" for anonymous clients, since
2425
// ConcurrentHashMap doesn't support nulls
2526
private static final String defaultClientUuid = UUID.randomUUID().toString();
27+
private static final int SHUTDOWN_TIMEOUT_SECONDS = 3;
2628
private final Map<String, HandlerStore> handlerStores = new ConcurrentHashMap<>();
2729
private final HandlerStore globalHandlerStore = new HandlerStore();
2830
private final ExecutorService taskExecutor = Executors.newCachedThreadPool(runnable -> {
@@ -146,13 +148,19 @@ public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails
146148
}
147149

148150
/**
149-
* Stop the event handler task executor.
151+
* Stop the event handler task executor and block until either termination has completed
152+
* or timeout period has elapsed.
150153
*/
151154
public void shutdown() {
155+
taskExecutor.shutdown();
152156
try {
153-
taskExecutor.shutdown();
154-
} catch (Exception e) {
155-
log.warn("Exception while attempting to shutdown task executor", e);
157+
if (!taskExecutor.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
158+
log.warn("Task executor did not terminate before the timeout period had elapsed");
159+
taskExecutor.shutdownNow();
160+
}
161+
} catch (InterruptedException e) {
162+
taskExecutor.shutdownNow();
163+
Thread.currentThread().interrupt();
156164
}
157165
}
158166

0 commit comments

Comments
 (0)