fix(core): replay a Docker request after a lost response only when it is safe to repeat (#5171)

* fix(core): replay a Docker request after a lost response only when it is safe to repeat

The transport retry replayed every control-plane request except exec calls
and an exclusion list (restart, network connect/disconnect, network create,
unnamed container create), so other mutating calls were still replayed after
a lost response: commit made a second image, pause, unpause and kill got a
409 for a state that had already changed, and rename hit a name conflict.

The policy is now an allowlist: GET, HEAD, DELETE (a replay answers 404,
which callers tolerate) and PUT archive uploads as before, and POST only for
start, stop, wait, image pull, volume create and a named container create.
Every other POST gets one attempt, including any call added later.

* fix(core): replay a volume create only when it names the volume

An unnamed volume create replayed after a lost response makes a second
volume and orphans the first; a named one returns the existing volume.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-06 07:08:30 -04:00
1 parent df958117bb
commit 957d49255a
2 files changed
+126 -38

No files matched your search

@@ -287,6 +287,84 @@ class RetryingDockerHttpClientTest {
}
}
@Test
void doesNotRetryMutatingPostsOutsideTheAllowlist() {
// Catches: replaying a commit (a second image), a pause, unpause or kill (a 409 for a state
// that already changed) or a rename (a name conflict) after a lost response. The exclusion
// list this replaced named none of them.
for (String path : List.of("/commit?container=abc&repo=img", "/containers/abc/pause",
"/containers/abc/unpause", "/containers/abc/kill?signal=SIGKILL",
"/containers/abc/rename?name=new-name")) {
FakeTransport delegate = new FakeTransport(attempt -> {
throw brokenPipe();
});
RetryingDockerHttpClient client = new RetryingDockerHttpClient(delegate, MAX_ATTEMPTS, 0L);
Request request = Request.builder()
.method(Request.Method.POST)
.path(path)
.build();
RuntimeException thrown = assertThrows(RuntimeException.class, () -> client.execute(request));
assertEquals("Broken pipe", thrown.getCause().getMessage());
assertEquals(1, delegate.calls.get(), path);
}
}
@Test
void retriesTheAllowlistedPosts() {
// Catches: an allowlist too narrow to keep the replays that are safe: a start or stop (304
// on replay), a wait and an image pull.
for (String path : List.of("/containers/abc/start", "/containers/abc/stop?t=10",
"/containers/abc/wait", "/images/create?fromImage=alpine&tag=3")) {
Response ok = mock(Response.class);
FakeTransport delegate = new FakeTransport(attempt -> {
if (attempt == 1) {
throw brokenPipe();
}
return ok;
});
RetryingDockerHttpClient client = new RetryingDockerHttpClient(delegate, MAX_ATTEMPTS, 0L);
Request request = Request.builder()
.method(Request.Method.POST)
.path(path)
.build();
assertSame(ok, client.execute(request), path);
assertEquals(2, delegate.calls.get(), path);
}
}
@Test
void retriesOnlyANamedVolumeCreate() {
// Catches: replaying an unnamed volume create, which makes a second volume and orphans the
// first; a named one is safe because docker returns the existing volume.
assertEquals(2, volumeCreateAttempts("{\"Name\":\"floci-aws-data\",\"Labels\":{}}"));
assertEquals(1, volumeCreateAttempts("{\"Labels\":{}}"));
assertEquals(1, volumeCreateAttempts("{\"Name\":\"\"}"));
assertEquals(1, volumeCreateAttempts(null));
}
private static int volumeCreateAttempts(String json) {
Response ok = mock(Response.class);
FakeTransport delegate = new FakeTransport(attempt -> {
if (attempt == 1) {
throw brokenPipe();
}
return ok;
});
RetryingDockerHttpClient client = new RetryingDockerHttpClient(delegate, MAX_ATTEMPTS, 0L);
Request.Builder builder = Request.builder().method(Request.Method.POST).path("/volumes/create");
if (json != null) {
builder.bodyBytes(json.getBytes(StandardCharsets.UTF_8));
}
try {
client.execute(builder.build());
} catch (RuntimeException expected) {
// a refused replay surfaces the first attempt's failure; only the attempt count matters
}
return delegate.calls.get();
}
@Test
void retriesNamedContainerCreate() {
// Catches: over-broad exclusion that stops replaying named creates, which are safe