From edd767cb64f96c22a4a288921253cfc58110c790 Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Thu, 3 Dec 2026 22:51:29 +0100 Subject: [PATCH 1/3] Make blocking stream reads cancellable consumeStream and both consumeFrames flows called the blocking hasNext()/readNext() directly. Coroutine cancellation (withTimeout, Cancellable.cancel()) only takes effect at a suspension point, which never comes while a native read blocks, so attach/exec timeouts were not enforced and could hang for minutes on Windows named pipes. Run the blocking calls via runInterruptible on Dispatchers.IO, so that cancellation interrupts the blocked thread. Co-Authored-By: Claude Opus 5.5 --- .../remote/api/core/ResponseConsumer.kt | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/api-client/src/main/kotlin/de/gesellix/docker/remote/api/core/ResponseConsumer.kt b/api-client/src/main/kotlin/de/gesellix/docker/remote/api/core/ResponseConsumer.kt index 2af59b53..15835779 100644 --- a/api-client/src/main/kotlin/de/gesellix/docker/remote/api/core/ResponseConsumer.kt +++ b/api-client/src/main/kotlin/de/gesellix/docker/remote/api/core/ResponseConsumer.kt @@ -3,9 +3,12 @@ package de.gesellix.docker.remote.api.core import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Types import de.gesellix.docker.response.JsonChunksReader +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.runInterruptible +import kotlinx.coroutines.withContext import okhttp3.Request import okhttp3.Response import okhttp3.ResponseBody @@ -63,8 +66,13 @@ inline fun ResponseBody?.consumeStream(mediaType: String?): F "application/jsonl" -> { val reader = JsonChunksReader(source(), Serializer.moshi) val events = flow { - while (reader.hasNext()) { - val next = reader.readNext(T::class.java) + // The blocking calls run via runInterruptible on Dispatchers.IO so that cancelling this flow's + // collector (e.g. a withTimeout() or an explicit Cancellable.cancel()) actually interrupts the + // thread blocked in the underlying read, rather than only taking effect at the next suspension + // point -- which never arrives if the read itself never returns. JsonChunksReader.hasNext() + // already checks Thread.currentThread().isInterrupted() for exactly this reason. + while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) { + val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(T::class.java) } } emit(next) } this@consumeStream.closeQuietly() @@ -87,8 +95,11 @@ fun Socket?.consumeFrames(mediaType: String?): Flow { ApiClient.DockerRawStreamMediaType -> { val reader = FrameReader(source, mediaType) val events = flow { - while (reader.hasNext()) { - val next = reader.readNext(Frame::class.java) + // See the comment in consumeStream(): runInterruptible+Dispatchers.IO makes cancellation + // (withTimeout(), Cancellable.cancel()) actually interrupt the thread blocked in the underlying + // socket read, which FrameReader.hasNext() already anticipates via its interrupted-flag check. + while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) { + val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } } emit(next) } source.closeQuietly() @@ -112,8 +123,9 @@ fun ResponseBody?.consumeFrames(mediaType: String?): Flow { ApiClient.DockerRawStreamMediaType -> { val reader = FrameReader(source(), mediaType) val events = flow { - while (reader.hasNext()) { - val next = reader.readNext(Frame::class.java) + // See the comment in consumeStream()/Socket.consumeFrames() above. + while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) { + val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } } emit(next) } this@consumeFrames.closeQuietly() From 497254e19266567fea36e99caf60d26731460b01 Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Thu, 3 Dec 2026 23:12:52 +0100 Subject: [PATCH 2/3] Fix flaky/outdated Windows integration test checks - systemVersion: accept any API version >= 1.42 instead of a list that needs an update with every Docker release (failed on all Windows CI runs) - containerArchiveInfoGetAndPut: wait for the mkdir exec to finish before stopping the container, it could be stopped before the directory existed Co-Authored-By: Claude Opus 5.5 --- .../client/ContainerApiIntegrationTest.java | 21 +++++++++++++++++++ .../api/client/SystemApiIntegrationTest.java | 5 ++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/api-client/src/test/java/de/gesellix/docker/remote/api/client/ContainerApiIntegrationTest.java b/api-client/src/test/java/de/gesellix/docker/remote/api/client/ContainerApiIntegrationTest.java index 7494d93c..ce429a1e 100644 --- a/api-client/src/test/java/de/gesellix/docker/remote/api/client/ContainerApiIntegrationTest.java +++ b/api-client/src/test/java/de/gesellix/docker/remote/api/client/ContainerApiIntegrationTest.java @@ -13,6 +13,7 @@ import de.gesellix.docker.remote.api.ContainerUpdateResponse; import de.gesellix.docker.remote.api.EngineApiClient; import de.gesellix.docker.remote.api.ExecConfig; +import de.gesellix.docker.remote.api.ExecInspectResponse; import de.gesellix.docker.remote.api.ExecStartConfig; import de.gesellix.docker.remote.api.HostConfig; import de.gesellix.docker.remote.api.IdResponse; @@ -225,6 +226,9 @@ public void containerArchiveInfoGetAndPut() throws IOException { engineApiClient.getExecApi().execStart( containerExec.getId(), new ExecStartConfig(null, null, null)); + // execStart doesn't wait for the command to finish: without waiting, the container might be stopped + // before the directory exists, which happens on slow (e.g. Windows CI) machines. + awaitExecFinished(containerExec.getId(), Duration.ofSeconds(30)); // filesystem operations against a running Hyper-V container are not supported, // so we stop the container before using the archive container api @@ -239,6 +243,23 @@ public void containerArchiveInfoGetAndPut() throws IOException { removeContainer(engineApiClient, "container-archive-info-test"); } + private void awaitExecFinished(String execId, Duration timeout) { + long deadline = System.nanoTime() + timeout.toNanos(); + ExecInspectResponse execInspect = engineApiClient.getExecApi().execInspect(execId); + while (Boolean.TRUE.equals(execInspect.getRunning()) && System.nanoTime() < deadline) { + try { + Thread.sleep(200); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(e); + } + execInspect = engineApiClient.getExecApi().execInspect(execId); + } + assertFalse(Boolean.TRUE.equals(execInspect.getRunning()), "exec still running after " + timeout); + assertEquals(0, execInspect.getExitCode(), "exec failed"); + } + @Test public void containerInspect() { imageApi.imageCreate(testImage.getImageName(), null, null, testImage.getImageTag(), null, null, null, null, null); diff --git a/api-client/src/test/java/de/gesellix/docker/remote/api/client/SystemApiIntegrationTest.java b/api-client/src/test/java/de/gesellix/docker/remote/api/client/SystemApiIntegrationTest.java index f8274fc9..e8e51133 100644 --- a/api-client/src/test/java/de/gesellix/docker/remote/api/client/SystemApiIntegrationTest.java +++ b/api-client/src/test/java/de/gesellix/docker/remote/api/client/SystemApiIntegrationTest.java @@ -141,7 +141,10 @@ public void systemPingHead() { @Test public void systemVersion() { SystemVersion systemVersion = systemApi.systemVersion(); - assertTrue(asList("1.42", "1.43", "1.44", "1.45", "1.46", "1.47", "1.48", "1.49", "1.50", "1.51", "1.52", "1.53", "1.54").contains(systemVersion.getApiVersion())); + // any engine supporting at least our minimum API version, without having to list every newer release + String[] apiVersion = systemVersion.getApiVersion().split("\\."); + assertEquals("1", apiVersion[0], "unexpected API version " + systemVersion.getApiVersion()); + assertTrue(Integer.parseInt(apiVersion[1]) >= 42, "unexpected API version " + systemVersion.getApiVersion()); } static class SystemEventsCallback implements StreamCallback { From 96669de01c5624f46ecdaedb942fab5be85137e0 Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Thu, 3 Dec 2026 23:16:01 +0100 Subject: [PATCH 3/3] Bump de.gesellix:docker-filesocket to 2.0.202612032315 Named pipe fixes for WCOW (docker-client/docker-filesocket#526) and UnixSocket timeout/half-close delegation (docker-client/docker-filesocket#527). Co-Authored-By: Claude Opus 5.5 --- api-client/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api-client/build.gradle.kts b/api-client/build.gradle.kts index f8a936ab..5abe6f25 100644 --- a/api-client/build.gradle.kts +++ b/api-client/build.gradle.kts @@ -76,7 +76,7 @@ dependencies { implementation("org.apache.commons:commons-compress:1.28.0") // implementation("com.squareup.okhttp3:logging-interceptor:${libs.versions.okhttpVersionrange.get()}!!${libs.versions.okhttp.get()}") implementation("de.gesellix:docker-remote-api-model-1-44:2.0.202611282237") - implementation("de.gesellix:docker-filesocket:2.0.202611282235") + implementation("de.gesellix:docker-filesocket:2.0.202612032315") implementation(libs.slf4j) testImplementation("ch.qos.logback:logback-classic:${libs.versions.logbackVersionrange.get()}!!${libs.versions.logback.get()}")