fix(proxy): default the periodic allocator trim on for glibc Linux too

The trim task shipped for #2820 defaulted on only for macOS, leaving glibc
Linux opt-in via HEADROOM_MALLOC_TRIM=1. glibc retains freed pages the
same way, and on a many-core host its per-thread arenas multiply the
high-water mark: the 100 GB RSS report above was a Linux box with the trim
off. Default on wherever a trim call exists (darwin, linux); platforms
without one already disable the task themselves.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-09-22 22:35:06 +02:00
co-authored by Claude Fable 5.1
parent f06e95a1dd
commit ba5f99d771
3 changed files with 17 additions and 11 deletions
+9 -6
View File
@@ -470,12 +470,15 @@ class ProxyConfig:
# Periodic allocator trim. Long-lived proxies processing large concurrent
# request bodies ratchet RSS through freed-but-retained allocator pages;
# this returns them to the OS (malloc_zone_pressure_relief on macOS,
# malloc_trim on glibc). Default-on only on macOS, where the retained-page
# ratchet is the documented failure (#2820); an opt-in elsewhere via
# HEADROOM_MALLOC_TRIM=1 so glibc deployments do not silently take on a
# once-a-minute allocator purge they did not ask for. Envs:
# HEADROOM_MALLOC_TRIM=0/1, HEADROOM_MALLOC_TRIM_INTERVAL_SECONDS.
periodic_malloc_trim_enabled: bool = field(default_factory=lambda: sys.platform == "darwin")
# malloc_trim on glibc). Default-on where a trim call exists: macOS, where
# the retained-page ratchet was first documented (#2820), and glibc Linux,
# which ratchets the same way (a coding-agent session took one proxy to
# 100 GB RSS on a 128 GB host with the trim off). Platforms without a trim
# call disable the task themselves. Envs: HEADROOM_MALLOC_TRIM=0/1,
# HEADROOM_MALLOC_TRIM_INTERVAL_SECONDS.
periodic_malloc_trim_enabled: bool = field(
default_factory=lambda: sys.platform in ("darwin", "linux")
)
malloc_trim_interval_seconds: int = 60
# Stateless mode — disable all filesystem writes for read-only / container deployments
+1 -1
View File
@@ -5710,7 +5710,7 @@ def _proxy_config_from_env() -> ProxyConfig:
http_proxy=os.environ.get("HEADROOM_HTTP_PROXY") or None,
periodic_toin_stats_enabled=_get_env_bool("HEADROOM_PERIODIC_TOIN_STATS", True),
periodic_malloc_trim_enabled=_get_env_bool(
"HEADROOM_MALLOC_TRIM", sys.platform == "darwin"
"HEADROOM_MALLOC_TRIM", sys.platform in ("darwin", "linux")
),
malloc_trim_interval_seconds=_get_env_int("HEADROOM_MALLOC_TRIM_INTERVAL_SECONDS", 60),
proxy_token=os.environ.get("HEADROOM_PROXY_TOKEN") or None,
+7 -4
View File
@@ -276,16 +276,19 @@ async def test_slow_trim_does_not_stop_unrelated_async_work(monkeypatch):
# --------------------------------------------------------------------------- #
# ProxyConfig wiring
# --------------------------------------------------------------------------- #
def test_proxy_config_malloc_trim_default_is_darwin_scoped(monkeypatch):
# Default-on only on macOS (the platform with the documented RSS ratchet);
# elsewhere it is opt-in, so glibc deployments do not silently take on a
# once-a-minute allocator purge.
def test_proxy_config_malloc_trim_default_is_scoped_to_platforms_with_a_trim_call(monkeypatch):
# Default-on on macOS and glibc Linux, the two platforms with a trim call
# and a documented RSS ratchet; elsewhere the periodic task is a no-op, so
# the default stays off rather than scheduling wakeups for nothing.
from headroom.proxy import models
monkeypatch.setattr(models.sys, "platform", "darwin")
assert models.ProxyConfig().periodic_malloc_trim_enabled is True
monkeypatch.setattr(models.sys, "platform", "linux")
assert models.ProxyConfig().periodic_malloc_trim_enabled is True
monkeypatch.setattr(models.sys, "platform", "win32")
assert models.ProxyConfig().periodic_malloc_trim_enabled is False
# The interval knob is platform-independent.