Ktor 2.0 HttpClient connection pool memory leak fix: Is the CIO engine fully leak‑free after 2.0.1?
0 reputation · 30 Jan 2024, 12:23 UTC
0 reputation · 30 Jan 2024, 12:23 UTC
Determine whether the CIO engine’s connection pool in Ktor 2.0.1 eliminates all coroutine context and ByteBuffer leaks observed in 2.0.0 under high‑concurrency workloads.
Use the default CIO engine, issue thousands of short‑lived requests, and monitor JVM heap for retained coroutine contexts and pooled ByteBuffers. The test environment must not use experimental plugins or custom engine configurations that could mask leaks.
HttpClient.close() in 2.3.x affect the cleanup semantics for connections that were still in use when the client is closed?No. The CIO engine connection-pool leak observed in Ktor 2.0.0 is fixed in 2.0.1 for the default engine configuration, but the engine is not absolutely leak-free. The pool itself no longer retains coroutine contexts or pooled ByteBuffers under normal request completion, cancellation or timeout. Leaks can still appear if user code retains references to HttpResponse or its body beyond the request lifecycle.
Under the constraints you specified — default CIO, thousands of short-lived requests, no custom plugins — heap growth of coroutine contexts and pooled ByteBuffers should return to baseline after GC. The likely cause of any remaining growth is user retention, e.g., storing HttpResponse, readChannel or body bytes in a collection, logging the whole response, or keeping a reference across coroutines.
Edge cases such as large payloads or keep-alive timeouts are not documented as pool leaks in 2.0.1 for the default configuration. Applications that keep the HttpClient open for long periods with keep-alive should still monitor pool size and idle timeout settings to avoid gradual memory growth from idle connections.
The suspend-function HttpClient.close() introduced in 2.3.x improves graceful shutdown semantics for in-flight connections. It does not retroactively change 2.0.1 cleanup, and in 2.0.1 closing the client should be done after in-flight requests are finished or cancelled.
One diagnostic detail that changes the recommendation: are you retaining HttpResponse or body references beyond the use-site? If yes, the leak is application-side and requires explicit consume-and-drop of the body.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.