Skip to content

Fix cgroup v2 memory usage crash on hosts and WSL - #7754

Open
LouisDeconinck wants to merge 1 commit into
dotnet:mainfrom
LouisDeconinck:fix/cgroupv2-wsl-memory-usage
Open

LouisDeconinck wants to merge 1 commit into
dotnet:mainfrom
LouisDeconinck:fix/cgroupv2-wsl-memory-usage

Conversation

@LouisDeconinck

@LouisDeconinck LouisDeconinck commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #7748.

On cgroup v2, the root cgroup has no memory.current file (it only exists on non-root cgroups). That is the case when an app runs directly on a host or on WSL, so LinuxUtilizationParserCgroupV2.GetMemoryUsageInBytes falls back to summing memory.current across the top-level *.slice directories.

The bug: the summed slice usage was then compared against the inactive_file value of the root memory.stat, which accounts for the entire system (page cache, init.scope, etc.). On WSL the root inactive_file can exceed the total slice usage (e.g. 492773376 vs 140439552), producing a negative result and an InvalidOperationException that crashes the app at startup.

The fix makes both operands come from the same cgroups:

  • GetMemoryUsageInBytesFromSlices now subtracts each slice's own inactive_file (read from the slice's memory.stat) from its memory.current. Within a cgroup inactive_file is always a subset of memory.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.
  • GetMemoryUsageInBytes returns that sum directly instead of subtracting the root inactive_file. The container/pod path (where /sys/fs/cgroup/memory.current exists) is unchanged, including its exception.
  • Exception messages in the new path name the actual slice files that were read.

To exercise the slices fallback in tests, HardcodedValueFileSystem.GetDirectoryNames was fixed to emulate Directory.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_file larger than total slice usage), per-slice memory.stat subtraction, missing slice memory.stat, and the exception naming the slice's memory.stat path.

Microsoft Reviewers: Open in CodeFlow

Copilot AI lite review requested due to automatic review settings September 13, 2026 20:56
@LouisDeconinck
LouisDeconinck requested a review from a team as a code owner September 13, 2026 20:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_file from its memory.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fail to start application when using Microsoft.Extensions.Diagnostics.ResourceMonitoring on WSL

2 participants