Skip to content

fix: stop false critical memory alerts from file cache in Linux containers - #208754

Merged
obviyus merged 3 commits into
mainfrom
fix/memory-sampler-reclaimable-cache
Nov 30, 2026
Merged

obviyus merged 3 commits into
mainfrom
fix/memory-sampler-reclaimable-cache

Conversation

@obviyus

@obviyus obviyus commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #208631

What Problem This Solves

A Gateway that runs in a memory-limited Linux container or systemd unit (cgroup v2) counts file cache as used memory. Normal file work fills the cgroup with cache that the kernel frees on demand, so the Gateway logs memory pressure: level=critical reason=process_memory_threshold every 5 minutes and never clears. The same false signal publishes openclaw.memory.critical, which retires idle SQLite workers and clears session caches, defers optional work, refuses extra automation runs, and starts no update-hash workers, while most of the limit is free.

User Impact

  • Cache-heavy Gateways in containers and memory-limited units no longer report critical pressure or throw away warm workers and caches when only reclaimable file cache fills the limit.
  • Real pressure still reports: anonymous memory, active file cache and kernel charges still count. A charge above memory.high still reports no headroom.
  • Bare hosts, macOS and Bun keep their readings. cgroup v1 and hosts without a visible cgroup v2 leaf keep Node's reading (unchanged; see Limits).

Why This Change Was Made

