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 15835779..1ba0ae38 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 @@ -4,6 +4,7 @@ import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Types import de.gesellix.docker.response.JsonChunksReader import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flow @@ -17,6 +18,7 @@ import okio.Socket import okio.appendingSink import okio.buffer import java.io.File +import java.io.IOException import java.io.InputStream import java.lang.reflect.Type import java.nio.file.Files @@ -37,6 +39,26 @@ fun Response?.isTcpUpgrade(): Boolean { this.header("Upgrade")?.contains("tcp", ignoreCase = true) ?: false } +/** + * Runs a blocking read via runInterruptible on Dispatchers.IO, so that cancelling the collecting coroutine + * (e.g. a withTimeout() or an explicit Cancellable.cancel()) interrupts the thread blocked in the read. + * + * Socket implementations report such an interrupt as an IOException (e.g. InterruptedIOException for + * named pipes, ClosedByInterruptException for channels) instead of the InterruptedException + * runInterruptible translates to a cancellation. When the coroutine has been cancelled, that IOException + * is only a consequence of the cancellation, so we report the cancellation instead of a failed read. + */ +@PublishedApi +internal suspend fun readInterruptible(read: () -> T): T = + withContext(Dispatchers.IO) { + try { + runInterruptible { read() } + } catch (e: IOException) { + ensureActive() + throw e + } + } + fun ResponseBody?.consumeFile(): File? { if (this == null) { return null @@ -71,8 +93,8 @@ inline fun ResponseBody?.consumeStream(mediaType: String?): F // 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) } } + while (readInterruptible { reader.hasNext() }) { + val next = readInterruptible { reader.readNext(T::class.java) } emit(next) } this@consumeStream.closeQuietly() @@ -98,8 +120,8 @@ fun Socket?.consumeFrames(mediaType: String?): Flow { // 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) } } + while (readInterruptible { reader.hasNext() }) { + val next = readInterruptible { reader.readNext(Frame::class.java) } emit(next) } source.closeQuietly() @@ -124,8 +146,8 @@ fun ResponseBody?.consumeFrames(mediaType: String?): Flow { val reader = FrameReader(source(), mediaType) val events = flow { // 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) } } + while (readInterruptible { reader.hasNext() }) { + val next = readInterruptible { reader.readNext(Frame::class.java) } emit(next) } this@consumeFrames.closeQuietly()