fix(docker): dispose owned clients with their CDI beans (#7267)

Close control-plane and streaming Docker clients through qualifier-matched CDI disposers. Verify generated bean destruction releases its owned idle eviction thread while preserving another client pool. Close direct producer fixtures.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-02 07:04:46 -04:00
1 parent 23a8dbb198
commit 699d472f20
3 files changed
+95 -8

No files matched your search

@@ -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
@@ -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<DockerClient> bean = Arc.container().instance(DockerClient.class, qualifier).getBean();
CreationalContext<DockerClient> firstContext = Arc.container().beanManager().createCreationalContext(bean);
CreationalContext<DockerClient> secondContext = Arc.container().beanManager().createCreationalContext(bean);
ClassLoader previous = Thread.currentThread().getContextClassLoader();
ClassLoader marker = new ClassLoader(previous) { };
List<Thread> firstThreads = List.of();
List<Thread> 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<Thread> 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<Thread> 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<Thread> 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<StreamingDocker>
implements StreamingDocker {
private static final long serialVersionUID = 1L;
}
}
@@ -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)