Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.net.SocketTimeoutException with no lines referencing my code #1263

Closed
Thomas-Vos opened this issue Aug 3, 2019 · 24 comments
Closed

java.net.SocketTimeoutException with no lines referencing my code #1263

Thomas-Vos opened this issue Aug 3, 2019 · 24 comments
Assignees
Labels
ux User Experience issue

Comments

@Thomas-Vos
Copy link
Contributor

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.

@Thomas-Vos Thomas-Vos added the bug label Aug 3, 2019
@Thomas-Vos Thomas-Vos changed the title java.net.SocketTimeoutException java.net.SocketTimeoutException with no lines referencing my code Aug 3, 2019
@cy6erGn0m
Copy link
Contributor

java.net.SocketTimeoutException: Read timed out is quite usual error then a peer is not responding for too long due to service outage or poor network connection. In this case such exception could be rethrown from a ktor client, for example from client.get<...>("...")

@Thomas-Vos
Copy link
Contributor Author

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?

@e5l e5l self-assigned this Aug 5, 2019
@e5l e5l added ux User Experience issue and removed bug labels Aug 5, 2019
@Dominaezzz
Copy link

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)

@santoshastagi
Copy link

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

@ScottPierce
Copy link

ScottPierce commented Aug 26, 2019

@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.

@fpoppinga
Copy link

fpoppinga commented Sep 20, 2019

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

java.net.SocketTimeoutException: 10,000 milliseconds timeout on connection http-outgoing-24 [ACTIVE]
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:381) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92) ~[httpasyncclient-4.1.4.jar:4.1.4]
	at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39) ~[httpasyncclient-4.1.4.jar:4.1.4]
	at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:263) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:492) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:213) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588) ~[httpcore-nio-4.4.10.jar:4.4.10]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92-internal]

Interestingly, the time difference between us starting the request and the SocketTimeoutException being thrown is much less than 10000ms.

@fpoppinga
Copy link

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

[...]
Interestingly, the time difference between us starting the request and the SocketTimeoutException being thrown is much less than 10000ms.

Actually this turned out to be not true.

The issue in our case was indeed a socket timeout exception towards a very slow backend. So our mitigation was just to override the corresponding timeouts of the Engine with higher values.

@luqasn
Copy link

luqasn commented Sep 27, 2019

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

@santoshastagi
Copy link

@e5l I saw the following in 1.2.5 release notes: "Fixed Android crash on client and server exceptions" . Is that referencing the fix for this problem?

@Thomas-Vos
Copy link
Contributor Author

Just updated to Ktor 1.2.5 and got a reported crash with a different stack trace. I think it is the same issue. Does this give someone any ideas what could be going wrong?

Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:355)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
       at java.util.concurrent.FutureTask.run(FutureTask.java:271)
       at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:784)
Caused by java.util.concurrent.CancellationException: Channel closed due to job failure
       at kotlinx.coroutines.io.ByteBufferChannel$attachJob$1.invoke(ByteBufferChannel.java:66)
       at kotlinx.coroutines.io.ByteBufferChannel$attachJob$1.invoke(ByteBufferChannel.java:21)
       at -$$LambdaGroup$ks$Dri4zzrjtMazMq0ziurFDak3Bng.invoke(-.java:14)
       at kotlinx.coroutines.InvokeOnCancelling.invoke(InvokeOnCancelling.java:1398)
       at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.java:1437)
       at kotlinx.coroutines.JobSupport.tryMakeCompletingSlowPath(JobSupport.java:843)
       at kotlinx.coroutines.JobSupport.tryMakeCompleting(JobSupport.java:811)
       at kotlinx.coroutines.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.java:787)
       at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.java:111)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:46)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:241)
       at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:784)
Caused by java.net.SocketException: Software caused connection abort
       at java.net.SocketInputStream.socketRead0(SocketInputStream.java)
       at java.net.SocketInputStream.socketRead(SocketInputStream.java:119)
       at java.net.SocketInputStream.read(SocketInputStream.java:176)
       at java.net.SocketInputStream.read(SocketInputStream.java:144)
       at okio.InputStreamSource.read(InputStreamSource.java:102)
       at okio.AsyncTimeout$source$1.read(AsyncTimeout.java:159)
       at okio.RealBufferedSource.read(RealBufferedSource.java:41)
       at okhttp3.internal.http1.Http1ExchangeCodec$AbstractSource.read(Http1ExchangeCodec.java:352)
       at okhttp3.internal.http1.Http1ExchangeCodec$UnknownLengthSource.read(Http1ExchangeCodec.java:488)
       at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.java:279)
       at okio.RealBufferedSource.read(RealBufferedSource.java:41)
       at okhttp3.internal.cache.CacheInterceptor$cacheWritingResponse$cacheWritingSource$1.read(CacheInterceptor.java:158)
       at okio.RealBufferedSource.read(RealBufferedSource.java:148)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke(OkHttpEngineKt.java:103)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1$1$1.invoke(OkHttpEngineKt.java:1)
       at kotlinx.coroutines.io.ByteBufferChannel.write(ByteBufferChannel.java:1508)
       at kotlinx.coroutines.io.ByteWriteChannel$DefaultImpls.write$default(ByteWriteChannel.java:84)
       at io.ktor.client.engine.okhttp.OkHttpEngineKt$toChannel$1.invokeSuspend(OkHttpEngineKt.java:102)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:241)
       at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:784)

@willinwu
Copy link