Root cause: the leaf cgroup's headroom was limit - memory.current, and memory.current includes page cache. It was computed in two places, both raw: Node's native process.availableMemory() (libuv reads the leaf's memory.current) consumed at src/logging/diagnostic-memory.ts:371 and src/infra/host-memory.ts:62,305,310, and OpenClaw's delegated-mount reader readDelegatedCgroupMemory (src/infra/host-memory.ts:286, "Node counts raw charges, including file cache"). The same module already used the working set (memory.current - inactive_file) for ancestor cgroups and the lean worker budget.

The fix gives src/infra/host-memory.ts one reader, readProcessMemory(), for the process limit and the headroom left in it:

  • With a verified cgroup v2 view (ordinary or delegated mount), it reads the leaf files directly: headroom = limit - (memory.current - inactive_file). The charge is read unclipped, so above memory.high the headroom is 0, not cache credit added to libuv's clipped 0.
  • This is the working set that kubelet eviction and cAdvisor use (Kubernetes node-pressure eviction, cgroup v2 memory interface).
  • An ordinary leaf without memory counters (the root cgroup) and every non-cgroup-v2 case keep Node's native reading.
  • All four consumers read it: diagnostic pressure and the openclaw.memory.critical channel, isMemoryPressureHigh, cron's extra-run admission, and update-hash worker sizing. The delegated-only and cron-only capture readers are removed. The cgroup view is mapped once per membership (the existing isMemoryPressureHigh cache, now shared).
  • Cron shares the definition on purpose: it asks whether 1 GiB is available to a new run, and inactive cache is given back on demand. Earlier changes kept the raw leaf only to match Node (Fix Gateway pressure checks on delegated Linux cgroups #206308, fix(cron): use the service's free memory for extra automation runs #206322).

Supersedes #208658 (draft), which corrected only the diagnostic signal.

Evidence

Red on base, green on fix. The fix's unit fixtures (1000-byte leaf, memory.current 950, inactive_file 945) run against base 7215944 with the base API: on both ordinary (/) and delegated (/pod) mount roots, isMemoryPressureHigh() returned true and openclaw.memory.critical was published. With the fix: no pressure, no retirement. With anonymous memory holding the same charge (inactive_file 50): critical, usedBytes: 900, retirement published. Above memory.high (high 512 MiB, current 768 MiB, inactive 128 MiB): headroom 0.

E2E on real cgroup v2 (Linux 7.2 kernel, Docker --memory=2g --memory-swap=2g), an isolated Gateway from the repo Dockerfile, built from base 7215944 and from this branch. Numbers from the container's memory.stat:

Phase Base Fix
Idle (anon ~0.85 GB) no critical no critical
File cache fills the limit (memory.current 2.14 GB, inactive_file 1.24-1.26 GB, anon 0.80-0.88 GB) level=critical reason=process_memory_threshold ... thresholdRatio=111.1% limit=2 GiB used=2 GiB no critical
Anonymous memory to 93% working set (anon 2.11 GB, inactive_file 17 MB) (already critical, throttled) level=critical reason=process_memory_threshold ... thresholdRatio=103.9% limit=2 GiB used=1.87 GiB

A 1 GiB limit OOM-kills a stock Gateway during startup (RSS 963 MB at ready), so the runs use 2 GiB. The fix column was rerun on the PR's product code (f8371b6; later commits change tests only) with the same results.

The other consumers, called from the built dist of each image:

Case Base Fix
2 GiB leaf full of file cache (inactive_file 2.14 GB, anon 0.4 MB) cron headroom 0.00 GiB (no extra run), isMemoryPressureHigh() true cron headroom 1.98 GiB, isMemoryPressureHigh() false
Leaf without memory controller files (0::/nomem) host reading 50.75 GiB, false host reading 50.70 GiB, false

Cost: on that kernel, the leaf reads take 13.1 µs per call vs 8.5 µs for the native pair; the pressure check runs once per second.

Checks: host-memory, diagnostic-memory, worker-pool-sizing, cron delegated/cross-tick/run-queue admission, page-cache, runtime-services maintenance and cron maintenance tests pass. Core typecheck (source and tests) and changed-file lint pass. check:changed also reports plugin-boundary and dead-export failures in files this change does not touch.

Limits

  • Active file cache still counts as used, as in kubelet. On this kernel (MGLRU on), re-reading the same file kept it in inactive_file (active 5 MB of 1.27 GB). Not tested on a classic-LRU kernel, where re-read cache can move to active_file and still count.
  • cgroup v1 keeps Node's raw reading; OpenClaw already does not detect v1 limits.
  • Unchanged here: libuv's soft rlimits still lower the reported limit while headroom stays cgroup-wide, and a tighter parent limit is still checked only by isMemoryPressureHigh's ancestor check.

obviyus and others added 2 commits November 30, 2026 08:37
- readProcessMemory in host-memory owns process limit and headroom; a cgroup v2 leaf uses memory.current minus inactive_file
- diagnostic pressure, optional-work deferral, cron extra-run admission and update hash sizing read it
- remove the delegated-only and cron-only capture readers

Co-authored-by: Sleep8h <[email protected]>
- an ordinary cgroup v2 leaf with no memory files (the root cgroup) keeps libuv's host reading instead of unknown headroom
- reduce the diagnostic regression to one case
@clawsweeper

clawsweeper Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Pull request received.

ClawSweeper review complete

ClawSweeper finished reviewing this revision.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime size: M labels Nov 30, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. labels Nov 30, 2026
@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Nov 30, 2026
@obviyus
obviyus merged commit 2fdd401 into main Nov 30, 2026
130 checks passed
@obviyus
obviyus deleted the fix/memory-sampler-reclaimable-cache branch November 30, 2026 03:36
@obviyus

obviyus commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator Author

Landed. In a memory-limited cgroup v2 container or unit, the Gateway counted reclaimable file cache as used memory, so it logged critical pressure every 5 minutes, retired warm workers, deferred optional work and refused extra automation runs. readProcessMemory() in src/infra/host-memory.ts now owns the process headroom for all four consumers, using the cgroup working set (memory.current - inactive_file). On a real cgroup v2 kernel, a Gateway with a cache-full 2 GiB limit stayed quiet, and anonymous memory at 93% still raised critical pressure.

Thanks @garrytan for the clear report and reproduction, and @LetMeSleep8h for #208658.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 30, 2026
…iners (openclaw#208754)

Fixes openclaw#208631

## What Problem This Solves

A Gateway that runs in a memory-limited Linux container or systemd unit (cgroup v2) counts file cache as used memory. Normal file work fills the cgroup with cache that the kernel frees on demand, so the Gateway logs `memory pressure: level=critical reason=process_memory_threshold` every 5 minutes and never clears. The same false signal publishes `openclaw.memory.critical`, which retires idle SQLite workers and clears session caches, defers optional work, refuses extra automation runs, and starts no update-hash workers, while most of the limit is free.

## User Impact

- Cache-heavy Gateways in containers and memory-limited units no longer report critical pressure or throw away warm workers and caches when only reclaimable file cache fills the limit.
- Real pressure still reports: anonymous memory, active file cache and kernel charges still count. A charge above `memory.high` still reports no headroom.
- Bare hosts, macOS and Bun keep their readings. cgroup v1 and hosts without a visible cgroup v2 leaf keep Node's reading (unchanged; see Limits).

## Why This Change Was Made

Root cause: the leaf cgroup's headroom was `limit - memory.current`, and `memory.current` includes page cache. It was computed in two places, both raw: Node's native `process.availableMemory()` (libuv reads the leaf's `memory.current`) consumed at `src/logging/diagnostic-memory.ts:371` and `src/infra/host-memory.ts:62,305,310`, and OpenClaw's delegated-mount reader `readDelegatedCgroupMemory` (`src/infra/host-memory.ts:286`, "Node counts raw charges, including file cache"). The same module already used the working set (`memory.current - inactive_file`) for ancestor cgroups and the lean worker budget.

The fix gives `src/infra/host-memory.ts` one reader, `readProcessMemory()`, for the process limit and the headroom left in it:

- With a verified cgroup v2 view (ordinary or delegated mount), it reads the leaf files directly: headroom = limit - (`memory.current` - `inactive_file`). The charge is read unclipped, so above `memory.high` the headroom is 0, not cache credit added to libuv's clipped 0.
- This is the working set that kubelet eviction and cAdvisor use ([Kubernetes node-pressure eviction](https://kubernetes.io/docs/concepts/scheduling-eviction/node-pressure-eviction/#active-file-memory-is-not-considered-as-available-memory), [cgroup v2 memory interface](https://docs.kernel.org/admin-guide/cgroup-v2.html#memory-interface-files)).
- An ordinary leaf without memory counters (the root cgroup) and every non-cgroup-v2 case keep Node's native reading.
- All four consumers read it: diagnostic pressure and the `openclaw.memory.critical` channel, `isMemoryPressureHigh`, cron's extra-run admission, and update-hash worker sizing. The delegated-only and cron-only capture readers are removed. The cgroup view is mapped once per membership (the existing `isMemoryPressureHigh` cache, now shared).
- Cron shares the definition on purpose: it asks whether 1 GiB is available to a new run, and inactive cache is given back on demand. Earlier changes kept the raw leaf only to match Node (openclaw#206308, openclaw#206322).

Supersedes openclaw#208658 (draft), which corrected only the diagnostic signal.

## Evidence

Red on base, green on fix. The fix's unit fixtures (1000-byte leaf, `memory.current` 950, `inactive_file` 945) run against base 3f68ec0 with the base API: on both ordinary (`/`) and delegated (`/pod`) mount roots, `isMemoryPressureHigh()` returned true and `openclaw.memory.critical` was published. With the fix: no pressure, no retirement. With anonymous memory holding the same charge (`inactive_file` 50): `critical`, `usedBytes: 900`, retirement published. Above `memory.high` (high 512 MiB, current 768 MiB, inactive 128 MiB): headroom 0.

E2E on real cgroup v2 (Linux 7.2 kernel, Docker `--memory=2g --memory-swap=2g`), an isolated Gateway from the repo Dockerfile, built from base 3f68ec0 and from this branch. Numbers from the container's `memory.stat`:

| Phase | Base | Fix |
| --- | --- | --- |
| Idle (anon ~0.85 GB) | no critical | no critical |
| File cache fills the limit (`memory.current` 2.14 GB, `inactive_file` 1.24-1.26 GB, anon 0.80-0.88 GB) | `level=critical reason=process_memory_threshold ... thresholdRatio=111.1% limit=2 GiB used=2 GiB` | no critical |
| Anonymous memory to 93% working set (anon 2.11 GB, `inactive_file` 17 MB) | (already critical, throttled) | `level=critical reason=process_memory_threshold ... thresholdRatio=103.9% limit=2 GiB used=1.87 GiB` |

A 1 GiB limit OOM-kills a stock Gateway during startup (RSS 963 MB at ready), so the runs use 2 GiB. The fix column was rerun on the PR's product code (f8371b6; later commits change tests only) with the same results.

The other consumers, called from the built `dist` of each image:

| Case | Base | Fix |
| --- | --- | --- |
| 2 GiB leaf full of file cache (`inactive_file` 2.14 GB, anon 0.4 MB) | cron headroom 0.00 GiB (no extra run), `isMemoryPressureHigh()` true | cron headroom 1.98 GiB, `isMemoryPressureHigh()` false |
| Leaf without memory controller files (`0::/nomem`) | host reading 50.75 GiB, false | host reading 50.70 GiB, false |

Cost: on that kernel, the leaf reads take 13.1 µs per call vs 8.5 µs for the native pair; the pressure check runs once per second.

Checks: `host-memory`, `diagnostic-memory`, `worker-pool-sizing`, cron delegated/cross-tick/run-queue admission, page-cache, runtime-services maintenance and cron maintenance tests pass. Core typecheck (source and tests) and changed-file lint pass. `check:changed` also reports plugin-boundary and dead-export failures in files this change does not touch.

## Limits

- Active file cache still counts as used, as in kubelet. On this kernel (MGLRU on), re-reading the same file kept it in `inactive_file` (active 5 MB of 1.27 GB). Not tested on a classic-LRU kernel, where re-read cache can move to `active_file` and still count.
- cgroup v1 keeps Node's raw reading; OpenClaw already does not detect v1 limits.
- Unchanged here: libuv's soft rlimits still lower the reported limit while headroom stays cgroup-wide, and a tighter parent limit is still checked only by `isMemoryPressureHigh`'s ancestor check.

Co-authored-by: Sleep8h <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clawsweeper:intake:bug Preliminary submission classification; full source review pending. docs Improvements or additions to documentation gateway Gateway runtime impact:other This issue has meaningful maintainer-visible impact outside the owned taxonomy. P2 Normal backlog priority with limited blast radius. size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Linux cgroup v2: reclaimable page cache raises level=critical process_memory_threshold and publishes memory.critical every sample

1 participant