Fail to start application when using Microsoft.Extensions.Diagnostics.ResourceMonitoring on WSL
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 894
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 23
Description
### Description
When adding resource monitoring with `services.AddResourceMonitoring()`, the application fails to start with the following error message on WSL (Ubuntu 24.04):
> System.InvalidOperationException: 'The total memory usage read from '/sys/fs/cgroup/memory.current' is lesser than inactive memory read from '/sys/fs/cgroup/memory.stat'.'
Debugging the method `LinuxUtilizationParserCgroupV2.GetMemoryUsageInBytes`, revealed that the memory usage is not read from `/sys/fs/cgroup/memory.current` (which does not exist) but from `sys/fs/cgroup/user.slice/memory.current` and `/sys/fs/cgroup/system.slice/memory.current`.
The calculated total memory usage is then a large negative number, which seems to be odd.
Example:
inactive memory: 492773376
memory usage: 140439552
140439552 - 492773376 = -352333824
### Reproduction Steps
Start a .NET application which uses `Microsoft.Extensions.Diagnostics.ResourceMonitoring` on WSL (maybe only with Ubuntu 24.04?).
### Expected behavior
1. No exception to be thrown.
2. The exception message should show the correct path/file name of the actually read `memory.current` files.
### Actual behavior
An `InvalidOperationException` is thrown and the exception message is displaying the wrong path of the actually read `memory.current` files.
### Regression?
_No response_
### Known Workarounds
No.
### Configuration
WSL version: 2.7.12.0 with .NET SDK 10.0.111
Running on Windows 11 (25H2 (OS Build 26200.9106)
### Other information
_No response_
Contributor guide
Research direction
Start with LinuxUtilizationParserCgroupV2.GetMemoryUsageInBytes and reproduce the issue on WSL Ubuntu 24.04 using an application configured with services.AddResourceMonitoring(). Check how memory.current and memory.stat are selected under /sys/fs/cgroup, then verify that startup succeeds and any exception reports the actual memory.current paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- observability-sre
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100