Repository navigation
fix(memory): exclude reclaimable file cache from cgroup process pressure - #208659
wangmiao0668000666 wants to merge 1 commit into
Conversation
On cgroup v2 a workload that touches files fills the cgroup with page cache until memory.current reaches the limit. The diagnostic sampler computed usedBytes as limit minus process.availableMemory(), so that reclaimable cache counted as used memory and published process_memory_threshold at level=critical every five minutes, plus openclaw.memory.critical, while anonymous memory was a few percent of the limit. The channel's subscribers retire idle SQLite workers and clear snapshot caches, so warm state was discarded while memory was plentiful. Subtract the leaf's inactive_file cache from the process charge before classification. Reuse the existing cgroup memory owner: factor the inactive_file read out of cgroupV2WorkingSetBytes and expose it as readReclaimableCgroupFileCacheBytes, which stays undefined unless the verified leaf exposes memory.stat, preserving the raw-charge fallback. Only read the cache once the raw charge reaches the warning ratio, so steady-state sampling keeps its single cgroup read. The scheduler's availability reader stays raw, so only the diagnostic classification moves; genuine anonymous pressure and the worker-retirement policy are unchanged. Fixes openclaw#208631.
|
🦞👀 ClawSweeper review completeClawSweeper finished reviewing this revision. |
|
Codex review: needs real behavior proof before merge. What this changesThe PR subtracts inactive file cache before classifying Linux cgroup process-memory pressure. Example: A 1 GiB cgroup contains 20 MiB of anonymous memory and 1004 MiB of inactive file cache.
Review scores
ProductKind: Bug fix · Worth it: Needs a maintainer decision · Fix scope: Partial Merge readiness⛔ Blocked before merge - 3 items remain This PR addresses a real false-alert problem that remains on main, but its subtraction can also suppress genuine memory pressure. The competing PR is still unmerged and does not yet justify closing this contribution. Reviewed head: Decision needed
Before merge
Findings
Tests
Agent review detailsHow this fits togetherThe Gateway diagnostic heartbeat converts process and cgroup memory readings into alerts and a diagnostics-channel signal consumed by idle-worker retirement and snapshot-cache cleanup. flowchart TD
A[Linux cgroup counters] --> B[Host memory reader]
C[Node memory APIs] --> D[Diagnostic sampler]
B --> D
D --> E[Pressure classification]
E --> F[Diagnostic alerts]
E --> G[Worker retirement and cache cleanup]
Technical reviewBest possible solution: Keep diagnostic refinement in host-memory.ts, compute working sets from matching cgroup counters and ceilings, and preserve raw fallback for other process-limit sources. Do we have a high-confidence way to reproduce the issue? I did not run a reproduction; current-main accounting and the linked report's cgroup counters establish the false-alert path, and source inspection establishes the introduced undercount. Is this the best way to solve the issue? Reusing the host-memory owner is appropriate, but subtracting cache from reconstructed availability loses above-limit usage and mixes cgroup cache with potentially different process limits. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against bf848c90cedc. Provenance checked
TestingProof path: unit tests only. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +44, Tests +106. Total +150 across 4 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall readiness starts with the lower of the proof and patch scores. It is capped at 3/6 if the fix is partial, the change offers little benefit, a product decision is still open, or the PR reverses an earlier decision without saying why. ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 29, 2026, 7:30 PM ET / November 30, 2026, 00:30 UTC. |
|
Closing as a duplicate of #208658, which opened two minutes earlier for the same issue (#208631) and has a more correct implementation. My version subtracted reclaimable cache from reconstructed availability. ClawSweeper found two real defects in that approach, both of which #208658 already avoids by using working sets only when the verified leaf matches the sampled limit:
I have posted those as review notes on #208658 so its author can keep the guard it already has. Thanks. |
Fixes #208631
What Problem This Solves
On Linux with a cgroup v2 memory limit, a workload that reads or writes files fills the cgroup with page cache until
memory.currentreaches the limit. That is normal kernel behavior: the kernel drops those pages on demand. The diagnostic memory sampler counted that reclaimable cache as used memory, so a Gateway whose anonymous memory was a few percent of its limit loggedevery five minutes and published
openclaw.memory.critical. The channel's subscribers retire idle SQLite workers and clear session snapshot caches, so warm state was thrown away while memory was plentiful. Operators got up to 288 false critical alerts a day that never cleared and hid real pressure.Why This Change Was Made
emitDiagnosticMemorySample(src/logging/diagnostic-memory.ts:373) computedusedBytes = limit - process.availableMemory(). On cgroup v2, libuv's available memory ismemory.max - memory.current, andmemory.currentincludes file cache. The delegated reader (readDelegatedCgroupMemory,src/infra/host-memory.ts:284) used the same rawmemory.current.The module already owns the correct accounting:
cgroupV2WorkingSetBytessubtractsinactive_filefor worker budgets and ancestor checks. This change reuses it rather than introducing a new owner:inactive_fileread out ofcgroupV2WorkingSetBytesintocgroupV2InactiveFileBytes, then exposereadReclaimableCgroupFileCacheBytesfor the process leaf. It returnsundefinedunless the verified leaf exposesmemory.stat, so the raw-charge fallback is preserved when the counter is unavailable.memory.statread happens only near the threshold.Two boundaries are held:
readDelegatedCgroupMemory.availableBytesandcaptureServiceAvailableMemoryReaderalso serve cron scheduling and worker sizing; a classification repair must not move them. Onlyprocess_memory_thresholdin the diagnostic sampler changes.This is a narrower version of #197039, which was parked as a closed draft for lacking a performance qualification. It does not move the cgroup memory owner into
src/process, does not change worker-retirement policy, and makes no performance claim.Evidence
Red on base, green with the fix
src/logging/diagnostic-memory.test.ts→excludes reclaimable file cache from process pressure so warm cache never signals criticalsimulates the reported shape: a 1 GiB cgroup fully charged with 20 MiB anonymous and the rest reclaimable inactive cache. It asserts no pressure event and no retirement publication.keeps genuine anonymous pressure after excluding reclaimable file cacheproves the fix does not silence real pressure: 1 GiB charged with only 100 MiB reclaimable leaves a 924 MiB working set, stillcritical.skips the reclaimable-cache read while raw usage stays below warningpins the gate: at 50% charge the cache reader is never called.src/infra/host-memory.test.ts→reads reclaimable inactive file cache from the process leafcovers the new reader: nomemory.statmeansundefined(raw fallback), a presentmemory.statyieldsinactive_file, and a moved leaf without its ownmemory.statdoes not inherit the previous leaf's cache.Counterfactual — reverting only the subtraction in
diagnostic-memory.ts(keeping the export so the suite compiles):With the fix restored:
The failing case is the reported defect: the sampler treats warm page cache as critical process pressure.
Consumer tests of the shared owner stay green:
node scripts/run-vitest.mjs run src/infra/worker-memory-budget.test.ts src/infra/shared-worker-host.test.ts # Tests 21 passed (21) + Tests 9 passed (9)oxlintandoxfmt --checkare clean on the changed files.Not tested
docker run --memory=1greproduction could not be run locally. The regression simulates the exactmemory.current/inactive_fileshape through the real reader and the real sampler, but a v2 runner should confirm the end-to-end alert disappears.memory.statread happens only once the raw charge crosses the warning ratio.memory.stat; if a future change needs cache-adjusted numbers at all levels, the gate would need widening.