I have a problem with interrupted streaming in kto...
# ktor
I have a problem with interrupted streaming in ktor and
java.net.SocketException: Software caused connection abort
This is how my streaming looks:
Copy code
var hashingSink: HashingSink? = null
        var writeSink: BufferedSink? = null

        try {

            hashingSink = HashingSink.md5(destinationFile.sink())
            writeSink = hashingSink.buffer()

            NetworkClient.client.get<HttpStatement>(url = Url(urlPath)).execute { response: HttpResponse ->
                var offset = 0
                val channel = response.receive<ByteReadChannel>()
                val contentLen = response.contentLength()?.toInt() ?: 0
                val byteBufferSize = 1024 * 100
                val buffer = ByteArray(byteBufferSize)
                do {
                    val currentRead = channel.readAvailable(buffer, 0, byteBufferSize)
                    if (currentRead <= 0) break
                    writeSink.write(buffer, 0, currentRead)
                    val progress = if (contentLen == 0) 0 else (offset / contentLen.toFloat()) * 100
                    offset += currentRead
                } while (!channel.isClosedForRead)

            val downloadChecksum = hashingSink.hash.hex()
            if (downloadChecksum != checksum) throw ChecksumException(downloadChecksum, checksum)

        } finally {
Now if it starts streaming and then I disable Internet on device (android) it stops with
- this is expected and fine
However all consecutive requests made with that
also fail with that exception
is it a bug in ktor or some issue in my code (e.g. not releasing everything properly)?
(ktor 1.6.0, kotlin 1.5.10)
The only solution I found so far is creating a new HttpClient for every call. I could probably only create new when the old one failed, but I don't have a good trigger for recreation and retrying of the call