reqwest: Potential memory leak in ClientBuilder or rustls
I seem to have some memory leak related to reqwest clients

This is my code:
async fn request_well_known(
globals: &crate::database::globals::Globals,
destination: &str,
) -> Option<String> {
let body: serde_json::Value = serde_json::from_str(
&globals
.reqwest_client()
.ok()?
.build()
.ok()?
.get(&format!(
"https://{}/.well-known/matrix/server",
destination
))
.send()
.await
.ok()?
.text()
.await
.ok()?,
)
.ok()?;
Some(body.get("m.server")?.as_str()?.to_owned())
}
And here is my reqwest_client function:
pub fn reqwest_client(&self) -> Result<reqwest::ClientBuilder> {
let mut reqwest_client_builder = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(30))
.timeout(Duration::from_secs(60 * 3))
.pool_max_idle_per_host(1);
// Proxy not used currently
if let Some(proxy) = self.config.proxy.to_proxy()? {
reqwest_client_builder = reqwest_client_builder.proxy(proxy);
}
Ok(reqwest_client_builder)
}
I don’t reuse the same client because in some other code I need to use a slightly different client and manipulate the builder before calling build(). In that other code I use builder.resolve()
So it looks like reqwest/rustls put certificates into a global store and don’t clean it up. The high amount of ram usage it takes suggests that it stores the same certs multiple times
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 17 (1 by maintainers)
It looks like switching to jemalloc completely fixes this on both rustls-tls and default-tls!
#dependencies tikv-jemalloc-ctl = { version = “0.4.2”, features = [‘use_std’] } tikv-jemallocator = { version = “0.4.1”, features = [‘unprefixed_malloc_on_supported_platforms’] }
#main.rs #[global_allocator] static GLOBAL: Jemalloc = Jemalloc;