ktor: java.net.SocketTimeoutException with no lines referencing my code

Ktor Version and Engine Used (client or server and name)

  • io.ktor:ktor-client-core:1.2.3-rc
  • io.ktor:ktor-client-core-jvm:1.2.3-rc
  • io.ktor:ktor-client-okhttp:1.2.3-rc

Describe the bug See the exception below. This exception is reported by Firebase. I can not reproduce it myself although quite some users appear to get this bug. I have no idea where this happens in my code and that is why I am unable to fix it myself. It looks like some sort of timeout error.

Fatal Exception: java.net.SocketTimeoutException: timeout
       at okio.SocketAsyncTimeout.newTimeoutException + 159(SocketAsyncTimeout.java:159)
       at okio.AsyncTimeout.exit$jvm + 203(AsyncTimeout.java:203)
       at okio.AsyncTimeout$source$1.read + 163(AsyncTimeout.java:163)
       at okio.RealBufferedSource.read + 41(RealBufferedSource.java:41)
       at okhttp3.internal.http1.Http1ExchangeCodec$AbstractSource.read + 352(Http1ExchangeCodec.java:352)
       at okhttp3.internal.http1.Http1ExchangeCodec$UnknownLengthSource.read + 488(Http1ExchangeCodec.java:488)
       at okhttp3.internal.connection.Exchange$ResponseBodySource.read + 279(Exchange.java:279)
       at okio.RealBufferedSource.read + 41(RealBufferedSource.java:41)
       at okhttp3.internal.cache.CacheInterceptor$cacheWritingResponse$cacheWritingSource$1.read + 158(CacheInterceptor.java:158)
       at okio.RealBufferedSource.read + 148(RealBufferedSource.java:148)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke + 97(OkHttpEngineKt.java:97)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke(OkHttpEngineKt.java:1)
       at kotlinx.coroutines.io.ByteBufferChannel.write + 1508(ByteBufferChannel.java:1508)
       at kotlinx.coroutines.io.ByteWriteChannel$DefaultImpls.write$default + 84(ByteWriteChannel.java:84)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1.invokeSuspend + 96(OkHttpEngineKt.java:96)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith + 33(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run + 241(DispatchedTask.java:241)
       at java.util.concurrent.ThreadPoolExecutor.runWorker + 1167(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run + 641(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run + 764(Thread.java:764)
Caused by java.net.SocketTimeoutException: Read timed out
       at java.net.SocketInputStream.socketRead0(SocketInputStream.java)
       at java.net.SocketInputStream.socketRead + 119(SocketInputStream.java:119)
       at java.net.SocketInputStream.read + 176(SocketInputStream.java:176)
       at java.net.SocketInputStream.read + 144(SocketInputStream.java:144)
       at okio.InputStreamSource.read + 102(InputStreamSource.java:102)
       at okio.AsyncTimeout$source$1.read + 159(AsyncTimeout.java:159)
       at okio.RealBufferedSource.read + 41(RealBufferedSource.java:41)
       at okhttp3.internal.http1.Http1ExchangeCodec$AbstractSource.read + 352(Http1ExchangeCodec.java:352)
       at okhttp3.internal.http1.Http1ExchangeCodec$UnknownLengthSource.read + 488(Http1ExchangeCodec.java:488)
       at okhttp3.internal.connection.Exchange$ResponseBodySource.read + 279(Exchange.java:279)
       at okio.RealBufferedSource.read + 41(RealBufferedSource.java:41)
       at okhttp3.internal.cache.CacheInterceptor$cacheWritingResponse$cacheWritingSource$1.read + 158(CacheInterceptor.java:158)
       at okio.RealBufferedSource.read + 148(RealBufferedSource.java:148)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke + 97(OkHttpEngineKt.java:97)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke(OkHttpEngineKt.java:1)
       at kotlinx.coroutines.io.ByteBufferChannel.write + 1508(ByteBufferChannel.java:1508)
       at kotlinx.coroutines.io.ByteWriteChannel$DefaultImpls.write$default + 84(ByteWriteChannel.java:84)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1.invokeSuspend + 96(OkHttpEngineKt.java:96)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith + 33(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run + 241(DispatchedTask.java:241)
       at java.util.concurrent.ThreadPoolExecutor.runWorker + 1167(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run + 641(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run + 764(Thread.java:764)

To Reproduce As the stacktrace shows no lines of my code I do not know how to reproduce it. I would really appreciate some help for this exception. I am not entirely sure if this is related to Ktor, but thought this would be the best place to report it. I have Kotlin Coroutines debug mode enabled as well.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 24 (7 by maintainers)

Most upvoted comments

We are also experiencing a similar problem (using 1.2.5):

java.net.SocketTimeoutException: 10,000 milliseconds timeout on connection http-outgoing-9 [ACTIVE]
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:387)
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92)
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39)
	at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:263)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:492)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:213)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
	at java.lang.Thread.run(Thread.java:748)`

I wrote a repro case using the current prerelease version 1.3-beta1 (1.2 has the same issue). Not using SocketTimeoutException but ConnectionRefused because it is easier to cause, but behaviour should be the same: https://github.com/luqasn/ktor-httpclient-exception-repro

We are encountering this issue too. We have been getting SocketTimeoutException most of the times, but have also seen things like UnknownHost, SSLHandShakeException, but it does no references to anything in our code is found, however our error stack traces are similar to what @Thomas-Vos is getting i.e. okio, okthttp3 etc. We are also using 1.2.3

@e5l We started seeing this when my team upgraded from 1.1.4 to 1.2.2. I think it’s reasonable that ktor may be the cause in some way, however it’s really difficult to tell for sure. My team hasn’t been able to reproduce it, however we’re starting to get negative reviews over this with people complaining in such a way that indicates that users can get the app in a state where none of their network requests can go through.

What’s also very interesting is that we only allow network requests in a separate module, and that separate module has a a built-in try catch around everything (using coroutines), which makes it very confusing for us that we’re seeing this crashing our application sometimes.

Users that encounter this, it seems like they end up with a completely unusable application, which makes this a BLOCKER bug for us.

I’ve encountered a similar issue but with

  • io.ktor:ktor-client-core:1.2.3
  • io.ktor:ktor-client-core-jvm:1.2.3
  • io.ktor:ktor-client-apache:1.2.3
java.net.SocketTimeoutException: 10,000 milliseconds timeout on connection http-outgoing-26 [ACTIVE]
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:381)
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92)
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39)
	at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:263)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:492)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:213)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588)
	at java.base/java.lang.Thread.run(Thread.java:834)

Thanks, I know that it is a timeout error. I already guessed it was thrown from client.get or similar, but it shows no lines of my code. I am using Ktor throughout my app so it is difficult to find the place where I forgot the catch this exception. Is there a way to get a more complete stacktrace?