Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(docker): do not retry a call when the Docker socket does not exist (#5424)

Aymen JAMAAOUIdd6bf4cfcbb7
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerRetry.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerRetry.java
index fdb53754b9..f784d68af0 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerRetry.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerRetry.java
@@ -73,7 +73,8 @@ public final class DockerRetry {
/**
* True when {@code t} (or any cause in its chain) is a transient docker I/O failure worth
* retrying, an {@link IOException} such as {@code Broken pipe} / {@code Connection reset},
- * possibly wrapped by docker-java in a {@link RuntimeException}.
+ * possibly wrapped by docker-java in a {@link RuntimeException}. A docker socket file that is
+ * missing, or that this process may not open, is never transient.
*/
public static boolean isTransientIo(Throwable t) {
// InterruptedIOException covers both a genuine thread interruption (masking it would
@@ -94,6 +95,9 @@ public final class DockerRetry {
if (c instanceof LocalFailure) {
return false;
}
+ if (isUnusableSocket(c)) {
+ return false;
+ }
if (c.getCause() == c) {
break;
}
@@ -120,6 +124,22 @@ public final class DockerRetry {
return false;
}
+ /**
+ * True when {@code t} reports that the docker socket file does not exist ({@code No such
+ * file or directory}), as when Floci runs without a mounted docker socket, or that this
+ * process may not open it ({@code Permission denied}). A retry cannot create the file or
+ * change the process's permissions, so retrying only delays the failure: six attempts held
+ * Floci's startup, and each docker call of its shutdown, for about 15 seconds.
+ */
+ private static boolean isUnusableSocket(Throwable t) {
+ String m = t.getMessage();
+ if (m == null) {
+ return false;
+ }
+ String lower = m.toLowerCase(Locale.ROOT);
+ return lower.contains("no such file or directory") || lower.contains("permission denied");
+ }
+
/**
* Runs {@code call}, retrying up to {@code maxAttempts} times on a transient docker I/O
* error ({@link #isTransientIo}) with a capped exponential backoff between attempts
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerRetryTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerRetryTest.java
index 0c436335ba..85fee89401 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerRetryTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerRetryTest.java
@@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InterruptedIOException;
+import java.net.BindException;
+import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
@@ -174,4 +176,24 @@ class DockerRetryTest {
assertTrue(DockerRetry.isTransientIo(new RuntimeException("BROKEN PIPE")));
assertTrue(DockerRetry.isTransientIo(new RuntimeException("Broken Pipe")));
}
+
+ @Test
+ void isTransientIo_missingDockerSocket_isNotTransient() {
+ // Floci started without a mounted docker socket: a retry cannot create the file.
+ assertFalse(DockerRetry.isTransientIo(new SocketException("No such file or directory")));
+ assertFalse(DockerRetry.isTransientIo(
+ new RuntimeException(new SocketException("No such file or directory"))));
+ assertFalse(DockerRetry.isTransientIo(
+ new IOException("Broken pipe", new SocketException("NO SUCH FILE OR DIRECTORY"))));
+ }
+
+ @Test
+ void isTransientIo_dockerSocketPermissionDenied_isNotTransient() {
+ // The socket file exists but this process may not open it: a retry cannot change that.
+ assertFalse(DockerRetry.isTransientIo(new BindException("Permission denied")));
+ assertFalse(DockerRetry.isTransientIo(
+ new RuntimeException(new BindException("Permission denied"))));
+ assertFalse(DockerRetry.isTransientIo(
+ new IOException("Broken pipe", new SocketException("PERMISSION DENIED"))));
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/RetryingDockerHttpClientTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/RetryingDockerHttpClientTest.java
index 43b266275c..7c70aea51d 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/RetryingDockerHttpClientTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/RetryingDockerHttpClientTest.java
@@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.BindException;
+import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
@@ -408,6 +410,34 @@ class RetryingDockerHttpClientTest {
assertEquals(1, rejectedDelegate.calls.get());
}
+ @Test
+ void execute_missingDockerSocket_failsWithoutRetry() {
+ RuntimeException missingSocket = new RuntimeException(new SocketException("No such file or directory"));
+ FakeTransport delegate = new FakeTransport(attempt -> {
+ throw missingSocket;
+ });
+ RetryingDockerHttpClient client = new RetryingDockerHttpClient(delegate, MAX_ATTEMPTS, 0L);
+ Request list = Request.builder().method(Request.Method.GET).path("/containers/json").build();
+
+ RuntimeException thrown = assertThrows(RuntimeException.class, () -> client.execute(list));
+ assertSame(missingSocket, thrown);
+ assertEquals(1, delegate.calls.get(), "a missing docker socket must not be retried");
+ }
+
+ @Test
+ void execute_dockerSocketPermissionDenied_failsWithoutRetry() {
+ RuntimeException denied = new RuntimeException(new BindException("Permission denied"));
+ FakeTransport delegate = new FakeTransport(attempt -> {
+ throw denied;
+ });
+ RetryingDockerHttpClient client = new RetryingDockerHttpClient(delegate, MAX_ATTEMPTS, 0L);
+ Request inspect = Request.builder().method(Request.Method.GET).path("/volumes/data").build();
+
+ RuntimeException thrown = assertThrows(RuntimeException.class, () -> client.execute(inspect));
+ assertSame(denied, thrown);
+ assertEquals(1, delegate.calls.get(), "a docker socket this process may not open must not be retried");
+ }
+
@Test
void producerWrapsControlPlaneTransportOnly() {
DockerHttpClient raw = new FakeTransport(attempt -> null);