Hi, we're using ktor 2.1.3 (had a bunch of issues ...
# ktor
Hi, we're using ktor 2.1.3 (had a bunch of issues trying to upgrade to 2.2.x) and have a ktor service that hits another external rest api using ktor client. After the service has been running for about 24 hours, all the ktor client requests start failing and continue to fail with: io.ktor.client.network.sockets.ConnectTimeoutException: Connect timeout has expired [url=<<REDACTED>>, connect_timeout=unknown ms] at io.ktor.client.plugins.HttpTimeoutKt.ConnectTimeoutException(HttpTimeout.kt:209) at io.ktor.client.plugins.HttpTimeoutKt.ConnectTimeoutException$default(HttpTimeout.kt:206) at io.ktor.client.engine.cio.Endpoint.getTimeoutException(Endpoint.kt:217) at io.ktor.client.engine.cio.Endpoint.connect(Endpoint.kt:206) at io.ktor.client.engine.cio.Endpoint.access$connect(Endpoint.kt:23) at io.ktor.client.engine.cio.Endpoint$connect$1.invokeSuspend(Endpoint.kt) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:33) at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:102) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) If we restart the service all the errors go away. Has anyone seen this? Thanks, Brent
Do you close the connection after every request? You can do this automatically by using the HttpClient with Kotlins .use function
hmmm no not explicitly - I'll try that - ty
Then I'm positive that's the problem, I had the same issue a while ago with the server using more and more resources.
Per https://ktor.io/docs/create-client.html#use-client it's to be used when we're done using the client. In the case of the service - it's never really "done" so it seems like something is leaking
It also says
Note that creating HttpClient is not a cheap operation, and it's better to reuse its instance in the case of multiple requests.
@ESchouten are you creating a new
on every request in the service?
Personally, yes. But we're not making requests that often. When we forgot closing the connections the resource usage crept up slowly over the span of several days/weeks before crashing
@Brent Beardsley Did you ever figure this out?
We switched from CIO to OkHttp and it’s working great. So either there’s a bug in the CIO implementation or how we’re using it.