Skip to content

The headless browser for AI agents and web scraping

fix(cdp): cap glibc malloc arenas so memory does not track peak concurrency

sycatledc79fde1a0cc
diff --git a/Cargo.lock b/Cargo.lock
index 0fbe273a..aff873d6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1664,6 +1664,7 @@ dependencies = [
"anyhow",
"base64",
"futures-util",
+ "libc",
"obscura-browser",
"obscura-dom",
"obscura-js",
diff --git a/crates/obscura-cdp/Cargo.toml b/crates/obscura-cdp/Cargo.toml
index 973dc003..38e3551f 100644
--- a/crates/obscura-cdp/Cargo.toml
+++ b/crates/obscura-cdp/Cargo.toml
@@ -20,6 +20,9 @@ futures-util = "0.3"
base64 = "0.22"
obscura-net = { workspace = true }
+[target.'cfg(target_env = "gnu")'.dependencies]
+libc = "0.2"
+
[dev-dependencies]
tokio-tungstenite = { workspace = true }
futures-util = "0.3"
diff --git a/crates/obscura-cdp/src/server.rs b/crates/obscura-cdp/src/server.rs
index 06e76dae..21907d65 100644
--- a/crates/obscura-cdp/src/server.rs
+++ b/crates/obscura-cdp/src/server.rs
@@ -241,6 +241,8 @@ pub async fn start_with_full_serve_options(
// dropping one runtime here does the one-time setup single-threaded.
drop(obscura_js::runtime::ObscuraJsRuntime::new());
+ cap_malloc_arenas();
+
// Accept loop: hand each WebSocket connection to its own OS thread so its
// pages' isolates live on a dedicated thread.
loop {
@@ -272,6 +274,49 @@ pub async fn start_with_full_serve_options(
Ok(())
}
+/// Cap the number of per-thread malloc arenas glibc will create.
+///
+/// glibc hands each new thread its own 64 MiB arena (up to 8x cores). With one
+/// thread per connection that is the dominant per-connection memory term:
+/// measured with `reliability/conn-scale.py`, 100 connections each running JS
+/// reserve 90.0 GiB of address space uncapped and 83.5 GiB capped, and 100 idle
+/// connections go from 65 MiB of reserved address space per connection to
+/// 2.0 MiB.
+///
+/// For scale: at the same 100-connection JS workload `main` (one shared
+/// isolate) reserves 83.6 GiB, so with the cap this server is level with it on
+/// address space. Most of that total is V8's process-wide sandbox, which `main`
+/// pays too as soon as it runs any JS at all.
+///
+/// The resident-set effect matters more than the reservation: freed chunks stay
+/// in their arena rather than returning to the OS, so RSS tracks the *peak*
+/// number of concurrent connections and never comes back down, which reads as a
+/// leak. Measured in the container image against Google Maps, four concurrent
+/// connections per round: 350 / 619 / 826 MiB over three rounds uncapped and
+/// still climbing linearly, versus 166 / 235 / 269 MiB capped, on a
+/// decelerating curve.
+///
+/// Two arenas cost no measurable throughput here (8 concurrent connections x 12
+/// navigations: 1.53s uncapped, 1.50s capped): V8 allocates the JS heap through
+/// its own allocator, and the Rust side is dominated by network I/O rather than
+/// malloc traffic. Only `serve` calls this, and it owns the process. Respects a
+/// caller-set `MALLOC_ARENA_MAX`.
+fn cap_malloc_arenas() {
+ #[cfg(target_env = "gnu")]
+ {
+ if std::env::var_os("MALLOC_ARENA_MAX").is_some() {
+ return;
+ }
+ // M_ARENA_MAX is not exported by the libc crate.
+ const M_ARENA_MAX: libc::c_int = -8;
+ // SAFETY: mallopt is thread-safe; called once here before any
+ // connection thread exists.
+ if unsafe { libc::mallopt(M_ARENA_MAX, 2) } != 1 {
+ warn!("mallopt(M_ARENA_MAX) failed; memory will scale with peak concurrency");
+ }
+ }
+}
+
/// Run one WebSocket connection on its own OS thread: a `current_thread` tokio
/// runtime + `LocalSet` hosting this connection's `cdp_processor` (with its own
/// `CdpContext` and pages) and its frame reader. Confining a connection's pages