Ktor: Operation is already in progress Exception

Created on 2 Jul 2019  路  5Comments  路  Source: ktorio/ktor

Ktor Version

1.2.2

Ktor Engine Used(client or server and name)

Client Android

JVM Version, Operating System and Relevant Context

1.8, reproductible on a pure JVM project, and on a Android application consuming this JVM project.

Feedback

Stacktrace:

Exception in thread "Thread-3 @ktor-android-context#4" java.lang.IllegalStateException: Operation is already in progress
    at kotlinx.coroutines.io.ByteBufferChannel.suspensionForSize(ByteBufferChannel.kt:2984)
    at kotlinx.coroutines.io.ByteBufferChannel.access$suspensionForSize(ByteBufferChannel.kt:21)
    at kotlinx.coroutines.io.ByteBufferChannel.readSuspendImpl(ByteBufferChannel.kt:2309)
    at kotlinx.coroutines.io.ByteBufferChannel.readSuspend(ByteBufferChannel.kt:2249)
    at kotlinx.coroutines.io.ByteBufferChannel.discardSuspend(ByteBufferChannel.kt:1723)
    at kotlinx.coroutines.io.ByteBufferChannel.discard(ByteBufferChannel.kt:1707)
    at kotlinx.coroutines.io.ByteReadChannelKt.discard(ByteReadChannel.kt:203)
    at io.ktor.client.response.HttpResponse$close$1.invokeSuspend(HttpResponse.kt:71)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

How to reproduce:

runBlocking {
            val client = HttpClient(Android) {
                install(JsonFeature) {
                    serializer = KotlinxSerializer()
                        .also {
                            it.register(JsonArraySerializer)
                        }
                }
            }

            repeat(100) {
                try {
                    withTimeout(500) {
                        client.request<JsonArray>("https://jsonplaceholder.typicode.com/todos/") {

                        }
                    }
                } catch (exception: TimeoutCancellationException) {
                    println(exception.message)
                }
            }
        }

From what I understand, it seems like if the timeout cancellation happens during the json deserialisation phase of the request, we get the Operation is already in progress exception. Because of that, it is not easy to reproduce: You might have to tweak the timeout value of 500, or run the test multiple times, for the exception to appear.

Is there a workaround in the meantime ?

bug

Most helpful comment

@e5l when is it supposed to be fixed? Or maybe there is some way to temporary work around the issue?

All 5 comments

@e5l when is it supposed to be fixed? Or maybe there is some way to temporary work around the issue?

Seems like 1.2.3-rc fixes this. I can no longer reproduce, but can you tell us if that is indeed the case ? Thank you 馃檹

@q-litzler In the release notes for 1.2.3-rc there is "Fixed client response body cancellation" so I would guess that's about this issue

@q-litzler In the release notes for 1.2.3-rc there is "Fixed client response body cancellation" so I would guess that's about this issue

Yes indeed, I also found this commit which looks related. Just making sure :)

Yep, sorry for the delay. Closed

Was this page helpful?
0 / 5 - 0 ratings