Fix cgroup v2 memory usage crash on hosts and WSL - #7754
Open
LouisDeconinck wants to merge 1 commit into
Open
LouisDeconinck wants to merge 1 commit into
LouisDeconinck wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved issues were identified that would block approval.
Pull request overview
Fixes cgroup v2 memory usage crashes on hosts and WSL by calculating slice usage from matching per-slice statistics.
Changes:
- Subtracts each slice’s
inactive_filefrom itsmemory.current. - Improves test filesystem glob emulation.
- Adds WSL and per-slice regression tests.
File summaries
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Linux/Resources/HardcodedValueFileSystem.cs | Updated as part of this pull request. |
| test/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring.Tests/Linux/LinuxUtilizationParserCgroupV2Tests.cs | Updated as part of this pull request. |
| src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/Linux/LinuxUtilizationParserCgroupV2.cs | Updated as part of this pull request. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
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 #7748.
On cgroup v2, the root cgroup has no
memory.currentfile (it only exists on non-root cgroups). That is the case when an app runs directly on a host or on WSL, soLinuxUtilizationParserCgroupV2.GetMemoryUsageInBytesfalls back to summingmemory.currentacross the top-level*.slicedirectories.The bug: the summed slice usage was then compared against the
inactive_filevalue of the rootmemory.stat, which accounts for the entire system (page cache,init.scope, etc.). On WSL the rootinactive_filecan exceed the total slice usage (e.g. 492773376 vs 140439552), producing a negative result and anInvalidOperationExceptionthat crashes the app at startup.The fix makes both operands come from the same cgroups:
GetMemoryUsageInBytesFromSlicesnow subtracts each slice's owninactive_file(read from the slice'smemory.stat) from itsmemory.current. Within a cgroupinactive_fileis always a subset ofmemory.current, so the result can't go legitimately negative; each slice's difference is still clamped at zero because the two files are read at different points in time.GetMemoryUsageInBytesreturns that sum directly instead of subtracting the rootinactive_file. The container/pod path (where/sys/fs/cgroup/memory.currentexists) is unchanged, including its exception.To exercise the slices fallback in tests,
HardcodedValueFileSystem.GetDirectoryNameswas fixed to emulateDirectory.GetDirectories(directory, pattern)(glob matching of direct children, distinct results) — it previously treated the pattern as a regex matched against full file paths, which threw on the"*.slice"glob the parser passes in production.Regression tests added: WSL scenario (root
inactive_filelarger than total slice usage), per-slicememory.statsubtraction, missing slicememory.stat, and the exception naming the slice'smemory.statpath.Microsoft Reviewers: Open in CodeFlow