fix(docker): await GELF accept shutdown before returning (#9324)

Closing a ServerSocket with a pending virtual-thread accept can defer release of its bound descriptor. Wait for the accept worker with a bounded join before shutdown returns, preserving interruption and occupied-port refusal. Retain existing delivery, token, rotation and polling behavior.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-23 23:56:30 -04:00
1 parent a756857581
commit 8df8cee800
2 files changed
+84 -1

No files matched your search

@@ -28,6 +28,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.function.BiConsumer;
/**
@@ -68,7 +69,9 @@ public class GelfLogListener {
private final int maxConnections;
private final Semaphore connections;
private final Object startLock = new Object();
private final ThreadFactory acceptThreads;
private ServerSocket server;
private Thread acceptThread;
private volatile String address;
@Inject
@@ -84,6 +87,13 @@ public class GelfLogListener {
GelfLogListener(DockerClient dockerClient, boolean enabled, String configuredHost, int port,
int maxConnections) {
this(dockerClient, enabled, configuredHost, port, maxConnections,
Thread.ofVirtual().name("floci-gelf-accept").factory());
}
GelfLogListener(DockerClient dockerClient, boolean enabled, String configuredHost, int port,
int maxConnections, ThreadFactory acceptThreads) {
this.acceptThreads = acceptThreads;
this.dockerClient = dockerClient;
this.enabled = enabled;
this.configuredHost = configuredHost;
@@ -204,7 +214,8 @@ public class GelfLogListener {
+ " (floci.docker.log-driver-port)", e);
}
address = host + ":" + server.getLocalPort();
Thread.ofVirtual().name("floci-gelf-accept").start(this::acceptLoop);
acceptThread = acceptThreads.newThread(this::acceptLoop);
acceptThread.start();
LOG.infov("Long-lived service containers send their logs to Floci over GELF at {0}", address);
}
@@ -342,6 +353,13 @@ public class GelfLogListener {
if (server != null) {
try {
server.close();
// A pending accept can retain the bound descriptor until its thread exits.
if (acceptThread != null && !acceptThread.join(Duration.ofSeconds(5))) {
LOG.warnv("Docker log-driver accept thread did not stop after closing its listener");
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.debugv("Interrupted while closing the Docker log-driver listener: {0}", e.getMessage());
} catch (IOException e) {
LOG.debugv("Could not close the Docker log-driver listener: {0}", e.getMessage());
}
@@ -5,6 +5,7 @@ import com.github.dockerjava.api.model.LogConfig;
import com.github.dockerjava.api.model.StreamType;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.testing.ParallelLocks;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
@@ -13,12 +14,18 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import static org.awaitility.Awaitility.await;
@@ -118,6 +125,64 @@ class GelfLogListenerTest {
assertFalse(restarted.listensAt(null));
}
@Test
void closeWaitsForTheAcceptThreadToReleaseItsSocket() throws Exception {
CountDownLatch exiting = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
AtomicReference<Throwable> workerFailure = new AtomicReference<>();
ThreadFactory threads = task -> Thread.ofVirtual().unstarted(() -> {
task.run();
exiting.countDown();
try {
release.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
workerFailure.set(e);
}
});
restarted = new GelfLogListener(mock(DockerClient.class), true, "127.0.0.1", 0, 8, threads);
restarted.ensureStarted();
Thread closing = Thread.ofVirtual().start(restarted::close);
try {
assertTrue(exiting.await(5, TimeUnit.SECONDS));
assertEquals(Thread.State.TIMED_WAITING, ThreadStates.awaitParkedOrDone(closing),
"close waits for the accept worker, not only ServerSocket.close");
} finally {
release.countDown();
assertTrue(closing.join(Duration.ofSeconds(5)));
}
assertNull(workerFailure.get());
}
@Test
void aPendingAcceptReleasesThePortBeforeCloseReturns() throws Exception {
AtomicReference<Thread> accepting = new AtomicReference<>();
ThreadFactory threads = task -> {
Thread thread = Thread.ofVirtual().unstarted(task);
accepting.set(thread);
return thread;
};
restarted = new GelfLogListener(mock(DockerClient.class), true, "127.0.0.1", 0, 8, threads);
String address = restarted.ensureStarted();
int port = Integer.parseInt(address.substring(address.lastIndexOf(':') + 1));
assertEquals(Thread.State.WAITING, ThreadStates.awaitParkedOrDone(accepting.get()));
restarted.close();
assertFalse(accepting.get().isAlive(), "accept has returned and released the bound descriptor");
try (ServerSocket replacement = new ServerSocket(port, 8, InetAddress.getLoopbackAddress())) {
assertEquals(port, replacement.getLocalPort());
}
}
@Test
void aPortOwnedByAnotherListenerIsNotClaimed() throws IOException {
String address = listener.ensureStarted();
int port = Integer.parseInt(address.substring(address.lastIndexOf(':') + 1));
restarted = new GelfLogListener(mock(DockerClient.class), true, "127.0.0.1", port, 8);
assertFalse(restarted.listensAt("tcp://" + address));
assertFalse(restarted.started());
assertTrue(listener.listensAt("tcp://" + address));
}
// Catches: the log configuration not pointing the daemon at the listener it opened.
@Test
void theLogConfigSendsGelfOverTcpToTheListener() throws Exception {