-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory Leak in Microsoft.Extensions.Caching.Memory when handling exceptions #42355
Conversation
…ptions When an exception is thrown inside MemoryCache.GetOrCreate, we are leaking CacheEntry objects. This is because they are not being Disposed properly, and the async local CacheEntryStack is growing indefinitely. The fix is to ensure the CacheEntry objects are disposed correctly. In order to do this, I set a flag to indicate whether the CacheEntry.Value has been set. If it hasn't, Disposing the CacheEntry won't add it to the cache. Fix dotnet#42321
I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @eerhardt, @maryamariyan |
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/MemoryCacheExtensions.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/MemoryCacheExtensions.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Caching.Abstractions/src/MemoryCacheExtensions.cs
Outdated
Show resolved
Hide resolved
I believe all current feedback has been addressed. I also renamed 2 variables to better describe what they are doing. PTAL. |
…inner cache's entries will reference the outer cache entries through the ScopeLease object. Null'ing out the CacheEntry._scope field when it is disposed fixes this issue.
/backport to release/5.0-rc2 |
Started backporting to release/5.0-rc2: https://github.com/dotnet/runtime/actions/runs/262733314 |
When an exception is thrown inside MemoryCache.GetOrCreate, we are leaking CacheEntry objects. This is because they are not being Disposed properly, and the async local CacheEntryStack is growing indefinitely.
The fix is to ensure the CacheEntry objects are disposed correctly. In order to do this, I set a flag to indicate whether the CacheEntry.Value has been set. If it hasn't, Disposing the CacheEntry won't add it to the cache.
Fix #42321