Repository navigation
fix: stop false critical memory alerts from file cache in Linux containers - #208754
Merged
Merged
Conversation
- 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
Contributor
|
🦞👀 ClawSweeper review completeClawSweeper finished reviewing this revision. |
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. 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_thresholdevery 5 minutes and never clears. The same false signal publishesopenclaw.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
memory.highstill reports no headroom.Why This Change Was Made
Root cause: the leaf cgroup's headroom was
limit - memory.current, andmemory.currentincludes page cache. It was computed in two places, both raw: Node's nativeprocess.availableMemory()(libuv reads the leaf'smemory.current) consumed atsrc/logging/diagnostic-memory.ts:371andsrc/infra/host-memory.ts:62,305,310, and OpenClaw's delegated-mount readerreadDelegatedCgroupMemory(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.tsone reader,readProcessMemory(), for the process limit and the headroom left in it:memory.current-inactive_file). The charge is read unclipped, so abovememory.highthe headroom is 0, not cache credit added to libuv's clipped 0.openclaw.memory.criticalchannel,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 existingisMemoryPressureHighcache, now shared).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.current950,inactive_file945) run against base 7215944 with the base API: on both ordinary (/) and delegated (/pod) mount roots,isMemoryPressureHigh()returned true andopenclaw.memory.criticalwas published. With the fix: no pressure, no retirement. With anonymous memory holding the same charge (inactive_file50):critical,usedBytes: 900, retirement published. Abovememory.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'smemory.stat:memory.current2.14 GB,inactive_file1.24-1.26 GB, anon 0.80-0.88 GB)level=critical reason=process_memory_threshold ... thresholdRatio=111.1% limit=2 GiB used=2 GiBinactive_file17 MB)level=critical reason=process_memory_threshold ... thresholdRatio=103.9% limit=2 GiB used=1.87 GiBA 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
distof each image:inactive_file2.14 GB, anon 0.4 MB)isMemoryPressureHigh()trueisMemoryPressureHigh()false0::/nomem)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:changedalso reports plugin-boundary and dead-export failures in files this change does not touch.Limits
inactive_file(active 5 MB of 1.27 GB). Not tested on a classic-LRU kernel, where re-read cache can move toactive_fileand still count.isMemoryPressureHigh's ancestor check.