dotnet / dotnet/aspnetcore

Response cache middleware corrupts data

Open
#58,164 1 comment 0 reactions 0 assignees View on GitHub
area-middleware feature-response-caching
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

Time to time webpage receives corrupted json text:

"\u0000\u0000\u0000 ... \u0000\u0000\u0000\u00009cd8-287f-4df0-a645-558e5cff0c63","name":"Corn","symbols":["ZCE?1","ZCE?2","ZCE?3","ZCE?4","ZCE?5","ZCE?6","F.US.ZCEZ24"]},{"id":"de2579b3-5340-40ff-9ab3-dea0dbe774dc","name":"MN Spring Wheat","symbols":["F.US.MWEU23","F.US.MWEZ23","F.US.MWEH24","F.US.MWEK24","F.US.MWEN24","F.US.MWEU24","F.US.MWEZ24"]},{"id":"e4300ad3-ae20-4ea1-ba6e-fcae7b12b41f","name":"Metals","symbols":["GCE","FGD","CPE","TGD","SIE","LDKZ","GCES2","PLE","TPL","LALZ","LZHZ","PA","PAE","LNIZ","TMG"]},{"id":"ee406e54-b082-4dce-9eb0-361467ebd4a2","name":"Corn","symbols":["ZCE?1","ZCE?2","ZCE?3","ZCE?4","ZCE?5","ZCE?6","F.US.ZCEZ24"]}]"

I.e. significant part of the response is replaced by \u0000. It looks as if memory buffer was partially erased.

I am using ASP.NET Core 8 with response caching, hosted within docker container in kuber cluster.

Controller code:
```
[ResponseCache(VaryByQueryKeys = ["ts", "brandName"], Duration = 2592000 /* 30 days */)]
public async Task GetBrandSymbolLists(string brandName) {
try
{
AssetPackageSymbolListDto[] result = [];
var brand = await em.Brand.GetBrandByNameAsync(brandName);
var packageId = await em.BrandContentProfile.GetProfilePackageIdAsync(brand.ProfileId);

if (packageId.HasValue)
{
result = (await em.AssetPackageSymbolList
.GetByPackageIdAsync(packageId.Value))
.Select(l => new AssetPackageSymbolListDto(l))
.ToArray();
}

return Ok(result);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to get profile symbol lists");
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
```
This happens once a month, not so often but it happens. Already get several complains from our clients.
Previously I tried using response caching middleware for static files. Time to time server started serving zeroed content. Removing `public` cache-control directive helped to workaround the problem but also disabled server cache.

We started observing this issue since we migrated to asp.net 8.0 from 6.0.

### Expected Behavior

No data corruption when serving response.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

8.0.7

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the GetBrandSymbolLists controller action, its ResponseCache attribute, and the ASP.NET Core response caching middleware in the reported .NET 8.0.7 Docker/Kubernetes setup. There is no reproduction case or named test; done means serving the cached JSON response without the reported \u0000 corruption.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, docker, kubernetes
Domain
backend, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.