Light, fluffy, and always free - The AWS Local Emulator alternative
fix(core): stop leaked executor threads from pinning test classloaders (#2363)
TATSUNO “Taz” Yasuhiro6962e9102053
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.javaindex a406bc7ab2..55241f2ebe 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLogStreamer.java@@ -25,8 +25,8 @@ import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Optional;-import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;+import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.ScheduledFuture;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.ReentrantReadWriteLock;@@ -207,12 +207,21 @@ public class ContainerLogStreamer {*/static final class LogReassemblyCallback extends ResultCallback.Adapter<Frame> {- private static final ScheduledExecutorService CLOSE_DRAIN_SCHEDULER =- Executors.newSingleThreadScheduledExecutor(runnable -> {- Thread thread = new Thread(runnable, "floci-container-log-close");- thread.setDaemon(true);- return thread;- });+ // The idle core thread must time out. A permanent thread would keep executing-class+ // references alive and pin the whole application classloader after shutdown, which+ // accumulates across the many app restarts of a profile-switching test run.+ private static final ScheduledExecutorService CLOSE_DRAIN_SCHEDULER = closeDrainScheduler();++ private static ScheduledExecutorService closeDrainScheduler() {+ var scheduler = new ScheduledThreadPoolExecutor(1, runnable -> {+ Thread thread = new Thread(runnable, "floci-container-log-close");+ thread.setDaemon(true);+ return thread;+ });+ scheduler.setKeepAliveTime(5, TimeUnit.SECONDS);+ scheduler.allowCoreThreadTimeOut(true);+ return scheduler;+ }private static final long DEFAULT_CLOSE_GRACE_MILLIS = 2_000;private final Map<StreamType, LogLineBuffer> buffers = new EnumMap<>(StreamType.class);diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.javaindex fa6666123f..caa6a4812f 100644--- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java+++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.elbv2.model.TargetDescription;import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;import io.github.hectorvent.floci.services.ssm.SsmCommandService;import jakarta.annotation.PostConstruct;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.enterprise.event.Observes;import jakarta.inject.Inject;@@ -59,6 +60,11 @@ public class AutoScalingReconciler {scheduler.scheduleAtFixedRate(this::reconcileAll, 5, 10, TimeUnit.SECONDS);}+ @PreDestroy+ void stop() {+ scheduler.shutdownNow();+ }+void onStart(@Observes StartupEvent event) {LOG.debug("Auto Scaling reconciler initialized");}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.javaindex b8d970965b..079a8134d2 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java@@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import jakarta.annotation.PostConstruct;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -102,6 +103,11 @@ public class CloudFormationService {}}+ @PreDestroy+ void stop() {+ executor.shutdownNow();+ }+private void persistStack(Stack stack) {stackBackend.putForAccount(storageAccount, key(stack.getStackName(), stack.getRegion()), stack);}diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.javaindex 60f8de28b1..b5ace7e0ba 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudmap/CloudMapService.java@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.cloudmap.model.Instance;import io.github.hectorvent.floci.services.cloudmap.model.Namespace;import io.github.hectorvent.floci.services.cloudmap.model.Operation;import io.github.hectorvent.floci.services.cloudmap.model.Service;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -71,6 +72,13 @@ public class CloudMapService {: null;}+ @PreDestroy+ void stop() {+ if (scheduler != null) {+ scheduler.shutdownNow();+ }+ }+// ──────────────────────────── Namespaces ────────────────────────────public Operation createHttpNamespace(String name, String creatorRequestId,diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.javaindex 75701268c5..ca07188941 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2ContainerManager.java@@ -19,6 +19,7 @@ import com.github.dockerjava.api.model.ContainerNetwork;import com.github.dockerjava.api.model.Frame;import com.github.dockerjava.api.model.Mount;import com.github.dockerjava.api.model.MountType;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.apache.commons.compress.archivers.tar.TarArchiveEntry;@@ -98,6 +99,11 @@ public class Ec2ContainerManager {this.portForwardManager = portForwardManager;}+ @PreDestroy+ void stop() {+ executor.shutdownNow();+ }+/*** Launches a Docker container for the given EC2 instance.* The instance starts in pending state; an async thread transitions it to runningdiff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.javaindex bb1c7e88a5..cb5b8db465 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;import com.github.dockerjava.api.DockerClient;import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.model.ContainerNetwork;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -75,6 +76,11 @@ public class Ec2PortForwardManager {this.config = config;}+ @PreDestroy+ void stop() {+ executor.shutdownNow();+ }+/*** Reconciles the instance's live forwards against the desired port set: publishes ports* that are newly opened and unpublishes ports that no longer have an ingress rule. Runsdiff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.javaindex 9d06cce3ed..29a575a5de 100644--- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java@@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -129,6 +130,11 @@ public class SqsService implements Resettable {loadPersistedDedup();}+ @PreDestroy+ void stop() {+ moveTaskExecutor.shutdownNow();+ }+public void clear() {messagesByQueue.values().forEach(GuardedMessageQueue::close);messagesByQueue.clear();diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.javaindex e92d6dd569..551a046d87 100644--- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmCommandService.java@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.ssm.model.Command;import io.github.hectorvent.floci.services.ssm.model.CommandInvocation;import io.github.hectorvent.floci.services.ssm.model.InstanceInformation;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;import org.jboss.logging.Logger;@@ -79,6 +80,11 @@ public class SsmCommandService implements Resettable {});}+ @PreDestroy+ void stop() {+ directExecutionExecutor.shutdownNow();+ }+public void clear() {messageQueues.clear();messageIndex.clear();diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.javaindex 77b2251e5c..3dc55e37d2 100644--- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java+++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java@@ -48,6 +48,7 @@ import io.vertx.mutiny.ext.web.client.WebClient;import io.quarkus.arc.Arc;import io.quarkus.arc.ArcContainer;import io.quarkus.arc.ManagedContext;+import jakarta.annotation.PreDestroy;import jakarta.enterprise.context.ApplicationScoped;import jakarta.enterprise.inject.Instance;import jakarta.inject.Inject;@@ -189,6 +190,11 @@ public class AslExecutor {}}+ @PreDestroy+ void stop() {+ executor.shutdownNow();+ }+/*** Launches execution asynchronously. Calls onUpdate when execution status changes.*/diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.javaindex 5ee0037e61..1472d1ab3c 100644--- a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconcilerTest.java@@ -22,6 +22,7 @@ import java.util.Map;import java.util.Set;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.mockito.Mockito.anyList;import static org.mockito.Mockito.eq;import static org.mockito.Mockito.mock;@@ -44,6 +45,25 @@ class AutoScalingReconcilerTest {assertEquals(2, AutoScalingReconciler.activeCapacity(asg));}+ @Test+ void stopTerminatesReconcilerThread() throws Exception {+ var asgService = mock(AutoScalingService.class);+ var ec2Service = mock(Ec2Service.class);+ var elbV2Service = mock(ElbV2Service.class);+ var reconciler = new AutoScalingReconciler(asgService, ec2Service, elbV2Service);+ var preexisting = Thread.getAllStackTraces().keySet();++ reconciler.start();+ var reconcilerThread = Thread.getAllStackTraces().keySet().stream()+ .filter(t -> "asg-reconciler".equals(t.getName()) && !preexisting.contains(t))+ .findFirst().orElseThrow();++ reconciler.stop();+ reconcilerThread.join(5_000);++ assertFalse(reconcilerThread.isAlive());+ }+@Testvoid reconcileCompletesSettledInstanceRefreshes() {AutoScalingService asgService = mock(AutoScalingService.class);diff --git a/src/test/resources/application.yml b/src/test/resources/application.ymlindex 962316ab47..69badf49ea 100644--- a/src/test/resources/application.yml+++ b/src/test/resources/application.yml@@ -6,6 +6,11 @@ quarkus:max-body-size: ${floci.max-request-size}Mdatasource:active: false+ # Groovy (a REST Assured dependency) starts DFA-cache-cleaner threads that never stop.+ # Loaded parent-first they live in the shared system classloader, so they cannot pin a+ # per-profile application classloader across the many app restarts of a test run.+ class-loading:+ parent-first-artifacts: org.apache.groovy:groovyfloci:base-url: "http://localhost:4566"