I have a problem with interrupted streaming in kto...
# ktor
m
I have a problem with interrupted streaming in ktor and
java.net.SocketException: Software caused connection abort
(thread)
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
                    emit(progress)
                    offset += currentRead
                } while (!channel.isClosedForRead)
                writeSink.close()
            }

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

        } finally {
            writeSink?.close()
            hashingSink?.close()
        }
Now if it starts streaming and then I disable Internet on device (android) it stops with
java.net.SocketException
- this is expected and fine
However all consecutive requests made with that
HttpClient
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
r
m
sure