Ktor: java.lang.IllegalStateException: Reading is not available in state Reading

Created on 21 Dec 2018  路  9Comments  路  Source: ktorio/ktor

Ktor Version

1.0.1

Ktor Engine Used(client or server and name)

Netty / Firefox

JVM Version, Operating System and Relevant Context

1.8 Windows Server 2008R2

Feedback

I don't how this happen, I can't reproduce it.

stacktrace:

500 Internal Server Error: POST - /search/TT
java.lang.IllegalStateException: Reading is not available in state Reading
        at kotlinx.coroutines.io.internal.ReadWriteBufferState.startReading$kotlinx_coroutines_io_jvm(ReadWriteBufferSta
te.kt:19)
        at kotlinx.coroutines.io.ByteBufferChannel.setupStateForRead(ByteBufferChannel.kt:238)
        at kotlinx.coroutines.io.ByteBufferChannel.access$setupStateForRead(ByteBufferChannel.kt:21)
        at kotlinx.coroutines.io.ByteBufferChannel.lookAhead(ByteBufferChannel.kt:2868)
        at kotlinx.coroutines.io.DelimitedKt.readUntilDelimiter(Delimited.kt:24)
        at io.ktor.http.cio.MultipartKt.copyUntilBoundary(Multipart.kt:339)
        at io.ktor.http.cio.MultipartKt$copyUntilBoundary$1.invokeSuspend(Multipart.kt)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlinx.coroutines.DispatchedContinuation.resumeWith(Dispatched.kt:111)
        at kotlinx.coroutines.io.internal.CancellableReusableContinuation.resumeWith(CancellableReusableContinuation.kt:
83)
        at kotlinx.coroutines.io.ByteBufferChannel.resumeWriteOp(ByteBufferChannel.kt:2207)
        at kotlinx.coroutines.io.ByteBufferChannel.bytesRead(ByteBufferChannel.kt:865)
        at kotlinx.coroutines.io.ByteBufferChannel.readAsMuchAsPossible(ByteBufferChannel.kt:426)
        at kotlinx.coroutines.io.ByteBufferChannel.readAvailable(ByteBufferChannel.kt:559)
        at kotlinx.coroutines.io.ByteBufferChannel.readAvailableSuspend(ByteBufferChannel.kt:593)
        at kotlinx.coroutines.io.ByteBufferChannel$readAvailableSuspend$2.invokeSuspend(ByteBufferChannel.kt)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.lang.Thread.run(Thread.java:748)
bug

All 9 comments

Hi @kobe2000, thanks for the report.
It's probably a bug in kotlinx.io.

I'm seeing this bug in ktor client 1.1.3 on Android with OkHttp as the engine:

2019-03-14 12:53:57.109 2002-2002 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rally.coach.internal.debug, PID: 2002
    java.lang.IllegalStateException: Reading is not available in state Reading
        at kotlinx.coroutines.io.internal.ReadWriteBufferState.startReading$kotlinx_coroutines_io(ReadWriteBufferState.kt:19)
        at kotlinx.coroutines.io.ByteBufferChannel.setupStateForRead(ByteBufferChannel.kt:238)
        at kotlinx.coroutines.io.ByteBufferChannel.access$setupStateForRead(ByteBufferChannel.kt:21)
        at kotlinx.coroutines.io.ByteBufferChannel.readAsMuchAsPossible(ByteBufferChannel.kt:2573)
        at kotlinx.coroutines.io.ByteBufferChannel.readAsMuchAsPossible$default(ByteBufferChannel.kt:436)
        at kotlinx.coroutines.io.ByteBufferChannel.readRemaining(ByteBufferChannel.kt:2161)
        at kotlinx.coroutines.io.ByteReadChannelKt.readRemaining(ByteReadChannel.kt:175)
        at io.ktor.client.response.HttpResponseKt.readText(HttpResponse.kt:75)
        at io.ktor.client.response.HttpResponseKt.readText$default(HttpResponse.kt:74)
        at com.rally.coach.api.service.utils.ApiExceptionUtilKt.toApiException(ApiExceptionUtil.kt:18)
        at com.rally.coach.api.BaseApi.withApiContext(BaseApi.kt:46)
        at com.rally.coach.api.BaseApi$withApiContext$1.invokeSuspend(Unknown Source:32)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:285)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I posted this bug in kotlinx.io here: https://github.com/Kotlin/kotlinx-io/issues/43

@e5l same here: according to the stacktrace

        at kotlinx.coroutines.io.ByteBufferChannel.lookAhead(ByteBufferChannel.kt:2868)
        at kotlinx.coroutines.io.DelimitedKt.readUntilDelimiter(Delimited.kt:24)

readUntilDelimiter does enter lookAhead and fails immediately because we already have reading state. I see no way how any read operation could leave reading state unreleased without exceptions that usually kill everything.

Hi, I met this error again recently, here is the stacktrace. The ktor version is 1.1.3
1

Hi, I upgraded ktor to 1.2.2. But this issue exists still. The client is commons-httpclient. The server OS is CentOS7. After this exeption happens, all clients will be blocked forever(without timeout settings). Stacktrace is:

java.lang.IllegalStateException: Reading is not available in state Reading
  at kotlinx.coroutines.io.internal.ReadWriteBufferState.startReading$kotlinx_coroutines_io(ReadWriteBufferState.kt:19)
  at kotlinx.coroutines.io.ByteBufferChannel.setupStateForRead(ByteBufferChannel.kt:238)
  at kotlinx.coroutines.io.ByteBufferChannel.access$setupStateForRead(ByteBufferChannel.kt:21)
  at kotlinx.coroutines.io.ByteBufferChannel.lookAhead(ByteBufferChannel.kt:2874)
  at kotlinx.coroutines.io.DelimitedKt.readUntilDelimiter(Delimited.kt:24)
  at io.ktor.http.cio.MultipartKt.copyUntilBoundary(Multipart.kt:346)
  at io.ktor.http.cio.MultipartKt$copyUntilBoundary$1.invokeSuspend(Multipart.kt)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
  at kotlinx.coroutines.DispatchedContinuation.resumeWith(Dispatched.kt:108)
  at kotlinx.coroutines.io.internal.CancellableReusableContinuation.resumeWith(CancellableReusableContinuation.kt:83)
  at kotlinx.coroutines.io.ByteBufferChannel.resumeWriteOp(ByteBufferChannel.kt:2210)
  at kotlinx.coroutines.io.ByteBufferChannel.bytesRead(ByteBufferChannel.kt:865)
  at kotlinx.coroutines.io.ByteBufferChannel.readAsMuchAsPossible(ByteBufferChannel.kt:426)
  at kotlinx.coroutines.io.ByteBufferChannel.readAvailable(ByteBufferChannel.kt:559)
  at kotlinx.coroutines.io.ByteBufferChannel.readAvailableSuspend(ByteBufferChannel.kt:593)
  at kotlinx.coroutines.io.ByteBufferChannel$readAvailableSuspend$2.invokeSuspend(ByteBufferChannel.kt)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
  at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
  at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
  at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
  at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
  at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
  at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
  at java.lang.Thread.run(Thread.java:748)

@cy6erGn0m Please take a look, thanks

For me, readUntilDelimiter(buffer: ByteBuffer); is working once, then the function becomes non blocking and always returns 0

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Fixed since 1.5.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisjenx picture chrisjenx  路  4Comments

baruchn picture baruchn  路  3Comments

guenhter picture guenhter  路  4Comments

PatrickLemke picture PatrickLemke  路  3Comments

dedward3 picture dedward3  路  4Comments