fix(core): stop leaked executor threads from pinning test classloaders (#2363)

Service beans never shut down their executors. The leftover threads
pinned the classloader of every stopped test application. Around 40
profile restarts per run grew retained heap past the 4GB surefire
default and CI failed with OutOfMemoryError.

Add PreDestroy shutdown to the owning beans. Let the close-drain
scheduler thread time out when idle. Load Groovy parent-first in tests
so its cleaner threads stop pinning app classloaders.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-08-17 22:36:41 -04:00
1 parent 0444753eb8
commit 6962e91020
11 files changed
+91 -7

No files matched your search

@@ -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);
@@ -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");
}
@@ -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);
}
@@ -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,
@@ -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 running
@@ -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. Runs
@@ -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();
@@ -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();
@@ -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.
*/
@@ -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());
}
@Test
void reconcileCompletesSettledInstanceRefreshes() {
AutoScalingService asgService = mock(AutoScalingService.class);
+5
View File
@@ -6,6 +6,11 @@ quarkus:
max-body-size: ${floci.max-request-size}M
datasource:
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:groovy
floci:
base-url: "http://localhost:4566"