dotnet / dotnet/aspnetcore

FromQueryAttribute + argument type with a Action property will cause serious memory leaks

Open
#57,892 8 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Hello everyone, I believe I have encountered a very rare but serious bug: When developing an Asp.net WebApi program, if you use the **FromQueryAttribute** to describe an API action, and the parameter type contains a property of the Action type, simply running the project will exhaust all the memory on the local machine within a few seconds. This happens even without calling the WebApi; just running it in Visual Studio (by pressing F5) is enough.

### Expected Behavior

Does not cause incorrect memory consumption

### Steps To Reproduce

Open Visual Studio 2022, Create a new Asp.net Core WebAPI project, and just edit the default controller like this:
`
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
public class MyArgs
{
public string? Name { get; set; } // string is ok

public Action? DoSomething { get; set; } // BAD here!

//public Action[]? MoreActions { get; set; } // Action array is OK
}

[HttpGet("SayHello")]
public string Get([FromQuery] MyArgs args) // [FromQuery] must be used, otherwise everything will be fine.
{
return "Hello";
}
}
`
[WebApplication1.zip](https://github.com/user-attachments/files/17015208/WebApplication1.zip)

There are a few key points to keep in mind when reproducing this issue:

1. The project must be run in debug mode within Visual Studio; running it via the command line with `dotnet run` will not cause the memory leak.
2. The FromQuery attribute must be used in conjunction with a property of the Action type. However, using Action[] (an array of Action) will not trigger this issue.
3. My computer runs on Windows 10 with an AMD Ryzen 5 5600X CPU, and it seems that my other Intel-based computer does not have this issue.
4. Restarting the computer does not resolve this issue.

### Exceptions (if any)

My computer has 32GB of memory, but running this program for just a few seconds completely exhausts it. If I don't take a screenshot quickly, the computer will become unresponsive due to the memory being depleted.

### .NET Version

8.0.400

### Anything else?

Screenshot 2024-09-16 225214
Screenshot 2024-09-16 222848

`
.NET SDK:
Version: 8.0.400
Commit: 36fe6dda56
Workload version: 8.0.400-manifests.6c274a57
MSBuild version: 17.11.3+0c8610977

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.400\

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[aspire]
Installation Source: VS 17.11.35303.130
Manifest Version: 8.1.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.1.0\WorkloadManifest.json
Install Type: FileBased

Host:
Version: 8.0.8
Architecture: x64
Commit: 08338fcaa5

.NET SDKs installed:
6.0.410 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.