Hi all, another SocketException question. If I get...
# ktor
c
Hi all, another SocketException question. If I get this error
io.ktor.client.network.sockets.SocketTimeoutException: Socket timeout has expired [url=<http://192.168.4.1:3331/firmware_upgrade>, socket_timeout=unknown] ms
is this timeout from the client side or the server side? Here's the config
Copy code
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
    install(Logging) {
        level = LogLevel.ALL
        logger = Logger.DEFAULT
    }
    install(HttpTimeout) {
        requestTimeoutMillis = 60000
        connectTimeoutMillis = 60000
        socketTimeoutMillis = 60000

    }
    config(this)
    engine {
        // this: OkHttpConfig
        config {
            retryOnConnectionFailure(true)
        }
    }
}
Stacktrace
Copy code
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  io.ktor.client.network.sockets.SocketTimeoutException: Socket timeout has expired [url=<http://192.168.4.1:3331/firmware_upgrade>, socket_timeout=unknown] ms
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at io.ktor.client.plugins.HttpTimeoutKt.SocketTimeoutException(HttpTimeout.kt:239)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at io.ktor.client.engine.okhttp.OkUtilsKt.mapOkHttpException(OkUtils.kt:80)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at io.ktor.client.engine.okhttp.OkUtilsKt.access$mapOkHttpException(OkUtils.kt:1)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at io.ktor.client.engine.okhttp.OkHttpCallback.onFailure(OkUtils.kt:39)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:525)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2025-03-03 11:18:37.201 19051-25449 System.err              com.company.test         W  	at java.lang.Thread.run(Thread.java:923)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  Caused by: java.net.SocketTimeoutException: timeout
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:146)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.AsyncTimeout.access$newTimeoutException(AsyncTimeout.kt:186)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:390)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:455)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:348)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:178)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:106)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:79)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	... 3 more
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  Caused by: java.net.SocketException: Socket closed
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at java.net.SocketInputStream.read(SocketInputStream.java:209)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at java.net.SocketInputStream.read(SocketInputStream.java:144)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.InputStreamSource.read(JvmOkio.kt:93)
2025-03-03 11:18:37.202 19051-25449 System.err              com.company.test         W  	at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:153)
a
It's difficult to say what is the root cause. According to the stack trace, the
SocketTimeoutException
was thrown because the socket had been closed.
c
Hi @Aleksei Tirman [JB] Thanks for responding! I discussed it with our hardware team to check the logs on their end and see if the socket is getting closed on the server side and it looks like the socket is getting closed by the client after 10 seconds. Is there a possible misconfiguration on my end for this to occur on client side despite the
socketTimeout
override?
a
I don't see any misconfiguration in your setup — the read and write timeouts of 60 seconds are configured for the underlying OkHttp client.
c
Oh interesting! Can I override the read and write timeouts by the OkHttp Client? or is there another engine with a longer timeout?
a
The read and write timeouts of the underlying OkHttp client are automatically set to the value of
socketTimeoutMillis
from the HttpTimeout plugin.
c
so then in theory, this should resolve the issue? or is there a max to the timeout?
Copy code
install(HttpTimeout) {
        requestTimeoutMillis = 5 * 60 * 1000 // 5 minutes
        connectTimeoutMillis = 2 * 60 * 1000 // 2 minutes
        socketTimeoutMillis = 5 * 60 * 1000  // 5 minutes
    }
a
Yes, it should.
c
Sounds good! I'll give this a try and let you know!
This works but inconsistently. Like sometimes I would get the sockettimeout even without changing the payload. Anything I'm missing?