dotnet / dotnet/AspNetCore.Docs

Document concurrency issues in keeping mutable shared state in ASP.NET Core InputFormatter & Outputformtter

Open
#25,468 3 comments 0 reactions 0 assignees View on GitHub
area-web-frameworks re-Aditya
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 2h
Merged PRs (30d)
109

Description

Recently we tracked down issues related to shared state update, issue happens when ASP.NET Core API service under load. Investigation found that , their was some **mutable shared state** in one of our **output formatter** implementation which was affecting request processing.
Most of our code uses dependency injection to with proper scope for each of the dependencies required for processing the request. In this formatter are inserted using MVCOption as indicated below was **getting reused** for **all the request executing concurrently** hence the issue.

**Its good add explicit note in the documentation to highlight implication of storing mutable shared state**

```csharp
builder.Services.AddControllers(mvcoptions =>
{
mvcoptions.InputFormatters.Insert(0, new CustomInputFormatter());
mvcoptions.OutputFormatters.Insert(0, new CustomOutputFormatter());
});

public class CustomOutputFormatter : OutputFormatter
{
string SharedState;
.....
}

```

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.