diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java index 8176048964..240aad926f 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java @@ -12,6 +12,7 @@ import com.github.dockerjava.transport.DockerHttpClient; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.Digests; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Disposes; import jakarta.enterprise.inject.Produces; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -450,6 +451,14 @@ public class DockerClientProducer { validateStreamingMaxConnections(config.docker().streamingMaxConnections()), "streaming"); } + void disposeDockerClient(@Disposes DockerClient client) throws IOException { + client.close(); + } + + void disposeStreamingDockerClient(@Disposes @StreamingDocker DockerClient client) throws IOException { + client.close(); + } + /** * Builds the {@link DefaultDockerClientConfig} shared by both DockerClient beans (host and * Docker-context resolution, optional Docker config path, and any TLS material the active diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientDisposalIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientDisposalIntegrationTest.java new file mode 100644 index 0000000000..4ad78a2b46 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientDisposalIntegrationTest.java @@ -0,0 +1,78 @@ +package io.github.hectorvent.floci.core.common.docker; + +import com.github.dockerjava.api.DockerClient; +import io.quarkus.arc.Arc; +import io.quarkus.arc.InjectableBean; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.enterprise.context.spi.CreationalContext; +import jakarta.enterprise.inject.Default; +import jakarta.enterprise.util.AnnotationLiteral; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.lang.annotation.Annotation; +import java.time.Duration; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class DockerClientDisposalIntegrationTest { + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void destroyingProducedClientStopsOnlyItsEvictor(boolean streaming) throws Exception { + Annotation qualifier = streaming ? new StreamingLiteral() : Default.Literal.INSTANCE; + InjectableBean bean = Arc.container().instance(DockerClient.class, qualifier).getBean(); + CreationalContext firstContext = Arc.container().beanManager().createCreationalContext(bean); + CreationalContext secondContext = Arc.container().beanManager().createCreationalContext(bean); + ClassLoader previous = Thread.currentThread().getContextClassLoader(); + ClassLoader marker = new ClassLoader(previous) { }; + List firstThreads = List.of(); + List secondThreads = List.of(); + Thread.currentThread().setContextClassLoader(marker); + try (DockerClient first = bean.create(firstContext)) { + firstThreads = evictors(marker); + assertEquals(1, firstThreads.size(), "one owned connection eviction thread per client"); + try (DockerClient second = bean.create(secondContext)) { + List alreadyOwned = firstThreads; + secondThreads = evictors(marker).stream().filter(thread -> !alreadyOwned.contains(thread)).toList(); + assertEquals(1, secondThreads.size()); + Thread.currentThread().setContextClassLoader(previous); + + bean.destroy(first, firstContext); + assertStopped(firstThreads); + assertTrue(secondThreads.getFirst().isAlive(), "disposing one client must not close another pool"); + bean.destroy(second, secondContext); + assertStopped(secondThreads); + } + } finally { + Thread.currentThread().setContextClassLoader(previous); + firstContext.release(); + secondContext.release(); + assertStopped(firstThreads); + assertStopped(secondThreads); + } + } + + private static List evictors(ClassLoader marker) { + return Thread.getAllStackTraces().keySet().stream() + .filter(thread -> thread.getContextClassLoader() == marker) + .filter(thread -> thread.getName().startsWith("idle-connection-evictor-")) + .toList(); + } + + private static void assertStopped(List threads) throws InterruptedException { + for (Thread thread : threads) { + thread.join(Duration.ofSeconds(5)); + assertFalse(thread.isAlive(), "CDI destruction must release the client thread and its classloader"); + } + } + + private static final class StreamingLiteral extends AnnotationLiteral + implements StreamingDocker { + private static final long serialVersionUID = 1L; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java index 2063addb36..143f146afe 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java @@ -264,7 +264,7 @@ class DockerClientProducerTest { // therefore be genuinely separate DockerClient instances (and thus separate pools), // not the same instance handed out under two qualifiers. @Test - void dockerClientAndStreamingDockerClient_areDistinctInstances() { + void dockerClientAndStreamingDockerClient_areDistinctInstances() throws IOException { EmulatorConfig config = mock(EmulatorConfig.class); EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class); when(config.docker()).thenReturn(docker); @@ -277,13 +277,13 @@ class DockerClientProducerTest { DockerClientProducer producer = new DockerClientProducer(config); - DockerClient controlPlaneClient = producer.dockerClient(); - DockerClient streamingClient = producer.streamingDockerClient(); - - assertNotNull(controlPlaneClient); - assertNotNull(streamingClient); - assertNotSame(controlPlaneClient, streamingClient, - "Control-plane and streaming DockerClient beans must use separate connection pools"); + try (DockerClient controlPlaneClient = producer.dockerClient(); + DockerClient streamingClient = producer.streamingDockerClient()) { + assertNotNull(controlPlaneClient); + assertNotNull(streamingClient); + assertNotSame(controlPlaneClient, streamingClient, + "Control-plane and streaming DockerClient beans must use separate connection pools"); + } } private static void writeContextFixture(Path dockerConfigDir, String contextName, String host)