dotnet / dotnet/AspNetCore.Docs
Document concurrency issues in keeping mutable shared state in ASP.NET Core InputFormatter & Outputformtter
- 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
Assessment
This issue has not been assessed yet.