willinwu commented Dec 19, 2019

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)`

@haneev
Copy link
Contributor

haneev commented Dec 23, 2019

@willinwu Do you use NewRelic by any chance? We encounter the same issue, but our suspicion is that the newrelic-agent is also listening as a failed handler in the apache client.

@InsanusMokrassar
Copy link
Contributor

Is there some progress on this issue? I have faced with the same problem with this environment:

Ktor: 1.3.0
Ktor Client OkHttp: 1.3.0
Kotlin Coroutines Core: 1.3.3

Exact exception:

Exception in thread "pool-3-thread-31" java.net.SocketTimeoutException: timeout
  at okio.Okio$4.newTimeoutException(Okio.java:232)
  at okio.AsyncTimeout.exit(AsyncTimeout.java:286)
  at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
  at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
  at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
  at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242)
  at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213)
  at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115)
  at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
  at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
  at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)
  at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Socket closed
  at java.net.SocketInputStream.read(SocketInputStream.java:203)
  at java.net.SocketInputStream.read(SocketInputStream.java:141)
  at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
  at sun.security.ssl.InputRecord.read(InputRecord.java:503)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
  at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
  at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
  at okio.Okio$2.read(Okio.java:140)
  at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
  ... 24 more

@mobiletoly
Copy link

mobiletoly commented Feb 13, 2020

Same here (ktor 1.2.5):

java.util.concurrent.CancellationException: Fail to execute request
	at kotlinx.coroutines.ExceptionsKt.CancellationException(Exceptions.kt:33)
	at io.ktor.client.engine.apache.ApacheResponseConsumer.failed(ApacheResponseConsumer.kt:118)
	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.executionFailed(DefaultClientExchangeHandlerImpl.java:99)
	at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.failed(AbstractClientExchangeHandler.java:426)
	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.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: 180,000 milliseconds timeout on connection http-outgoing-21 [ACTIVE]
	... 11 common frames omitted

@mobiletoly
Copy link

Had to rollback from 1.2.5 way back to 1.2.1 to get rid of this nasty blocking issue

@mobiletoly
Copy link

mobiletoly commented Feb 14, 2020

Interesting, when I removed
header(HttpHeaders.Connection, "close")
no more CancellationException timeouts (at least for Apache engine).

@hardysim
Copy link

hardysim commented Mar 25, 2020

Looks like I have a similar problem:

I'm setting requestTimeoutMillis but receive a SocketTimeoutException when I set it to a value longer than 10 seconds (and I get the exception after 10 seconds). Shorter timeouts will produce the correct HttpRequestTimeoutException.

HttpClient: REQUEST https://testserver/sleep?sec=30 failed with exception: io.ktor.network.sockets.SocketTimeoutException: Socket timeout has been expired [url=https://testserver/sleep?sec=30, socket_timeout=unknown] ms

So it looks like socketTimeoutMillis is set somewhere (but I'm not setting it). I'm using a preconfigured OkHttpClient but I'm not setting any timeouts on this either. The docs say, all timeouts are infinite as default.

I'm using ktor 1.3.2.


Setting socketTimeoutMillis as well fixes the problem. But how can I set it to infinite as intended?

@oleg-larshin
Copy link

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

@e5l
Copy link
Member

e5l commented Apr 5, 2021

Can't reproduce with 1.5.2

@e5l e5l closed this as completed Apr 5, 2021
@Merseyside
Copy link

Merseyside commented Oct 28, 2021

The problem still reproducing on 1.6.2

io.ktor.network.sockets.SocketTimeoutException: Socket timeout has expired [url=https://rest-dev.izibook.ru:10003
    at io.ktor.client.features.HttpTimeoutKt.SocketTimeoutException(HttpTimeout.kt:207)
    at io.ktor.client.engine.okhttp.OkUtilsKt.mapOkHttpException(OkUtils.kt:79)
    at io.ktor.client.engine.okhttp.OkUtilsKt.access$mapOkHttpException(OkUtils.kt:1)
    at io.ktor.client.engine.okhttp.OkHttpCallback.onFailure(OkUtils.kt:39)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:510)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
Caused by: java.net.SocketTimeoutException: timeout
    at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.kt:677)
    at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.kt:686)
    at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.kt:143)
    at okhttp3.internal.http2.Http2ExchangeCodec.readResponseHeaders(Http2ExchangeCodec.kt:96)
    at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:106)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:79)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502)
	... 3 more

@Stexxe
Copy link
Contributor

Stexxe commented Oct 28, 2021

@Merseyside could you please share a code snippet here to reproduce it?

@sherwinzadeh
Copy link

For me, SocketTimeoutException was a misleading exception. It had nothing to do with timeout at all but it seems like if my server is returning a 401, Ktor will throw this exception. It may be a bug in Ktor but I'm not sure.

@rmyhal
Copy link

rmyhal commented Oct 3, 2023

I started seeing the same issue when I manually added a header to my request, like:

client.get(endpoint) {
  parameter("param1", param1)
  header("apikey", apikey)
}

But it was wrong because it overrides all headers with only one. When I changed it to this:

client.get(endpoint) {
  parameter("param1", param1)
  headers {
    append("apikey", apiKey)
  }
}

The issue disappeared and I stopped getting io.ktor.client.network.sockets.SocketTimeoutException: Socket timeout has expired

@gandrewstone
Copy link

In ktor multiplatform 2.3.9, you can get a platform-specific java.net.SocketTimeoutException during readAvailable or awaitContent if you have configured your socket with a timeout like this:

val tmp = aSocket(selectorManager).tcp().connect(name, port) {
                        socketTimeout = TIMEOUT
                    }

since this is multiplatform code, I recommend you catch all exceptions and then check the input socket via "isClosedForRead".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ux User Experience issue
Projects
None yet
Development

No branches or pull requests